From 2ec26308a76661f503e5a57f183df20b734be0b7 Mon Sep 17 00:00:00 2001 From: sta Date: Mon, 3 Mar 2014 17:22:29 +0900 Subject: [PATCH] Refactored Logger.cs and LogData.cs --- README.md | 2 +- websocket-sharp/LogData.cs | 34 ++++--- websocket-sharp/Logger.cs | 195 ++++++++++++++++++++----------------- 3 files changed, 122 insertions(+), 109 deletions(-) diff --git a/README.md b/README.md index 7a333e6f..4251d0b2 100644 --- a/README.md +++ b/README.md @@ -474,7 +474,7 @@ So if you would like to change the current logging level (`WebSocketSharp.LogLev ws.Log.Level = LogLevel.Debug; ``` -This means a log with less than `LogLevel.Debug` cannot be outputted. +This means a log with lower than `LogLevel.Debug` cannot be outputted. And if you would like to output a log, you should use any of the output methods. The following outputs a log with `LogLevel.Debug`. diff --git a/websocket-sharp/LogData.cs b/websocket-sharp/LogData.cs index 3c142f14..fe0e7fa9 100644 --- a/websocket-sharp/LogData.cs +++ b/websocket-sharp/LogData.cs @@ -4,8 +4,8 @@ * * The MIT License * - * Copyright (c) 2013 sta.blockhead - * + * Copyright (c) 2013-2014 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 @@ -15,7 +15,7 @@ * * 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 @@ -33,7 +33,7 @@ using System.Text; namespace WebSocketSharp { /// - /// Represents the log data used by the class. + /// Represents a log data used by the class. /// public class LogData { @@ -52,7 +52,7 @@ namespace WebSocketSharp { _level = level; _caller = caller; - _message = message; + _message = message ?? String.Empty; _date = DateTime.Now; } @@ -64,7 +64,7 @@ namespace WebSocketSharp /// Gets the information of the logging method caller. /// /// - /// A that contains the information of a logging method caller. + /// A that provides the information of the logging method caller. /// public StackFrame Caller { get { @@ -76,7 +76,7 @@ namespace WebSocketSharp /// Gets the date and time when the log data was created. /// /// - /// A that contains the date and time when the log data was created. + /// A that represents the date and time when the log data was created. /// public DateTime Date { get { @@ -85,11 +85,10 @@ namespace WebSocketSharp } /// - /// Gets the logging level associated with the log data. + /// Gets the logging level of the log data. /// /// - /// One of the values that indicates the logging level - /// associated with the log data. + /// One of the enum values, indicates the logging level of the log data. /// public LogLevel Level { get { @@ -101,7 +100,7 @@ namespace WebSocketSharp /// Gets the message of the log data. /// /// - /// A that contains the message of a log data. + /// A that represents the message of the log data. /// public string Message { get { @@ -126,7 +125,8 @@ namespace WebSocketSharp var type = method.DeclaringType; #if DEBUG var lineNum = _caller.GetFileLineNumber (); - var headerAndCaller = String.Format ("{0}{1}.{2}:{3}|", header, type.Name, method.Name, lineNum); + var headerAndCaller = String.Format ( + "{0}{1}.{2}:{3}|", header, type.Name, method.Name, lineNum); #else var headerAndCaller = String.Format ("{0}{1}.{2}|", header, type.Name, method.Name); #endif @@ -135,14 +135,16 @@ namespace WebSocketSharp if (messages.Length <= 1) return String.Format ("{0}{1}", headerAndCaller, _message); - var output = new StringBuilder (String.Format ("{0}{1}\n", headerAndCaller, messages [0]), 64); + var log = new StringBuilder ( + String.Format ("{0}{1}\n", headerAndCaller, messages [0]), 64); + var space = header.Length; var format = String.Format ("{{0,{0}}}{{1}}\n", space); for (var i = 1; i < messages.Length; i++) - output.AppendFormat (format, "", messages [i]); + log.AppendFormat (format, "", messages [i]); - output.Length--; - return output.ToString (); + log.Length--; + return log.ToString (); } #endregion diff --git a/websocket-sharp/Logger.cs b/websocket-sharp/Logger.cs index 50ede317..415f2e2c 100644 --- a/websocket-sharp/Logger.cs +++ b/websocket-sharp/Logger.cs @@ -4,8 +4,8 @@ * * The MIT License * - * Copyright (c) 2013 sta.blockhead - * + * Copyright (c) 2013-2014 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 @@ -15,7 +15,7 @@ * * 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 @@ -33,22 +33,20 @@ using System.IO; namespace WebSocketSharp { /// - /// Provides the simple logging functions. + /// Provides a set of methods and properties for logging. /// /// /// - /// The Logger class provides some methods that output the logs associated with the each - /// values. - /// If the value associated with a log is less than the , - /// the log can not be outputted. + /// If you output a log with lower than the , + /// it cannot 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. + /// The default output action writes a log to the standard output stream and + /// the if it has a valid path. /// /// - /// If you want to run custom output action, you can replace the current output action with - /// your output action by using the method. + /// If you would like to use the custom output action, you should set the + /// to any Action<LogData, string>. /// /// public class Logger @@ -68,8 +66,7 @@ namespace WebSocketSharp /// 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 . + /// This constructor initializes the current logging level with . /// public Logger () : this (LogLevel.Error, null, null) @@ -77,14 +74,11 @@ namespace WebSocketSharp } /// - /// Initializes a new instance of the class - /// with the specified logging . + /// 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. + /// One of the enum values. /// public Logger (LogLevel level) : this (level, null, null) @@ -92,26 +86,26 @@ namespace WebSocketSharp } /// - /// Initializes a new instance of the class - /// with the specified logging , path to the log - /// and action. + /// Initializes a new instance of the class with the specified + /// logging , path to the log , and + /// action. /// /// - /// One of the values to initialize. + /// One of the enum values. /// /// - /// A that contains a path to the log file to initialize. + /// A that represents the path to the log file. /// /// - /// An Action<LogData, string> delegate that references the method(s) to initialize. - /// A parameter to pass to the method(s) is the value of the - /// if any. + /// An Action<LogData, string> delegate that references the method(s) + /// used to output a log. A parameter passed to this delegate + /// is if any. /// public Logger (LogLevel level, string file, Action output) { _level = level; _file = file; - _output = output != null ? output : defaultOutput; + _output = output ?? defaultOutput; _sync = new object (); } @@ -123,7 +117,7 @@ namespace WebSocketSharp /// Gets or sets the path to the log file. /// /// - /// A that contains a path to the log file if any. + /// A that represents the path to the log file if any. /// public string File { get { @@ -131,10 +125,10 @@ namespace WebSocketSharp } set { - lock (_sync) - { + lock (_sync) { _file = value; - Warn (String.Format ("The current path to the log file has been changed to {0}.", _file ?? "")); + Warn ( + String.Format ("The current path to the log file has been changed to {0}.", _file)); } } } @@ -143,10 +137,10 @@ namespace WebSocketSharp /// Gets or sets the current logging level. /// /// - /// A log associated with a less than the current logging level can not be outputted. + /// A log with lower than the value of this property cannot be outputted. /// /// - /// One of the values that indicates the current logging level. + /// One of the enum values, indicates the current logging level. /// public LogLevel Level { get { @@ -154,8 +148,37 @@ namespace WebSocketSharp } set { - _level = value; - Warn (String.Format ("The current logging level has been changed to {0}.", _level)); + 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 if any. + /// + /// + /// 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."); + } } } @@ -168,16 +191,15 @@ namespace WebSocketSharp var log = data.ToString (); Console.WriteLine (log); if (path != null && path.Length > 0) - writeLine (log, path); + writeToFile (path, log); } private void output (string message, LogLevel level) { - if (level < _level || message == null || message.Length == 0) - return; + lock (_sync) { + if (_level > level) + return; - lock (_sync) - { LogData data = null; try { data = new LogData (level, new StackFrame (2, true), message); @@ -190,11 +212,10 @@ namespace WebSocketSharp } } - private static void writeLine (string value, string path) + private static void writeToFile (string path, string value) { using (var writer = new StreamWriter (path, true)) - using (var syncWriter = TextWriter.Synchronized (writer)) - { + using (var syncWriter = TextWriter.Synchronized (writer)) { syncWriter.WriteLine (value); } } @@ -204,44 +225,46 @@ namespace WebSocketSharp #region Public Methods /// - /// Outputs the specified as a log with the . + /// Outputs as a log with . /// /// - /// If the current logging level is greater than the , - /// this method does not output as a log. + /// If the current logging level is higher than , this method + /// doesn't output as a log. /// /// - /// A that contains a message to 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 the specified as a log with the . + /// Outputs as a log with . /// /// - /// If the current logging level is greater than the , - /// this method does not output as a log. + /// If the current logging level is higher than , this method + /// doesn't output as a log. /// /// - /// A that contains a message to 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 the specified as a log with the . + /// Outputs as a log with . /// - /// - /// If the current logging level is greater than the , - /// this method does not output as a log. - /// /// - /// A that contains a message to output as a log. + /// A that represents the message to output as a log. /// public void Fatal (string message) { @@ -249,68 +272,56 @@ namespace WebSocketSharp } /// - /// Outputs the specified as a log with the . + /// Outputs as a log with . /// /// - /// If the current logging level is greater than the , - /// this method does not output as a log. + /// If the current logging level is higher than , this method + /// doesn't output as a log. /// /// - /// A that contains a message to output as a log. + /// A that represents the message to output as a log. /// public void Info (string message) { - output (message, LogLevel.Info); - } + if (_level > LogLevel.Info) + return; - /// - /// Replaces the current output action with the specified action. - /// - /// - /// If is , - /// this method replaces the current output action with the default output action. - /// - /// - /// An Action<LogData, string> delegate that references the method(s) to set. - /// A parameter to pass to the method(s) is the value of the - /// if any. - /// - public void SetOutput (Action output) - { - lock (_sync) - { - _output = output != null ? output : defaultOutput; - Warn ("The current output action has been replaced."); - } + output (message, LogLevel.Info); } /// - /// Outputs the specified as a log with the . + /// Outputs as a log with . /// /// - /// If the current logging level is greater than the , - /// this method does not output as a log. + /// If the current logging level is higher than , this method + /// doesn't output as a log. /// /// - /// A that contains a message to 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 the specified as a log with the . + /// Outputs as a log with . /// /// - /// If the current logging level is greater than the , - /// this method does not output as a log. + /// If the current logging level is higher than , this method + /// doesn't output as a log. /// /// - /// A that contains a message to 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); }