Skip to content

Commit

Permalink
Merge pull request #29 from WildernessLabs/v1.4.0.3
Browse files Browse the repository at this point in the history
V1.4.0.3
  • Loading branch information
jorgedevs authored Nov 11, 2023
2 parents a44c07c + a955516 commit e387a7a
Show file tree
Hide file tree
Showing 2 changed files with 164 additions and 154 deletions.
308 changes: 158 additions & 150 deletions Source/Meadow.Logging/lib/Logger.cs
Original file line number Diff line number Diff line change
@@ -1,184 +1,192 @@
using System;

namespace Meadow.Logging
namespace Meadow.Logging;

/// <summary>
/// Class encapsulating logging providers and functions
/// </summary>
public class Logger
{
/// <summary>
/// Class encapsulating logging providers and functions
/// </summary>
public class Logger
{
private LogProviderCollection _providers = new LogProviderCollection();
private LogProviderCollection _providers = new LogProviderCollection();

private readonly int _startupTicks;
private readonly int _startupTicks;

/// <summary>
/// Gets or sets a value indicating whether to show ticks in log messages
/// </summary>
public bool ShowTicks { get; set; } = false;
/// <summary>
/// Gets or sets a value indicating whether to show ticks in log messages
/// </summary>
public bool ShowTicks { get; set; } = false;

/// <summary>
/// Gets or sets the current log level
/// </summary>
public LogLevel LogLevel { get; set; } = LogLevel.Error;
/// <summary>
/// Gets or sets the current log level
/// </summary>
public LogLevel LogLevel { get; set; } = LogLevel.Error;

/// <summary>
/// Creates a Logger instance
/// </summary>
/// <param name="providers">A collection of providers to add to the Logger</param>
public Logger(params ILogProvider[] providers)
/// <summary>
/// Creates a Logger instance
/// </summary>
/// <param name="providers">A collection of providers to add to the Logger</param>
public Logger(params ILogProvider[] providers)
{
foreach (var p in providers)
{
foreach (var p in providers)
{
AddProvider(p);
}
AddProvider(p);
}
}

/// <summary>
/// Creates a Logger instance
/// </summary>
/// <param name="provider">A provider to add to the Logger</param>
public Logger(ILogProvider provider)
{
AddProvider(provider);
}
/// <summary>
/// Creates a Logger instance
/// </summary>
/// <param name="provider">A provider to add to the Logger</param>
public Logger(ILogProvider provider)
{
AddProvider(provider);
}

/// <summary>
/// Creates a Logger instance
/// </summary>
public Logger()
{
_startupTicks = Environment.TickCount;
}
/// <summary>
/// Creates a Logger instance
/// </summary>
public Logger()
{
_startupTicks = Environment.TickCount;
}

/// <summary>
/// Adds an ILogProvider to the providers collection
/// </summary>
/// <param name="provider"></param>
public void AddProvider(ILogProvider provider)
/// <summary>
/// Adds an ILogProvider to the providers collection
/// </summary>
/// <param name="provider"></param>
public void AddProvider(ILogProvider provider)
{
lock (_providers)
{
lock (_providers)
{
_providers.Add(provider);
}
_providers.Add(provider);
}
}

/// <summary>
/// Sends a Trace-level message to all ILogProviders
/// </summary>
/// <param name="message">The message to send to Providers</param>
public void Trace(string message)
{
Log(LogLevel.Trace, message);
}
/// <summary>
/// Sends a Trace-level message to all ILogProviders
/// </summary>
/// <param name="message">The message to send to Providers</param>
public void Trace(string message)
{
Log(LogLevel.Trace, message);
}

/// <summary>
/// Conditionally sends a Trace-level message to all ILogProviders
/// </summary>
/// <param name="condition">The message will be sent to Providers only when this is true</param>
/// <param name="message">The message to send to Providers</param>
public void TraceIf(bool condition, string message)
{
if (condition) Log(LogLevel.Trace, message);
}
/// <summary>
/// Conditionally sends a Trace-level message to all ILogProviders
/// </summary>
/// <param name="condition">The message will be sent to Providers only when this is true</param>
/// <param name="message">The message to send to Providers</param>
public void TraceIf(bool condition, string message)
{
if (condition) Log(LogLevel.Trace, message);
}

/// <summary>
/// Sends a Debug-level message to all ILogProviders
/// </summary>
/// <param name="message">The message to send to Providers</param>
public void Debug(string message)
{
Log(LogLevel.Debug, message);
}
/// <summary>
/// Sends a Debug-level message to all ILogProviders
/// </summary>
/// <param name="message">The message to send to Providers</param>
public void Debug(string message)
{
Log(LogLevel.Debug, message);
}

/// <summary>
/// Conditionally sends a Debug-level message to all ILogProviders
/// </summary>
/// <param name="condition">The message will be sent to Providers only when this is true</param>
/// <param name="message">The message to send to Providers</param>
public void DebugIf(bool condition, string message)
{
if (condition) Log(LogLevel.Debug, message);
}
/// <summary>
/// Conditionally sends a Debug-level message to all ILogProviders
/// </summary>
/// <param name="condition">The message will be sent to Providers only when this is true</param>
/// <param name="message">The message to send to Providers</param>
public void DebugIf(bool condition, string message)
{
if (condition) Log(LogLevel.Debug, message);
}

/// <summary>
/// Sends an Info-level message to all ILogProviders
/// </summary>
/// <param name="message">The message to send to Providers</param>
public void Info(string message)
{
Log(LogLevel.Information, message);
}
/// <summary>
/// Sends an Info-level message to all ILogProviders
/// </summary>
/// <param name="message">The message to send to Providers</param>
public void Info(string message)
{
Log(LogLevel.Information, message);
}

/// <summary>
/// Conditionally sends a Info-level message to all ILogProviders
/// </summary>
/// <param name="condition">The message will be sent to Providers only when this is true</param>
/// <param name="message">The message to send to Providers</param>
public void InfoIf(bool condition, string message)
{
if (condition) Log(LogLevel.Information, message);
}
/// <summary>
/// Conditionally sends a Info-level message to all ILogProviders
/// </summary>
/// <param name="condition">The message will be sent to Providers only when this is true</param>
/// <param name="message">The message to send to Providers</param>
public void InfoIf(bool condition, string message)
{
if (condition) Log(LogLevel.Information, message);
}

/// <summary>
/// Sends a Warn-level message to all ILogProviders
/// </summary>
/// <param name="message">The message to send to Providers</param>
public void Warn(string message)
{
Log(LogLevel.Warning, message);
}
/// <summary>
/// Sends a Warn-level message to all ILogProviders
/// </summary>
/// <param name="message">The message to send to Providers</param>
public void Warn(string message)
{
Log(LogLevel.Warning, message);
}

/// <summary>
/// Conditionally sends a Warn-level message to all ILogProviders
/// </summary>
/// <param name="condition">The message will be sent to Providers only when this is true</param>
/// <param name="message">The message to send to Providers</param>
public void WarnIf(bool condition, string message)
{
if (condition) Log(LogLevel.Warning, message);
}
/// <summary>
/// Conditionally sends a Warn-level message to all ILogProviders
/// </summary>
/// <param name="condition">The message will be sent to Providers only when this is true</param>
/// <param name="message">The message to send to Providers</param>
public void WarnIf(bool condition, string message)
{
if (condition) Log(LogLevel.Warning, message);
}

/// <summary>
/// Sends a Error-level message to all ILogProviders
/// </summary>
/// <param name="message">The message to send to Providers</param>
public void Error(string message)
{
Log(LogLevel.Error, message);
}
/// <summary>
/// Sends a Error-level message to all ILogProviders
/// </summary>
/// <param name="exception">The exception to translate and send to Providers</param>
public void Error(Exception exception)
{
Log(LogLevel.Error, exception.ToString());
}

/// <summary>
/// Conditionally sends a Error-level message to all ILogProviders
/// </summary>
/// <param name="condition">The message will be sent to Providers only when this is true</param>
/// <param name="message">The message to send to Providers</param>
public void ErrorIf(bool condition, string message)
{
if (condition) Log(LogLevel.Error, message);
}
/// <summary>
/// Sends a Error-level message to all ILogProviders
/// </summary>
/// <param name="message">The message to send to Providers</param>
public void Error(string message)
{
Log(LogLevel.Error, message);
}

private void Log(LogLevel level, string message)
{
if (LogLevel > level) return;
/// <summary>
/// Conditionally sends a Error-level message to all ILogProviders
/// </summary>
/// <param name="condition">The message will be sent to Providers only when this is true</param>
/// <param name="message">The message to send to Providers</param>
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);
}
}
}
Expand Down
10 changes: 6 additions & 4 deletions Source/Meadow.Logging/lib/Meadow.Logging.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,23 @@

