From 80f07310206815a48fa0cd0b97ce8766a6d89b10 Mon Sep 17 00:00:00 2001 From: Chris Tacke Date: Wed, 18 Oct 2023 09:25:20 -0500 Subject: [PATCH] warnings as errors --- Source/Meadow.Logging/lib/Logger.cs | 308 +++++++++--------- .../Meadow.Logging/lib/Meadow.Logging.csproj | 10 +- 2 files changed, 164 insertions(+), 154 deletions(-) diff --git a/Source/Meadow.Logging/lib/Logger.cs b/Source/Meadow.Logging/lib/Logger.cs index dcba453..de6d4f9 100644 --- a/Source/Meadow.Logging/lib/Logger.cs +++ b/Source/Meadow.Logging/lib/Logger.cs @@ -1,184 +1,192 @@ using System; -namespace Meadow.Logging +namespace Meadow.Logging; + +/// +/// Class encapsulating logging providers and functions +/// +public class Logger { - /// - /// Class encapsulating logging providers and functions - /// - public class Logger - { - private LogProviderCollection _providers = new LogProviderCollection(); + private LogProviderCollection _providers = new LogProviderCollection(); - private readonly int _startupTicks; + private readonly int _startupTicks; - /// - /// Gets or sets a value indicating whether to show ticks in log messages - /// - public bool ShowTicks { get; set; } = false; + /// + /// Gets or sets a value indicating whether to show ticks in log messages + /// + public bool ShowTicks { get; set; } = false; - /// - /// Gets or sets the current log level - /// - public LogLevel LogLevel { get; set; } = LogLevel.Error; + /// + /// Gets or sets the current log level + /// + public LogLevel LogLevel { get; set; } = LogLevel.Error; - /// - /// Creates a Logger instance - /// - /// A collection of providers to add to the Logger - public Logger(params ILogProvider[] providers) + /// + /// Creates a Logger instance + /// + /// A collection of providers to add to the Logger + public Logger(params ILogProvider[] providers) + { + foreach (var p in providers) { - foreach (var p in providers) - { - AddProvider(p); - } + AddProvider(p); } + } - /// - /// Creates a Logger instance - /// - /// A provider to add to the Logger - public Logger(ILogProvider provider) - { - AddProvider(provider); - } + /// + /// Creates a Logger instance + /// + /// A provider to add to the Logger + public Logger(ILogProvider provider) + { + AddProvider(provider); + } - /// - /// Creates a Logger instance - /// - public Logger() - { - _startupTicks = Environment.TickCount; - } + /// + /// Creates a Logger instance + /// + public Logger() + { + _startupTicks = Environment.TickCount; + } - /// - /// Adds an ILogProvider to the providers collection - /// - /// - public void AddProvider(ILogProvider provider) + /// + /// Adds an ILogProvider to the providers collection + /// + /// + public void AddProvider(ILogProvider provider) + { + lock (_providers) { - lock (_providers) - { - _providers.Add(provider); - } + _providers.Add(provider); } + } - /// - /// Sends a Trace-level message to all ILogProviders - /// - /// The message to send to Providers - public void Trace(string message) - { - Log(LogLevel.Trace, message); - } + /// + /// Sends a Trace-level message to all ILogProviders + /// + /// The message to send to Providers + public void Trace(string message) + { + Log(LogLevel.Trace, message); + } - /// - /// Conditionally sends a Trace-level message to all ILogProviders - /// - /// The message will be sent to Providers only when this is true - /// The message to send to Providers - public void TraceIf(bool condition, string message) - { - if (condition) Log(LogLevel.Trace, message); - } + /// + /// Conditionally sends a Trace-level message to all ILogProviders + /// + /// The message will be sent to Providers only when this is true + /// The message to send to Providers + public void TraceIf(bool condition, string message) + { + if (condition) Log(LogLevel.Trace, message); + } - /// - /// Sends a Debug-level message to all ILogProviders - /// - /// The message to send to Providers - public void Debug(string message) - { - Log(LogLevel.Debug, message); - } + /// + /// Sends a Debug-level message to all ILogProviders + /// + /// The message to send to Providers + public void Debug(string message) + { + Log(LogLevel.Debug, message); + } - /// - /// Conditionally sends a Debug-level message to all ILogProviders - /// - /// The message will be sent to Providers only when this is true - /// The message to send to Providers - public void DebugIf(bool condition, string message) - { - if (condition) Log(LogLevel.Debug, message); - } + /// + /// Conditionally sends a Debug-level message to all ILogProviders + /// + /// The message will be sent to Providers only when this is true + /// The message to send to Providers + public void DebugIf(bool condition, string message) + { + if (condition) Log(LogLevel.Debug, message); + } - /// - /// Sends an Info-level message to all ILogProviders - /// - /// The message to send to Providers - public void Info(string message) - { - Log(LogLevel.Information, message); - } + /// + /// Sends an Info-level message to all ILogProviders + /// + /// The message to send to Providers + public void Info(string message) + { + Log(LogLevel.Information, message); + } - /// - /// Conditionally sends a Info-level message to all ILogProviders - /// - /// The message will be sent to Providers only when this is true - /// The message to send to Providers - public void InfoIf(bool condition, string message) - { - if (condition) Log(LogLevel.Information, message); - } + /// + /// Conditionally sends a Info-level message to all ILogProviders + /// + /// The message will be sent to Providers only when this is true + /// The message to send to Providers + public void InfoIf(bool condition, string message) + { + if (condition) Log(LogLevel.Information, message); + } - /// - /// Sends a Warn-level message to all ILogProviders - /// - /// The message to send to Providers - public void Warn(string message) - { - Log(LogLevel.Warning, message); - } + /// + /// Sends a Warn-level message to all ILogProviders + /// + /// The message to send to Providers + public void Warn(string message) + { + Log(LogLevel.Warning, message); + } - /// - /// Conditionally sends a Warn-level message to all ILogProviders - /// - /// The message will be sent to Providers only when this is true - /// The message to send to Providers - public void WarnIf(bool condition, string message) - { - if (condition) Log(LogLevel.Warning, message); - } + /// + /// Conditionally sends a Warn-level message to all ILogProviders + /// + /// The message will be sent to Providers only when this is true + /// The message to send to Providers + public void WarnIf(bool condition, string message) + { + if (condition) Log(LogLevel.Warning, message); + } - /// - /// Sends a Error-level message to all ILogProviders - /// - /// The message to send to Providers - public void Error(string message) - { - Log(LogLevel.Error, message); - } + /// + /// Sends a Error-level message to all ILogProviders + /// + /// The exception to translate and send to Providers + public void Error(Exception exception) + { + Log(LogLevel.Error, exception.ToString()); + } - /// - /// Conditionally sends a Error-level message to all ILogProviders - /// - /// The message will be sent to Providers only when this is true - /// The message to send to Providers - public void ErrorIf(bool condition, string message) - { - if (condition) Log(LogLevel.Error, message); - } + /// + /// Sends a Error-level message to all ILogProviders + /// + /// The message to send to Providers + public void Error(string message) + { + Log(LogLevel.Error, message); + } - private void Log(LogLevel level, string message) - { - if (LogLevel > level) return; + /// + /// Conditionally sends a Error-level message to all ILogProviders + /// + /// The message will be sent to Providers only when this is true + /// The message to send to Providers + public void ErrorIf(bool condition, string message) + { + if (condition) Log(LogLevel.Error, message); + } - TimeSpan? now = null; + private void Log(LogLevel level, string message) + { + if (LogLevel > level) return; + + TimeSpan? now = null; - lock (_providers) + lock (_providers) + { + if (ShowTicks) { - if (ShowTicks) - { - now = TimeSpan.FromMilliseconds(Environment.TickCount - _startupTicks); - } + now = TimeSpan.FromMilliseconds(Environment.TickCount - _startupTicks); + } - foreach (var p in _providers) + foreach (var p in _providers) + { + if (now != null) { - if (now != null) - { - message = $"[+{now:h\\:m\\:ss\\.FFF}] {message}"; - } - - p.Log(level, message); + message = $"[+{now:h\\:m\\:ss\\.FFF}] {message}"; } + + p.Log(level, message); } } } diff --git a/Source/Meadow.Logging/lib/Meadow.Logging.csproj b/Source/Meadow.Logging/lib/Meadow.Logging.csproj index e5f986f..1805d6f 100644 --- a/Source/Meadow.Logging/lib/Meadow.Logging.csproj +++ b/Source/Meadow.Logging/lib/Meadow.Logging.csproj @@ -2,21 +2,23 @@ netstandard2.1 - git - icon.png + git + icon.png 0.29.0 Meadow.Logging Wilderness Labs, Inc https://github.com/WildernessLabs/Meadow.Logging Meadow.Logging - icon.png - https://github.com/WildernessLabs/Meadow.Logging + icon.png + https://github.com/WildernessLabs/Meadow.Logging Meadow Wilderness Labs ultra-lightweight logging library true true Meadow enable + True + 10