#region License /* * Logger.cs * * The MIT License * * Copyright (c) 2013 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 the simple logging functions. /// /// /// /// The Logger class provides some methods that output the logs associated with the each /// values. /// If the value associated with a log was less than the , /// the log could not be outputted. /// /// /// The default output action used by the output methods outputs the log data to the standard output stream /// and writes the same log data to the if it has a valid path. /// /// /// If you wanted to run your custom output action, you would replace the current output action with /// your output action by using the method. /// /// 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 the and /// initializes the path to the log file with . /// public Logger () : this (LogLevel.ERROR, null, defaultOutput) { } /// /// Initializes a new instance of the class /// with the specified logging . /// /// /// This constructor initializes the path to the log file with . /// /// /// One of the values to initialize. /// public Logger (LogLevel level) : this (level, null, defaultOutput) { } /// /// Initializes a new instance of the class /// with the specified logging , path to the log /// and action. /// /// /// One of the values to initialize. /// /// /// A that contains a path to the log file to initialize. /// /// /// An Action<LogData, string> delegate that references the method(s) to initialize. /// A parameter to pass to the method(s) is the value of /// if any. /// public Logger (LogLevel level, string file, Action output) { _level = level; _file = file; if (output != null) _output = output; else _output = defaultOutput; _sync = new object (); } #endregion #region Public Properties /// /// Gets or sets the path to the log file. /// /// /// A that contains a path to the log file. /// public string File { get { return _file; } set { lock (_sync) { _file = value; } } } /// /// Gets or sets the current logging level. /// /// /// A log associated with a less than the current logging level can not be outputted. /// /// /// One of the values that indicates the current logging level. /// public LogLevel Level { get { return _level; } set { _level = value; } } #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) writeLine (log, path); } private void output (LogLevel level, string message) { if (level < _level || message == null || message.Length == 0) return; lock (_sync) { LogData data = null; try { data = new LogData (DateTime.Now, level, new StackFrame (2, true), message); _output (data, _file); } catch (Exception ex) { data = new LogData (DateTime.Now, LogLevel.FATAL, new StackFrame (0, true), ex.Message); Console.WriteLine (data.ToString ()); } } } private static void writeLine (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 the specified as a log with the . /// /// /// This method does not output as a log /// if the current logging level is greater than the . /// /// /// A that contains a message to output as a log. /// public void Debug (string message) { output (LogLevel.DEBUG, message); } /// /// Outputs the specified as a log with the . /// /// /// This method does not output as a log /// if the current logging level is greater than the . /// /// /// A that contains a message to output as a log. /// public void Error (string message) { output (LogLevel.ERROR, message); } /// /// Outputs the specified as a log with the . /// /// /// This method does not output as a log /// if the current logging level is greater than the . /// /// /// A that contains a message to output as a log. /// public void Fatal (string message) { output (LogLevel.FATAL, message); } /// /// Outputs the specified as a log with the . /// /// /// This method does not output as a log /// if the current logging level is greater than the . /// /// /// A that contains a message to output as a log. /// public void Info (string message) { output (LogLevel.INFO, message); } /// /// Replaces the current output action with the specified action. /// /// /// This method replaces the current output action with the default output action /// if is . /// /// /// An Action<LogData, string> delegate that references the method(s) to set. /// A parameter to pass to the method(s) is the value of /// if any. /// public void SetOutput (Action output) { lock (_sync) { if (output != null) _output = output; else _output = defaultOutput; } } /// /// Outputs the specified as a log with the . /// /// /// This method does not output as a log /// if the current logging level is greater than the . /// /// /// A that contains a message to output as a log. /// public void Trace (string message) { output (LogLevel.TRACE, message); } /// /// Outputs the specified as a log with the . /// /// /// This method does not output as a log /// if the current logging level is greater than the . /// /// /// A that contains a message to output as a log. /// public void Warn (string message) { output (LogLevel.WARN, message); } #endregion } }