Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release 1.11.0 #47

Merged
merged 1 commit into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 11 additions & 92 deletions Source/Meadow.Logging.LogProviders/Driver/CloudLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Json;
using System.Threading;
using System.Threading.Tasks;

namespace Meadow.Logging;

/// <summary>
/// Meadow.Cloud logging
/// </summary>
public class CloudLogger : ILogProvider
{
/// <summary>
Expand All @@ -26,58 +26,34 @@ public CloudLogger(LogLevel level = LogLevel.Information)
}

MinLevel = level;

LogFilePath = Path.Combine(Resolver.Device.PlatformOS.FileSystem.DocumentsDirectory, "cloud.log");
if (!File.Exists(LogFilePath))
{
using FileStream fs = File.Create(LogFilePath);
fs.Close();
}

EventFilePath = Path.Combine(Resolver.Device.PlatformOS.FileSystem.DocumentsDirectory, "events.log");
if (!File.Exists(EventFilePath))
{
using FileStream fs = File.Create(EventFilePath);
fs.Close();
}
}

/// <summary>
/// Path to the log file
/// </summary>
public string LogFilePath { get; protected set; }
/// <summary>
/// Path to the event file
/// </summary>
public string EventFilePath { get; protected set; }

/// <summary>
/// Current minimum level for the CloudLogger
/// </summary>
public LogLevel MinLevel { get; protected set; }

private static SemaphoreSlim semaphoreSlim = new SemaphoreSlim(1, 1);

/// <inheritdoc/>
public async void Log(LogLevel level, string message, string? _)
public void Log(LogLevel level, string message, string? _)
{
if (level >= MinLevel)
{
var cloudLog = new CloudLog()
var log = new CloudLog()
{
Severity = level.ToString(),
Message = message,
Timestamp = DateTime.UtcNow
};

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

/// <summary>
/// Log an exception.
/// </summary>
/// <param name="ex"></param>
public async void LogException(Exception ex)
public void LogException(Exception ex)
{
var log = new CloudLog()
{
Expand All @@ -87,7 +63,7 @@ public async void LogException(Exception ex)
Timestamp = DateTime.UtcNow
};

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

/// <summary>
Expand All @@ -96,7 +72,7 @@ public async void LogException(Exception ex)
/// <param name="eventId">id used for a set of events.</param>
/// <param name="description">Description of the event.</param>
/// <param name="measurements">Dynamic payload of measurements to be recorded.</param>
public async void LogEvent(int eventId, string description, Dictionary<string, object> measurements)
public void LogEvent(int eventId, string description, Dictionary<string, object> measurements)
{
var cloudEvent = new CloudEvent()
{
Expand All @@ -106,63 +82,6 @@ public async void LogEvent(int eventId, string description, Dictionary<string, o
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)
{
await semaphoreSlim.WaitAsync();

try
{
// send messages that were stored offline
var lines = File.ReadAllLines(file);
if (lines.Length > 0)
{
Resolver.Log.Debug($"processing {lines.Length} stored {typeof(T)}");
foreach (var line in lines)
{
if (string.IsNullOrWhiteSpace(line))
{
continue;
}

var o = JsonSerializer.Deserialize<T>(line, serializeOptions);
if (o != null)
{
await sendFunc(o);
}
}

using FileStream fs = File.Create(file);
fs.Close();
Resolver.Log.Debug($"cleared stored {typeof(T)}");
}

// send current message
Resolver.Log.Debug($"sending {typeof(T)}");
await sendFunc(item);
}
catch (Exception ex)
{
Resolver.Log.Debug($"error sending {typeof(T)}: {ex.Message}");
}
finally
{
semaphoreSlim.Release();
}
}
else
{
var json = JsonSerializer.Serialize(item, serializeOptions);
File.AppendAllLines(file, new[] { json });
Resolver.Log.Debug($"saved cloud log to local store {json}");
}
Resolver.MeadowCloudService.SendEvent(cloudEvent);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Meadow.Sdk/1.1.0">
<PropertyGroup>
<Version>1.9.0</Version>
<Version>1.11.0</Version>
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
<TargetFramework>netstandard2.1</TargetFramework>
<OutputType>Library</OutputType>
Expand Down
18 changes: 13 additions & 5 deletions Source/Meadow.Logging.LogProviders/Driver/UdpLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ namespace Meadow.Logging
public class UdpLogger : ILogProvider, IDisposable
{
private bool _isDisposed;
private int _port;
private UdpClient _client;
private IPEndPoint _broadcast;
private char _delimiter;
private readonly int _port;
private readonly UdpClient _client;
private readonly IPEndPoint _broadcast;
private readonly char _delimiter;

/// <summary>
/// Creates a UdpLogger instance
Expand All @@ -34,7 +34,15 @@ public UdpLogger(int port = 5100, char delimiter = '\t')
public void Log(LogLevel level, string message, string? _)
{
var payload = Encoding.UTF8.GetBytes($"{level}{_delimiter}{message}\n");
_client.Send(payload, payload.Length, _broadcast);
try
{
_client.Send(payload, payload.Length, _broadcast);
}
catch (Exception ex)
{
Console.WriteLine($"UDP ERR: {ex.Message}");
// TODO: ignore exceptions ?
}
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion Source/Meadow.Logging/lib/Meadow.Logging.csproj
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Version>1.9.0</Version>
<Version>1.11.0</Version>
<Authors>Wilderness Labs, Inc</Authors>
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
<TargetFramework>netstandard2.1</TargetFramework>
Expand Down
Loading