Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/develop' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
doingnz committed Jan 22, 2024
2 parents 61121f0 + 2c54e37 commit 25c1f84
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 58 deletions.
23 changes: 10 additions & 13 deletions Source/Meadow.Logging.LogProviders/Driver/CloudLogger.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
using Meadow.Cloud;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;
using Meadow.Cloud;

namespace Meadow.Logging;

Expand Down Expand Up @@ -33,7 +33,7 @@ public CloudLogger(LogLevel level = LogLevel.Information)
using FileStream fs = File.Create(LogFilePath);
fs.Close();
}

EventFilePath = Path.Combine(Resolver.Device.PlatformOS.FileSystem.DocumentsDirectory, "events.log");
if (!File.Exists(EventFilePath))
{
Expand All @@ -54,14 +54,11 @@ public CloudLogger(LogLevel level = LogLevel.Information)
/// Current minimum level for the CloudLogger
/// </summary>
public LogLevel MinLevel { get; protected set; }
static SemaphoreSlim semaphoreSlim = new SemaphoreSlim(1, 1);

/// <summary>
/// Send a log message to Meadow.Cloud.
/// </summary>
/// <param name="level">LogLevel</param>
/// <param name="message">Message of the log</param>
public async void Log(LogLevel level, string message)
private static SemaphoreSlim semaphoreSlim = new SemaphoreSlim(1, 1);

/// <inheritdoc/>
public async void Log(LogLevel level, string message, string? _)
{
if (level >= MinLevel)
{
Expand All @@ -71,7 +68,7 @@ public async void Log(LogLevel level, string message)
Message = message,
Timestamp = DateTime.UtcNow
};

await Send(LogFilePath, cloudLog, Resolver.MeadowCloudService.SendLog);
}
}
Expand All @@ -89,7 +86,7 @@ public async void LogException(Exception ex)
Message = ex.Message,
Timestamp = DateTime.UtcNow
};

await Send(LogFilePath, log, Resolver.MeadowCloudService.SendLog);
}

Expand All @@ -108,14 +105,14 @@ public async void LogEvent(int eventId, string description, Dictionary<string, o
Measurements = measurements,
Timestamp = DateTime.UtcNow
};

await Send(EventFilePath, cloudEvent, Resolver.MeadowCloudService.SendEvent);
}

private async Task Send<T>(string file, T item, Func<T, Task> sendFunc)
{
var serializeOptions = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase };

var connected = Resolver.Device.NetworkAdapters.Any(a => a.IsConnected);

if (connected)
Expand Down
2 changes: 1 addition & 1 deletion Source/Meadow.Logging.LogProviders/Driver/FileLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public FileLogger(LogLevel minimumLogLevel = LogLevel.Warning)
}

/// <inheritdoc/>
public void Log(LogLevel level, string message)
public void Log(LogLevel level, string message, string? _)
{
if (level != LogLevel.None && level >= MinimumLogLevel)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<Project Sdk="Meadow.Sdk/1.1.0">
<PropertyGroup>
<Version>1.8.0</Version>
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
<TargetFramework>netstandard2.1</TargetFramework>
<OutputType>Library</OutputType>
Expand All @@ -12,7 +13,6 @@
<PackageIcon>icon.png</PackageIcon>
<RepositoryUrl>https://github.com/WildernessLabs/Meadow.Foundation</RepositoryUrl>
<PackageTags>Meadow,Meadow.Foundation,ILogger,Logger,UdpLogger</PackageTags>
<Version>1.6.0.1</Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Description>Implementations of the Meadow ILogger interface</Description>
<LangVersion>10.0</LangVersion>
Expand Down
2 changes: 1 addition & 1 deletion Source/Meadow.Logging.LogProviders/Driver/UdpLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public UdpLogger(int port = 5100, char delimiter = '\t')
}

/// <inheritdoc/>
public void Log(LogLevel level, string message)
public void Log(LogLevel level, string message, string? _)
{
var payload = Encoding.UTF8.GetBytes($"{level}{_delimiter}{message}\n");
_client.Send(payload, payload.Length, _broadcast);
Expand Down
31 changes: 23 additions & 8 deletions Source/Meadow.Logging/lib/ConsoleLogProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,37 @@ public class ConsoleLogProvider : ILogProvider
/// <summary>
/// When true, the current log level will be prefixed to all logged messages
/// </summary>
public bool ShowLoglevel { get; set; } = false;
public bool ShowLogLevel { get; set; } = false;

/// <summary>
/// Called when the associated Logger has a message call
/// When true, the current message group will be prefixed to all logged messages
/// </summary>
/// <param name="level">The LogLevel for the message</param>
/// <param name="message">The message to log</param>
public void Log(LogLevel level, string message)
public bool ShowMessageGroup { get; set; } = false;

/// <inheritdoc/>
public void Log(LogLevel level, string message, string? messageGroup)
{
if (ShowLoglevel)
if (ShowLogLevel)
{
Console.WriteLine($"{level.ToString().ToUpper()}: {message}");
if (ShowMessageGroup)
{
Console.WriteLine($"({messageGroup}) {level.ToString().ToUpper()}: {message}");
}
else
{
Console.WriteLine($"{level.ToString().ToUpper()}: {message}");
}
}
else
{
Console.WriteLine($"{message}");
if (ShowMessageGroup)
{
Console.WriteLine($"({messageGroup}) {message}");
}
else
{
Console.WriteLine($"{message}");
}
}
}
}
Expand Down
10 changes: 3 additions & 7 deletions Source/Meadow.Logging/lib/DebugLogProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,10 @@
/// </summary>
public class DebugLogProvider : ILogProvider
{
/// <summary>
/// Called when the associated Logger has a message call
/// </summary>
/// <param name="level">The LogLevel for the message</param>
/// <param name="message">The message to log</param>
public void Log(LogLevel level, string message)
/// <inheritdoc/>
public void Log(LogLevel level, string message, string? messageGroup)
{
System.Diagnostics.Debug.WriteLine($"{level.ToString().ToUpper()}: {message}");
System.Diagnostics.Debug.WriteLine($"({messageGroup}){level.ToString().ToUpper()}: {message}");
}
}
}
3 changes: 2 additions & 1 deletion Source/Meadow.Logging/lib/ILogProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public interface ILogProvider
/// </summary>
/// <param name="level">The LogLevel for the message</param>
/// <param name="message">The message to log</param>
void Log(LogLevel level, string message);
/// <param name="messageGroup">The (optional) log message groupd</param>
void Log(LogLevel level, string message, string? messageGroup);
}
}
Loading

0 comments on commit 25c1f84

Please sign in to comment.