websocket-sharp/websocket-sharp/Logger.cs

331 lines
9.9 KiB
C#
Raw Normal View History

2013-07-15 19:42:55 +08:00
#region License
/*
* Logger.cs
*
* The MIT License
*
2015-11-04 14:20:06 +08:00
* Copyright (c) 2013-2015 sta.blockhead
2014-03-03 16:22:29 +08:00
*
2013-07-15 19:42:55 +08:00
* 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.
2014-03-03 16:22:29 +08:00
*
2013-07-15 19:42:55 +08:00
* 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
{
2013-07-15 19:42:55 +08:00
/// <summary>
2014-03-03 16:22:29 +08:00
/// Provides a set of methods and properties for logging.
2013-07-15 19:42:55 +08:00
/// </summary>
/// <remarks>
/// <para>
2015-11-05 15:49:37 +08:00
/// If you output a log with lower than the value of the <see cref="Logger.Level"/> property,
2014-03-03 16:22:29 +08:00
/// it cannot be outputted.
2013-07-15 19:42:55 +08:00
/// </para>
/// <para>
2015-11-05 15:49:37 +08:00
/// The default output action writes a log to the standard output stream and the log file
/// if the <see cref="Logger.File"/> property has a valid path to it.
2013-07-15 19:42:55 +08:00
/// </para>
/// <para>
2015-11-05 15:49:37 +08:00
/// If you would like to use the custom output action, you should set
/// the <see cref="Logger.Output"/> property to any <c>Action&lt;LogData, string&gt;</c>
/// delegate.
2013-07-15 19:42:55 +08:00
/// </para>
/// </remarks>
public class Logger
{
2013-07-15 19:42:55 +08:00
#region Private Fields
private volatile string _file;
private volatile LogLevel _level;
private Action<LogData, string> _output;
private object _sync;
#endregion
#region Public Constructors
/// <summary>
/// Initializes a new instance of the <see cref="Logger"/> class.
/// </summary>
/// <remarks>
2014-03-03 16:22:29 +08:00
/// This constructor initializes the current logging level with <see cref="LogLevel.Error"/>.
2013-07-15 19:42:55 +08:00
/// </remarks>
public Logger ()
: this (LogLevel.Error, null, null)
2013-07-15 19:42:55 +08:00
{
}
/// <summary>
2015-11-05 15:49:37 +08:00
/// Initializes a new instance of the <see cref="Logger"/> class with
/// the specified logging <paramref name="level"/>.
2013-07-15 19:42:55 +08:00
/// </summary>
/// <param name="level">
2014-03-03 16:22:29 +08:00
/// One of the <see cref="LogLevel"/> enum values.
2013-07-15 19:42:55 +08:00
/// </param>
public Logger (LogLevel level)
: this (level, null, null)
2013-07-15 19:42:55 +08:00
{
}
/// <summary>
2015-11-05 15:49:37 +08:00
/// Initializes a new instance of the <see cref="Logger"/> class with
/// the specified logging <paramref name="level"/>, path to the log <paramref name="file"/>,
/// and <paramref name="output"/> action.
2013-07-15 19:42:55 +08:00
/// </summary>
/// <param name="level">
2014-03-03 16:22:29 +08:00
/// One of the <see cref="LogLevel"/> enum values.
2013-07-15 19:42:55 +08:00
/// </param>
/// <param name="file">
2014-03-03 16:22:29 +08:00
/// A <see cref="string"/> that represents the path to the log file.
2013-07-15 19:42:55 +08:00
/// </param>
/// <param name="output">
2015-11-05 15:49:37 +08:00
/// An <c>Action&lt;LogData, string&gt;</c> delegate that references the method(s) used to
/// output a log. A <see cref="string"/> parameter passed to this delegate is
/// <paramref name="file"/>.
2013-07-15 19:42:55 +08:00
/// </param>
public Logger (LogLevel level, string file, Action<LogData, string> output)
{
_level = level;
_file = file;
2014-03-03 16:22:29 +08:00
_output = output ?? defaultOutput;
2013-07-15 19:42:55 +08:00
_sync = new object ();
}
#endregion
#region Public Properties
/// <summary>
2014-08-08 19:57:09 +08:00
/// Gets or sets the current path to the log file.
2013-07-15 19:42:55 +08:00
/// </summary>
/// <value>
2014-08-08 19:57:09 +08:00
/// A <see cref="string"/> that represents the current path to the log file if any.
2013-07-15 19:42:55 +08:00
/// </value>
public string File {
get {
return _file;
}
set {
2014-03-03 16:22:29 +08:00
lock (_sync) {
2013-07-15 19:42:55 +08:00
_file = value;
2014-03-03 16:22:29 +08:00
Warn (
String.Format ("The current path to the log file has been changed to {0}.", _file));
2013-07-15 19:42:55 +08:00
}
}
}
/// <summary>
/// Gets or sets the current logging level.
/// </summary>
/// <remarks>
2014-03-03 16:22:29 +08:00
/// A log with lower than the value of this property cannot be outputted.
2013-07-15 19:42:55 +08:00
/// </remarks>
/// <value>
2015-11-04 14:20:06 +08:00
/// One of the <see cref="LogLevel"/> enum values, specifies the current logging level.
2013-07-15 19:42:55 +08:00
/// </value>
public LogLevel Level {
get {
return _level;
}
set {
2014-03-03 16:22:29 +08:00
lock (_sync) {
_level = value;
Warn (String.Format ("The current logging level has been changed to {0}.", _level));
}
}
}
/// <summary>
/// Gets or sets the current output action used to output a log.
/// </summary>
/// <value>
/// <para>
/// An <c>Action&lt;LogData, string&gt;</c> delegate that references the method(s) used to
/// output a log. A <see cref="string"/> parameter passed to this delegate is the value of
2015-11-05 15:49:37 +08:00
/// the <see cref="Logger.File"/> property.
2014-03-03 16:22:29 +08:00
/// </para>
/// <para>
/// If the value to set is <see langword="null"/>, the current output action is changed to
/// the default output action.
/// </para>
/// </value>
public Action<LogData, string> Output {
get {
return _output;
}
set {
lock (_sync) {
_output = value ?? defaultOutput;
Warn ("The current output action has been changed.");
}
2013-07-15 19:42:55 +08:00
}
}
#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)
2014-08-08 19:57:09 +08:00
writeToFile (log, path);
2013-07-15 19:42:55 +08:00
}
private void output (string message, LogLevel level)
2013-07-15 19:42:55 +08:00
{
2014-03-03 16:22:29 +08:00
lock (_sync) {
if (_level > level)
return;
2013-07-15 19:42:55 +08:00
LogData data = null;
try {
data = new LogData (level, new StackFrame (2, true), message);
2013-07-15 19:42:55 +08:00
_output (data, _file);
}
catch (Exception ex) {
data = new LogData (LogLevel.Fatal, new StackFrame (0, true), ex.Message);
2013-07-15 19:42:55 +08:00
Console.WriteLine (data.ToString ());
}
}
}
2014-08-08 19:57:09 +08:00
private static void writeToFile (string value, string path)
2013-07-15 19:42:55 +08:00
{
using (var writer = new StreamWriter (path, true))
2014-08-08 19:57:09 +08:00
using (var syncWriter = TextWriter.Synchronized (writer))
2013-07-15 19:42:55 +08:00
syncWriter.WriteLine (value);
}
#endregion
#region Public Methods
/// <summary>
2014-03-03 16:22:29 +08:00
/// Outputs <paramref name="message"/> as a log with <see cref="LogLevel.Debug"/>.
2013-07-15 19:42:55 +08:00
/// </summary>
/// <remarks>
2014-08-08 19:57:09 +08:00
/// If the current logging level is higher than <see cref="LogLevel.Debug"/>,
/// this method doesn't output <paramref name="message"/> as a log.
2013-07-15 19:42:55 +08:00
/// </remarks>
/// <param name="message">
2014-03-03 16:22:29 +08:00
/// A <see cref="string"/> that represents the message to output as a log.
2013-07-15 19:42:55 +08:00
/// </param>
public void Debug (string message)
{
2014-03-03 16:22:29 +08:00
if (_level > LogLevel.Debug)
return;
output (message, LogLevel.Debug);
2013-07-15 19:42:55 +08:00
}
/// <summary>
2014-03-03 16:22:29 +08:00
/// Outputs <paramref name="message"/> as a log with <see cref="LogLevel.Error"/>.
2013-07-15 19:42:55 +08:00
/// </summary>
/// <remarks>
2014-08-08 19:57:09 +08:00
/// If the current logging level is higher than <see cref="LogLevel.Error"/>,
/// this method doesn't output <paramref name="message"/> as a log.
2013-07-15 19:42:55 +08:00
/// </remarks>
/// <param name="message">
2014-03-03 16:22:29 +08:00
/// A <see cref="string"/> that represents the message to output as a log.
2013-07-15 19:42:55 +08:00
/// </param>
public void Error (string message)
{
2014-03-03 16:22:29 +08:00
if (_level > LogLevel.Error)
return;
output (message, LogLevel.Error);
2013-07-15 19:42:55 +08:00
}
/// <summary>
2014-03-03 16:22:29 +08:00
/// Outputs <paramref name="message"/> as a log with <see cref="LogLevel.Fatal"/>.
2013-07-15 19:42:55 +08:00
/// </summary>
/// <param name="message">
2014-03-03 16:22:29 +08:00
/// A <see cref="string"/> that represents the message to output as a log.
2013-07-15 19:42:55 +08:00
/// </param>
public void Fatal (string message)
{
output (message, LogLevel.Fatal);
2013-07-15 19:42:55 +08:00
}
/// <summary>
2014-03-03 16:22:29 +08:00
/// Outputs <paramref name="message"/> as a log with <see cref="LogLevel.Info"/>.
2013-07-15 19:42:55 +08:00
/// </summary>
/// <remarks>
2014-08-08 19:57:09 +08:00
/// If the current logging level is higher than <see cref="LogLevel.Info"/>,
/// this method doesn't output <paramref name="message"/> as a log.
2013-07-15 19:42:55 +08:00
/// </remarks>
/// <param name="message">
2014-03-03 16:22:29 +08:00
/// A <see cref="string"/> that represents the message to output as a log.
2013-07-15 19:42:55 +08:00
/// </param>
public void Info (string message)
{
2014-03-03 16:22:29 +08:00
if (_level > LogLevel.Info)
return;
2013-07-15 19:42:55 +08:00
2014-03-03 16:22:29 +08:00
output (message, LogLevel.Info);
2013-07-15 19:42:55 +08:00
}
/// <summary>
2014-03-03 16:22:29 +08:00
/// Outputs <paramref name="message"/> as a log with <see cref="LogLevel.Trace"/>.
2013-07-15 19:42:55 +08:00
/// </summary>
/// <remarks>
2014-08-08 19:57:09 +08:00
/// If the current logging level is higher than <see cref="LogLevel.Trace"/>,
/// this method doesn't output <paramref name="message"/> as a log.
2013-07-15 19:42:55 +08:00
/// </remarks>
/// <param name="message">
2014-03-03 16:22:29 +08:00
/// A <see cref="string"/> that represents the message to output as a log.
2013-07-15 19:42:55 +08:00
/// </param>
public void Trace (string message)
{
2014-03-03 16:22:29 +08:00
if (_level > LogLevel.Trace)
return;
output (message, LogLevel.Trace);
2013-07-15 19:42:55 +08:00
}
/// <summary>
2014-03-03 16:22:29 +08:00
/// Outputs <paramref name="message"/> as a log with <see cref="LogLevel.Warn"/>.
2013-07-15 19:42:55 +08:00
/// </summary>
/// <remarks>
2014-08-08 19:57:09 +08:00
/// If the current logging level is higher than <see cref="LogLevel.Warn"/>,
/// this method doesn't output <paramref name="message"/> as a log.
2013-07-15 19:42:55 +08:00
/// </remarks>
/// <param name="message">
2014-03-03 16:22:29 +08:00
/// A <see cref="string"/> that represents the message to output as a log.
2013-07-15 19:42:55 +08:00
/// </param>
public void Warn (string message)
{
2014-03-03 16:22:29 +08:00
if (_level > LogLevel.Warn)
return;
output (message, LogLevel.Warn);
2013-07-15 19:42:55 +08:00
}
#endregion
}
}