From b88bde73c54dfe44a42267e3dc97a4cf0e413e0c Mon Sep 17 00:00:00 2001 From: Andreas Danek Date: Fri, 9 Oct 2020 12:33:57 +0200 Subject: [PATCH] Added log --- .../Log/CommonLoggingManager.cs | 238 ++++++++++++++++++ EnergySoultions.CoAP/Log/ConsoleLogManager.cs | 30 +++ EnergySoultions.CoAP/Log/ILogManager.cs | 30 +++ EnergySoultions.CoAP/Log/ILogger.cs | 82 ++++++ EnergySoultions.CoAP/Log/LogManager.cs | 111 ++++++++ EnergySoultions.CoAP/Log/NopLogManager.cs | 122 +++++++++ EnergySoultions.CoAP/Log/TextWriterLogger.cs | 178 +++++++++++++ 7 files changed, 791 insertions(+) create mode 100644 EnergySoultions.CoAP/Log/CommonLoggingManager.cs create mode 100644 EnergySoultions.CoAP/Log/ConsoleLogManager.cs create mode 100644 EnergySoultions.CoAP/Log/ILogManager.cs create mode 100644 EnergySoultions.CoAP/Log/ILogger.cs create mode 100644 EnergySoultions.CoAP/Log/LogManager.cs create mode 100644 EnergySoultions.CoAP/Log/NopLogManager.cs create mode 100644 EnergySoultions.CoAP/Log/TextWriterLogger.cs diff --git a/EnergySoultions.CoAP/Log/CommonLoggingManager.cs b/EnergySoultions.CoAP/Log/CommonLoggingManager.cs new file mode 100644 index 0000000..3bacf98 --- /dev/null +++ b/EnergySoultions.CoAP/Log/CommonLoggingManager.cs @@ -0,0 +1,238 @@ +/* + * Copyright (c) 2011-2014, Longxiang He , + * SmeshLink Technology Co. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY. + * + * This file is part of the CoAP.NET, a CoAP framework in C#. + * Please see README for more information. + */ + +using System; + +namespace WdIoT.Platform.Coap.Log +{ + class CommonLoggingManager : ILogManager + { + public ILogger GetLogger(Type type) + { + return new CommonLogging(global::Common.Logging.LogManager.GetLogger(type)); + } + + public ILogger GetLogger(String name) + { + return new CommonLogging(global::Common.Logging.LogManager.GetLogger(name)); + } + + class CommonLogging : ILogger + { + private readonly global::Common.Logging.ILog _log; + + public CommonLogging(global::Common.Logging.ILog log) + { + _log = log; + } + + public void Debug(Object message, Exception exception) + { + _log.Debug(message, exception); + } + + public void Debug(Object message) + { + _log.Debug(message); + } + + public void DebugFormat(IFormatProvider provider, String format, params Object[] args) + { + _log.DebugFormat(provider, format, args); + } + + public void DebugFormat(String format, Object arg0, Object arg1, Object arg2) + { + _log.DebugFormat(format, arg0, arg1, arg2); + } + + public void DebugFormat(String format, Object arg0, Object arg1) + { + _log.DebugFormat(format, arg0, arg1); + } + + public void DebugFormat(String format, Object arg0) + { + _log.DebugFormat(format, arg0); + } + + public void DebugFormat(String format, params Object[] args) + { + _log.DebugFormat(format, args); + } + + public void Error(Object message, Exception exception) + { + _log.Error(message, exception); + } + + public void Error(Object message) + { + _log.Error(message); + } + + public void ErrorFormat(IFormatProvider provider, String format, params Object[] args) + { + _log.ErrorFormat(provider, format, args); + } + + public void ErrorFormat(String format, Object arg0, Object arg1, Object arg2) + { + _log.ErrorFormat(format, arg0, arg1, arg2); + } + + public void ErrorFormat(String format, Object arg0, Object arg1) + { + _log.ErrorFormat(format, arg0, arg1); + } + + public void ErrorFormat(String format, Object arg0) + { + _log.ErrorFormat(format, arg0); + } + + public void ErrorFormat(String format, params Object[] args) + { + _log.ErrorFormat(format, args); + } + + public void Fatal(Object message, Exception exception) + { + _log.Fatal(message, exception); + } + + public void Fatal(Object message) + { + _log.Fatal(message); + } + + public void FatalFormat(IFormatProvider provider, String format, params Object[] args) + { + _log.FatalFormat(provider, format, args); + } + + public void FatalFormat(String format, Object arg0, Object arg1, Object arg2) + { + _log.FatalFormat(format, arg0, arg1, arg2); + } + + public void FatalFormat(String format, Object arg0, Object arg1) + { + _log.FatalFormat(format, arg0, arg1); + } + + public void FatalFormat(String format, Object arg0) + { + _log.FatalFormat(format, arg0); + } + + public void FatalFormat(String format, params Object[] args) + { + _log.FatalFormat(format, args); + } + + public void Info(Object message, Exception exception) + { + _log.Info(message, exception); + } + + public void Info(Object message) + { + _log.Info(message); + } + + public void InfoFormat(IFormatProvider provider, String format, params Object[] args) + { + _log.InfoFormat(provider, format, args); + } + + public void InfoFormat(String format, Object arg0, Object arg1, Object arg2) + { + _log.InfoFormat(format, arg0, arg1, arg2); + } + + public void InfoFormat(String format, Object arg0, Object arg1) + { + _log.InfoFormat(format, arg0, arg1); + } + + public void InfoFormat(String format, Object arg0) + { + _log.InfoFormat(format, arg0); + } + + public void InfoFormat(String format, params Object[] args) + { + _log.InfoFormat(format, args); + } + + public Boolean IsDebugEnabled + { + get { return LogLevel.Debug >= LogManager.Level && _log.IsDebugEnabled; } + } + + public Boolean IsErrorEnabled + { + get { return LogLevel.Error >= LogManager.Level && _log.IsErrorEnabled; } + } + + public Boolean IsFatalEnabled + { + get { return LogLevel.Fatal >= LogManager.Level && _log.IsFatalEnabled; } + } + + public Boolean IsInfoEnabled + { + get { return LogLevel.Info >= LogManager.Level && _log.IsInfoEnabled; } + } + + public Boolean IsWarnEnabled + { + get { return LogLevel.Warning >= LogManager.Level && _log.IsWarnEnabled; } + } + + public void Warn(Object message, Exception exception) + { + _log.Warn(message, exception); + } + + public void Warn(Object message) + { + _log.Warn(message); + } + + public void WarnFormat(IFormatProvider provider, String format, params Object[] args) + { + _log.WarnFormat(provider, format, args); + } + + public void WarnFormat(String format, Object arg0, Object arg1, Object arg2) + { + _log.WarnFormat(format, arg0, arg1, arg2); + } + + public void WarnFormat(String format, Object arg0, Object arg1) + { + _log.WarnFormat(format, arg0, arg1); + } + + public void WarnFormat(String format, Object arg0) + { + _log.WarnFormat(format, arg0); + } + + public void WarnFormat(String format, params Object[] args) + { + _log.WarnFormat(format, args); + } + } + } +} diff --git a/EnergySoultions.CoAP/Log/ConsoleLogManager.cs b/EnergySoultions.CoAP/Log/ConsoleLogManager.cs new file mode 100644 index 0000000..ed4374a --- /dev/null +++ b/EnergySoultions.CoAP/Log/ConsoleLogManager.cs @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2011-2014, Longxiang He , + * SmeshLink Technology Co. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY. + * + * This file is part of the CoAP.NET, a CoAP framework in C#. + * Please see README for more information. + */ + +using System; + +namespace WdIoT.Platform.Coap.Log +{ + class ConsoleLogManager : ILogManager + { + static readonly ILogger _logger = new TextWriterLogger(Console.Out); + + public ILogger GetLogger(Type type) + { + return _logger; + } + + public ILogger GetLogger(string name) + { + return _logger; + } + } +} diff --git a/EnergySoultions.CoAP/Log/ILogManager.cs b/EnergySoultions.CoAP/Log/ILogManager.cs new file mode 100644 index 0000000..32aff66 --- /dev/null +++ b/EnergySoultions.CoAP/Log/ILogManager.cs @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2011-2014, Longxiang He , + * SmeshLink Technology Co. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY. + * + * This file is part of the CoAP.NET, a CoAP framework in C#. + * Please see README for more information. + */ + +using System; + +namespace WdIoT.Platform.Coap.Log +{ + /// + /// Provides methods to acquire . + /// + public interface ILogManager + { + /// + /// Gets a logger of the given type. + /// + ILogger GetLogger(Type type); + /// + /// Gets a named logger. + /// + ILogger GetLogger(String name); + } +} diff --git a/EnergySoultions.CoAP/Log/ILogger.cs b/EnergySoultions.CoAP/Log/ILogger.cs new file mode 100644 index 0000000..d3d8213 --- /dev/null +++ b/EnergySoultions.CoAP/Log/ILogger.cs @@ -0,0 +1,82 @@ +/* + * Copyright (c) 2011-2012, Longxiang He , + * SmeshLink Technology Co. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY. + * + * This file is part of the CoAP.NET, a CoAP framework in C#. + * Please see README for more information. + */ + +using System; + +namespace WdIoT.Platform.Coap.Log +{ + /// + /// Provides methods to log messages. + /// + public interface ILogger + { + /// + /// Is debug enabled? + /// + Boolean IsDebugEnabled { get; } + /// + /// Is error enabled? + /// + Boolean IsErrorEnabled { get; } + /// + /// Is fatal enabled? + /// + Boolean IsFatalEnabled { get; } + /// + /// Is info enabled? + /// + Boolean IsInfoEnabled { get; } + /// + /// Is warning enabled? + /// + Boolean IsWarnEnabled { get; } + /// + /// Logs a debug message. + /// + void Debug(Object message); + /// + /// Logs a debug message. + /// + void Debug(Object message, Exception exception); + /// + /// Logs an error message. + /// + void Error(Object message); + /// + /// Logs an error message. + /// + void Error(Object message, Exception exception); + /// + /// Logs a fatal message. + /// + void Fatal(Object message); + /// + /// Logs a fatal message. + /// + void Fatal(Object message, Exception exception); + /// + /// Logs an info message. + /// + void Info(Object message); + /// + /// Logs an info message. + /// + void Info(Object message, Exception exception); + /// + /// Logs a warning message. + /// + void Warn(Object message); + /// + /// Logs a warning message. + /// + void Warn(Object message, Exception exception); + } +} diff --git a/EnergySoultions.CoAP/Log/LogManager.cs b/EnergySoultions.CoAP/Log/LogManager.cs new file mode 100644 index 0000000..b1da710 --- /dev/null +++ b/EnergySoultions.CoAP/Log/LogManager.cs @@ -0,0 +1,111 @@ +/* + * Copyright (c) 2011-2014, Longxiang He , + * SmeshLink Technology Co. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY. + * + * This file is part of the CoAP.NET, a CoAP framework in C#. + * Please see README for more information. + */ + +using System; + +namespace WdIoT.Platform.Coap.Log +{ + /// + /// Log manager. + /// + public static class LogManager + { + static LogLevel _level = LogLevel.All; + static ILogManager _manager; + + static LogManager() + { + Type test; + try + { + test = Type.GetType("Common.Logging.LogManager, Common.Logging"); + } + catch + { + test = null; + } + + if (test == null) + _manager = new ConsoleLogManager(); + else + _manager = new CommonLoggingManager(); + } + + /// + /// Gets or sets the global log level. + /// + public static LogLevel Level + { + get { return _level; } + set { _level = value; } + } + + /// + /// Gets or sets the to provide loggers. + /// + public static ILogManager Instance + { + get { return _manager; } + set { _manager = value ?? NopLogManager.Instance; } + } + + /// + /// Gets a logger for the given type. + /// + public static ILogger GetLogger(Type type) + { + return _manager.GetLogger(type); + } + + /// + /// Gets a logger for the given type name. + /// + public static ILogger GetLogger(String name) + { + return _manager.GetLogger(name); + } + } + + /// + /// Log levels. + /// + public enum LogLevel + { + /// + /// All logs. + /// + All, + /// + /// Debugs and above. + /// + Debug, + /// + /// Infos and above. + /// + Info, + /// + /// Warnings and above. + /// + Warning, + /// + /// Errors and above. + /// + Error, + /// + /// Fatals only. + /// + Fatal, + /// + /// No logs. + /// + None + } +} diff --git a/EnergySoultions.CoAP/Log/NopLogManager.cs b/EnergySoultions.CoAP/Log/NopLogManager.cs new file mode 100644 index 0000000..6c42cf2 --- /dev/null +++ b/EnergySoultions.CoAP/Log/NopLogManager.cs @@ -0,0 +1,122 @@ +/* + * Copyright (c) 2011-2014, Longxiang He , + * SmeshLink Technology Co. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY. + * + * This file is part of the CoAP.NET, a CoAP framework in C#. + * Please see README for more information. + */ + + +using System; + +namespace WdIoT.Platform.Coap.Log +{ + /// + /// A which always returns the unique instance of + /// a direct NOP (no operation) logger. + /// + public sealed class NopLogManager : ILogManager + { + /// + /// The singleton instance. + /// + public static readonly NopLogManager Instance = new NopLogManager(); + private static readonly NopLogger NOP = new NopLogger(); + + private NopLogManager() + { } + + /// + public ILogger GetLogger(Type type) + { + return NOP; + } + + /// + public ILogger GetLogger(String name) + { + return NOP; + } + + class NopLogger : ILogger + { + public Boolean IsDebugEnabled + { + get { return false; } + } + + public Boolean IsErrorEnabled + { + get { return false; } + } + + public Boolean IsFatalEnabled + { + get { return false; } + } + + public Boolean IsInfoEnabled + { + get { return false; } + } + + public Boolean IsWarnEnabled + { + get { return false; } + } + + public void Debug(Object message) + { + // NOP + } + + public void Debug(Object message, Exception exception) + { + // NOP + } + + public void Error(Object message) + { + // NOP + } + + public void Error(Object message, Exception exception) + { + // NOP + } + + public void Fatal(Object message) + { + // NOP + } + + public void Fatal(Object message, Exception exception) + { + // NOP + } + + public void Info(Object message) + { + // NOP + } + + public void Info(Object message, Exception exception) + { + // NOP + } + + public void Warn(Object message) + { + // NOP + } + + public void Warn(Object message, Exception exception) + { + // NOP + } + } + } +} diff --git a/EnergySoultions.CoAP/Log/TextWriterLogger.cs b/EnergySoultions.CoAP/Log/TextWriterLogger.cs new file mode 100644 index 0000000..a3acf8d --- /dev/null +++ b/EnergySoultions.CoAP/Log/TextWriterLogger.cs @@ -0,0 +1,178 @@ +/* + * Copyright (c) 2011-2014, Longxiang He , + * SmeshLink Technology Co. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY. + * + * This file is part of the CoAP.NET, a CoAP framework in C#. + * Please see README for more information. + */ + +using System; + +namespace WdIoT.Platform.Coap.Log +{ + /// + /// Logger that writes logs to a . + /// + public class TextWriterLogger : ILogger + { + private System.IO.TextWriter _writer; + + /// + /// Instantiates. + /// + public TextWriterLogger(System.IO.TextWriter writer) + { + _writer = writer; + } + + /// + public Boolean IsDebugEnabled + { + get { return LogLevel.Debug >= LogManager.Level; } + } + + /// + public Boolean IsInfoEnabled + { + get { return LogLevel.Info >= LogManager.Level; } + } + + /// + public Boolean IsErrorEnabled + { + get { return LogLevel.Error >= LogManager.Level; } + } + + /// + public Boolean IsFatalEnabled + { + get { return LogLevel.Fatal >= LogManager.Level; } + } + + /// + public Boolean IsWarnEnabled + { + get { return LogLevel.Warning >= LogManager.Level; } + } + + /// + public void Error(Object sender, String msg, params Object[] args) + { + String format = String.Format("ERROR - {0}\n", msg); + if (sender != null) + { + format = "[" + sender.GetType().Name + "] " + format; + } + + _writer.Write(format, args); + } + + /// + public void Warning(Object sender, String msg, params Object[] args) + { + String format = String.Format("WARNING - {0}\n", msg); + if (sender != null) + { + format = "[" + sender.GetType().Name + "] " + format; + } + + _writer.Write(format, args); + } + + /// + public void Info(Object sender, String msg, params Object[] args) + { + String format = String.Format("INFO - {0}\n", msg); + if (sender != null) + { + format = "[" + sender.GetType().Name + "] " + format; + } + + _writer.Write(format, args); + } + + /// + public void Debug(Object sender, String msg, params Object[] args) + { + String format = String.Format("DEBUG - {0}\n", msg); + if (sender != null) + { + format = "[" + sender.GetType().Name + "] " + format; + } + + _writer.Write(format, args); + } + + /// + public void Debug(Object message) + { + Log("DEBUG", message, null); + } + + /// + public void Debug(Object message, Exception exception) + { + Log("DEBUG", message, exception); + } + + /// + public void Error(Object message) + { + Log("Error", message, null); + } + + /// + public void Error(Object message, Exception exception) + { + Log("Error", message, exception); + } + + /// + public void Fatal(Object message) + { + Log("Fatal", message, null); + } + + /// + public void Fatal(Object message, Exception exception) + { + Log("Fatal", message, exception); + } + + /// + public void Info(Object message) + { + Log("Info", message, null); + } + + /// + public void Info(Object message, Exception exception) + { + Log("Info", message, exception); + } + + /// + public void Warn(Object message) + { + Log("Warn", message, null); + } + + /// + public void Warn(Object message, Exception exception) + { + Log("Warn", message, exception); + } + + private void Log(String level, Object message, Exception exception) + { + _writer.Write(level); + _writer.Write(" - "); + _writer.WriteLine(message); + if (exception != null) + _writer.WriteLine(exception); + } + } +}