Modified LogData.cs, LogLevel.cs, Logger.cs

This commit is contained in:
sta 2013-07-21 16:17:30 +09:00
parent 3e6c589953
commit 998a296d18
3 changed files with 56 additions and 60 deletions

View File

@ -30,13 +30,13 @@ using System;
using System.Diagnostics; using System.Diagnostics;
using System.Text; using System.Text;
namespace WebSocketSharp { namespace WebSocketSharp
{
/// <summary> /// <summary>
/// Represents the log data used by the <see cref="Logger"/> class. /// Represents the log data used by the <see cref="Logger"/> class.
/// </summary> /// </summary>
public class LogData { public class LogData
{
#region Private Fields #region Private Fields
private StackFrame _caller; private StackFrame _caller;
@ -48,12 +48,12 @@ namespace WebSocketSharp {
#region Internal Constructors #region Internal Constructors
internal LogData (DateTime date, LogLevel level, StackFrame caller, string message) internal LogData (LogLevel level, StackFrame caller, string message)
{ {
_date = date;
_level = level; _level = level;
_caller = caller; _caller = caller;
_message = message; _message = message;
_date = DateTime.Now;
} }
#endregion #endregion

View File

@ -28,13 +28,13 @@
using System; using System;
namespace WebSocketSharp { namespace WebSocketSharp
{
/// <summary> /// <summary>
/// Contains the values of the logging level. /// Contains the values of the logging level.
/// </summary> /// </summary>
public enum LogLevel { public enum LogLevel
{
/// <summary> /// <summary>
/// Indicates the bottom logging level. /// Indicates the bottom logging level.
/// </summary> /// </summary>

View File

@ -30,8 +30,8 @@ using System;
using System.Diagnostics; using System.Diagnostics;
using System.IO; using System.IO;
namespace WebSocketSharp { namespace WebSocketSharp
{
/// <summary> /// <summary>
/// Provides the simple logging functions. /// Provides the simple logging functions.
/// </summary> /// </summary>
@ -39,20 +39,20 @@ namespace WebSocketSharp {
/// <para> /// <para>
/// The Logger class provides some methods that output the logs associated with the each /// The Logger class provides some methods that output the logs associated with the each
/// <see cref="LogLevel"/> values. /// <see cref="LogLevel"/> values.
/// If the <see cref="LogLevel"/> value associated with a log was less than the <see cref="Level"/>, /// If the <see cref="LogLevel"/> value associated with a log is less than the <see cref="Level"/>,
/// the log could not be outputted. /// the log can not be outputted.
/// </para> /// </para>
/// <para> /// <para>
/// The default output action used by the output methods outputs the log data to the standard output stream /// 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 <see cref="Logger.File"/> if it has a valid path. /// and writes the same log data to the <see cref="Logger.File"/> if it has a valid path.
/// </para> /// </para>
/// <para> /// <para>
/// If you wanted to run your custom output action, you would replace the current output action with /// If you want to run custom output action, you can replace the current output action with
/// your output action by using the <see cref="SetOutput"/> method. /// your output action by using the <see cref="SetOutput"/> method.
/// </para> /// </para>
/// </remarks> /// </remarks>
public class Logger { public class Logger
{
#region Private Fields #region Private Fields
private volatile string _file; private volatile string _file;
@ -72,7 +72,7 @@ namespace WebSocketSharp {
/// initializes the path to the log file with <see langword="null"/>. /// initializes the path to the log file with <see langword="null"/>.
/// </remarks> /// </remarks>
public Logger () public Logger ()
: this (LogLevel.ERROR, null, defaultOutput) : this (LogLevel.ERROR, null, null)
{ {
} }
@ -87,7 +87,7 @@ namespace WebSocketSharp {
/// One of the <see cref="LogLevel"/> values to initialize. /// One of the <see cref="LogLevel"/> values to initialize.
/// </param> /// </param>
public Logger (LogLevel level) public Logger (LogLevel level)
: this (level, null, defaultOutput) : this (level, null, null)
{ {
} }
@ -104,18 +104,14 @@ namespace WebSocketSharp {
/// </param> /// </param>
/// <param name="output"> /// <param name="output">
/// An <c>Action&lt;LogData, string&gt;</c> delegate that references the method(s) to initialize. /// An <c>Action&lt;LogData, string&gt;</c> delegate that references the method(s) to initialize.
/// A <see cref="string"/> parameter to pass to the method(s) is the value of <see cref="Logger.File"/> /// A <see cref="string"/> parameter to pass to the method(s) is the value of the <see cref="Logger.File"/>
/// if any. /// if any.
/// </param> /// </param>
public Logger (LogLevel level, string file, Action<LogData, string> output) public Logger (LogLevel level, string file, Action<LogData, string> output)
{ {
_level = level; _level = level;
_file = file; _file = file;
if (output != null) _output = output != null ? output : defaultOutput;
_output = output;
else
_output = defaultOutput;
_sync = new object (); _sync = new object ();
} }
@ -127,7 +123,7 @@ namespace WebSocketSharp {
/// Gets or sets the path to the log file. /// Gets or sets the path to the log file.
/// </summary> /// </summary>
/// <value> /// <value>
/// A <see cref="string"/> that contains a path to the log file. /// A <see cref="string"/> that contains a path to the log file if any.
/// </value> /// </value>
public string File { public string File {
get { get {
@ -138,6 +134,7 @@ namespace WebSocketSharp {
lock (_sync) lock (_sync)
{ {
_file = value; _file = value;
Warn (String.Format ("The current path to the log file has been changed to {0}.", _file ?? ""));
} }
} }
} }
@ -158,6 +155,7 @@ namespace WebSocketSharp {
set { set {
_level = value; _level = value;
Warn (String.Format ("The current logging level has been changed to {0}.", _level));
} }
} }
@ -173,7 +171,7 @@ namespace WebSocketSharp {
writeLine (log, path); writeLine (log, path);
} }
private void output (LogLevel level, string message) private void output (string message, LogLevel level)
{ {
if (level < _level || message == null || message.Length == 0) if (level < _level || message == null || message.Length == 0)
return; return;
@ -182,11 +180,11 @@ namespace WebSocketSharp {
{ {
LogData data = null; LogData data = null;
try { try {
data = new LogData (DateTime.Now, level, new StackFrame (2, true), message); data = new LogData (level, new StackFrame (2, true), message);
_output (data, _file); _output (data, _file);
} }
catch (Exception ex) { catch (Exception ex) {
data = new LogData (DateTime.Now, LogLevel.FATAL, new StackFrame (0, true), ex.Message); data = new LogData (LogLevel.FATAL, new StackFrame (0, true), ex.Message);
Console.WriteLine (data.ToString ()); Console.WriteLine (data.ToString ());
} }
} }
@ -206,116 +204,114 @@ namespace WebSocketSharp {
#region Public Methods #region Public Methods
/// <summary> /// <summary>
/// Outputs the specified <paramref name="message"/> as a log with the <see cref="LogLevel.DEBUG"/>. /// Outputs the specified <see cref="string"/> as a log with the <see cref="LogLevel.DEBUG"/>.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// This method does not output <paramref name="message"/> as a log /// If the current logging level is greater than the <see cref="LogLevel.DEBUG"/>,
/// if the current logging level is greater than the <see cref="LogLevel.DEBUG"/>. /// this method does not output <paramref name="message"/> as a log.
/// </remarks> /// </remarks>
/// <param name="message"> /// <param name="message">
/// A <see cref="string"/> that contains a message to output as a log. /// A <see cref="string"/> that contains a message to output as a log.
/// </param> /// </param>
public void Debug (string message) public void Debug (string message)
{ {
output (LogLevel.DEBUG, message); output (message, LogLevel.DEBUG);
} }
/// <summary> /// <summary>
/// Outputs the specified <paramref name="message"/> as a log with the <see cref="LogLevel.ERROR"/>. /// Outputs the specified <see cref="string"/> as a log with the <see cref="LogLevel.ERROR"/>.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// This method does not output <paramref name="message"/> as a log /// If the current logging level is greater than the <see cref="LogLevel.ERROR"/>,
/// if the current logging level is greater than the <see cref="LogLevel.ERROR"/>. /// this method does not output <paramref name="message"/> as a log.
/// </remarks> /// </remarks>
/// <param name="message"> /// <param name="message">
/// A <see cref="string"/> that contains a message to output as a log. /// A <see cref="string"/> that contains a message to output as a log.
/// </param> /// </param>
public void Error (string message) public void Error (string message)
{ {
output (LogLevel.ERROR, message); output (message, LogLevel.ERROR);
} }
/// <summary> /// <summary>
/// Outputs the specified <paramref name="message"/> as a log with the <see cref="LogLevel.FATAL"/>. /// Outputs the specified <see cref="string"/> as a log with the <see cref="LogLevel.FATAL"/>.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// This method does not output <paramref name="message"/> as a log /// If the current logging level is greater than the <see cref="LogLevel.FATAL"/>,
/// if the current logging level is greater than the <see cref="LogLevel.FATAL"/>. /// this method does not output <paramref name="message"/> as a log.
/// </remarks> /// </remarks>
/// <param name="message"> /// <param name="message">
/// A <see cref="string"/> that contains a message to output as a log. /// A <see cref="string"/> that contains a message to output as a log.
/// </param> /// </param>
public void Fatal (string message) public void Fatal (string message)
{ {
output (LogLevel.FATAL, message); output (message, LogLevel.FATAL);
} }
/// <summary> /// <summary>
/// Outputs the specified <paramref name="message"/> as a log with the <see cref="LogLevel.INFO"/>. /// Outputs the specified <see cref="string"/> as a log with the <see cref="LogLevel.INFO"/>.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// This method does not output <paramref name="message"/> as a log /// If the current logging level is greater than the <see cref="LogLevel.INFO"/>,
/// if the current logging level is greater than the <see cref="LogLevel.INFO"/>. /// this method does not output <paramref name="message"/> as a log.
/// </remarks> /// </remarks>
/// <param name="message"> /// <param name="message">
/// A <see cref="string"/> that contains a message to output as a log. /// A <see cref="string"/> that contains a message to output as a log.
/// </param> /// </param>
public void Info (string message) public void Info (string message)
{ {
output (LogLevel.INFO, message); output (message, LogLevel.INFO);
} }
/// <summary> /// <summary>
/// Replaces the current output action with the specified <paramref name="output"/> action. /// Replaces the current output action with the specified <paramref name="output"/> action.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// This method replaces the current output action with the default output action /// If <paramref name="output"/> is <see langword="null"/>,
/// if <paramref name="output"/> is <see langword="null"/>. /// this method replaces the current output action with the default output action.
/// </remarks> /// </remarks>
/// <param name="output"> /// <param name="output">
/// An <c>Action&lt;LogData, string&gt;</c> delegate that references the method(s) to set. /// An <c>Action&lt;LogData, string&gt;</c> delegate that references the method(s) to set.
/// A <see cref="string"/> parameter to pass to the method(s) is the value of <see cref="Logger.File"/> /// A <see cref="string"/> parameter to pass to the method(s) is the value of the <see cref="Logger.File"/>
/// if any. /// if any.
/// </param> /// </param>
public void SetOutput (Action<LogData, string> output) public void SetOutput (Action<LogData, string> output)
{ {
lock (_sync) lock (_sync)
{ {
if (output != null) _output = output != null ? output : defaultOutput;
_output = output; Warn ("The current output action has been replaced.");
else
_output = defaultOutput;
} }
} }
/// <summary> /// <summary>
/// Outputs the specified <paramref name="message"/> as a log with the <see cref="LogLevel.TRACE"/>. /// Outputs the specified <see cref="string"/> as a log with the <see cref="LogLevel.TRACE"/>.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// This method does not output <paramref name="message"/> as a log /// If the current logging level is greater than the <see cref="LogLevel.TRACE"/>,
/// if the current logging level is greater than the <see cref="LogLevel.TRACE"/>. /// this method does not output <paramref name="message"/> as a log.
/// </remarks> /// </remarks>
/// <param name="message"> /// <param name="message">
/// A <see cref="string"/> that contains a message to output as a log. /// A <see cref="string"/> that contains a message to output as a log.
/// </param> /// </param>
public void Trace (string message) public void Trace (string message)
{ {
output (LogLevel.TRACE, message); output (message, LogLevel.TRACE);
} }
/// <summary> /// <summary>
/// Outputs the specified <paramref name="message"/> as a log with the <see cref="LogLevel.WARN"/>. /// Outputs the specified <see cref="string"/> as a log with the <see cref="LogLevel.WARN"/>.
/// </summary> /// </summary>
/// <remarks> /// <remarks>
/// This method does not output <paramref name="message"/> as a log /// If the current logging level is greater than the <see cref="LogLevel.WARN"/>,
/// if the current logging level is greater than the <see cref="LogLevel.WARN"/>. /// this method does not output <paramref name="message"/> as a log.
/// </remarks> /// </remarks>
/// <param name="message"> /// <param name="message">
/// A <see cref="string"/> that contains a message to output as a log. /// A <see cref="string"/> that contains a message to output as a log.
/// </param> /// </param>
public void Warn (string message) public void Warn (string message)
{ {
output (LogLevel.WARN, message); output (message, LogLevel.WARN);
} }
#endregion #endregion