#region License /* * Logger.cs * * The MIT License * * Copyright (c) 2013-2015 sta.blockhead * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ #endregion using System; using System.Diagnostics; using System.IO; namespace WebSocketSharp { /// /// Provides a set of methods and properties for logging. /// /// /// /// If you output a log with lower than the value of the property, /// it cannot be outputted. /// /// /// The default output action writes a log to the standard output stream and the log file /// if the property has a valid path to it. /// /// /// If you would like to use the custom output action, you should set /// the property to any Action<LogData, string> /// delegate. /// /// public class Logger { #region Private Fields private volatile string _file; private volatile LogLevel _level; private Action _output; private object _sync; #endregion #region Public Constructors /// /// Initializes a new instance of the class. /// /// /// This constructor initializes the current logging level with . /// public Logger () : this (LogLevel.Error, null, null) { } /// /// Initializes a new instance of the class with /// the specified logging . /// /// /// One of the enum values. /// public Logger (LogLevel level) : this (level, null, null) { } /// /// Initializes a new instance of the class with /// the specified logging , path to the log , /// and action. /// /// /// One of the enum values. /// /// /// A that represents the path to the log file. /// /// /// An Action<LogData, string> delegate that references the method(s) used to /// output a log. A parameter passed to this delegate is /// . /// public Logger (LogLevel level, string file, Action output) { _level = level; _file = file; _output = output ?? defaultOutput; _sync = new object (); } #endregion #region Public Properties /// /// Gets or sets the current path to the log file. /// /// /// A that represents the current path to the log file if any. /// public string File { get { return _file; } set { lock (_sync) { _file = value; Warn ( String.Format ("The current path to the log file has been changed to {0}.", _file)); } } } /// /// Gets or sets the current logging level. /// /// /// A log with lower than the value of this property cannot be outputted. /// /// /// One of the enum values, specifies the current logging level. /// public LogLevel Level { get { return _level; } set { lock (_sync) { _level = value; Warn (String.Format ("The current logging level has been changed to {0}.", _level)); } } } /// /// Gets or sets the current output action used to output a log. /// /// /// /// An Action<LogData, string> delegate that references the method(s) used to /// output a log. A parameter passed to this delegate is the value of /// the property. /// /// /// If the value to set is , the current output action is changed to /// the default output action. /// /// public Action Output { get { return _output; } set { lock (_sync) { _output = value ?? defaultOutput; Warn ("The current output action has been changed."); } } } #endregion #region Private Methods private static void defaultOutput (LogData data, string path) { var log = data.ToString (); Console.WriteLine (log); if (path != null && path.Length > 0) writeToFile (log, path); } private void output (string message, LogLevel level) { lock (_sync) { if (_level > level) return; LogData data = null; try { data = new LogData (level, new StackFrame (2, true), message); _output (data, _file); } catch (Exception ex) { data = new LogData (LogLevel.Fatal, new StackFrame (0, true), ex.Message); Console.WriteLine (data.ToString ()); } } } private static void writeToFile (string value, string path) { using (var writer = new StreamWriter (path, true)) using (var syncWriter = TextWriter.Synchronized (writer)) syncWriter.WriteLine (value); } #endregion #region Public Methods /// /// Outputs as a log with . /// /// /// If the current logging level is higher than , /// this method doesn't output as a log. /// /// /// A that represents the message to output as a log. /// public void Debug (string message) { if (_level > LogLevel.Debug) return; output (message, LogLevel.Debug); } /// /// Outputs as a log with . /// /// /// If the current logging level is higher than , /// this method doesn't output as a log. /// /// /// A that represents the message to output as a log. /// public void Error (string message) { if (_level > LogLevel.Error) return; output (message, LogLevel.Error); } /// /// Outputs as a log with . /// /// /// A that represents the message to output as a log. /// public void Fatal (string message) { output (message, LogLevel.Fatal); } /// /// Outputs as a log with . /// /// /// If the current logging level is higher than , /// this method doesn't output as a log. /// /// /// A that represents the message to output as a log. /// public void Info (string message) { if (_level > LogLevel.Info) return; output (message, LogLevel.Info); } /// /// Outputs as a log with . /// /// /// If the current logging level is higher than , /// this method doesn't output as a log. /// /// /// A that represents the message to output as a log. /// public void Trace (string message) { if (_level > LogLevel.Trace) return; output (message, LogLevel.Trace); } /// /// Outputs as a log with . /// /// /// If the current logging level is higher than , /// this method doesn't output as a log. /// /// /// A that represents the message to output as a log. /// public void Warn (string message) { if (_level > LogLevel.Warn) return; output (message, LogLevel.Warn); } #endregion } }