<PropertyGroup>
<TargetFramework>netstandard2.1</TargetFramework>
<RepositoryType>git</RepositoryType>
<PackageIcon>icon.png</PackageIcon>
<RepositoryType>git</RepositoryType>
<PackageIcon>icon.png</PackageIcon>
<Version>0.29.0</Version>
<AssemblyName>Meadow.Logging</AssemblyName>
<Company>Wilderness Labs, Inc</Company>
<PackageProjectUrl>https://github.com/WildernessLabs/Meadow.Logging</PackageProjectUrl>
<PackageId>Meadow.Logging</PackageId>
<PackageIcon>icon.png</PackageIcon>
<RepositoryUrl>https://github.com/WildernessLabs/Meadow.Logging</RepositoryUrl>
<PackageIcon>icon.png</PackageIcon>
<RepositoryUrl>https://github.com/WildernessLabs/Meadow.Logging</RepositoryUrl>
<PackageTags>Meadow</PackageTags>
<Description>Wilderness Labs ultra-lightweight logging library</Description>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<RootNamespace>Meadow</RootNamespace>
<Nullable>enable</Nullable>
<TreatWarningsAsErrors>True</TreatWarningsAsErrors>
<LangVersion>10</LangVersion>
</PropertyGroup>

<ItemGroup>
Expand Down

0 comments on commit e387a7a

Please sign in to comment.