Skip to content

Commit

Permalink
Merge pull request Mapsui#2862 from Mapsui/feature/improve-logging
Browse files Browse the repository at this point in the history
Improve logging for logging widget
  • Loading branch information
pauldendulk authored Feb 1, 2025
2 parents 1b8f334 + 1a8de21 commit 08adbbf
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 9 deletions.
4 changes: 2 additions & 2 deletions Mapsui.Rendering.Skia/MapRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ private void Render(SKCanvas canvas, Viewport viewport, IEnumerable<ILayer> laye
}
catch (Exception exception)
{
Logger.Log(LogLevel.Error, "Unexpected error in skia renderer", exception);
Logger.Log(LogLevel.Error, $"Unexpected error in skia renderer", exception);
}
}

Expand Down Expand Up @@ -337,7 +337,7 @@ public MapInfo GetMapInfo(ScreenPosition screenPosition, Viewport viewport, IEnu
}
catch (Exception exception)
{
Logger.Log(LogLevel.Error, "Unexpected error in skia renderer", exception);
Logger.Log(LogLevel.Error, $"Unexpected error in MapInfo skia renderer: {exception.Message}", exception);
}

return mapInfo;
Expand Down
3 changes: 1 addition & 2 deletions Mapsui.Rendering.Skia/SkiaWidgets/LoggingWidgetRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@ public void Draw(SKCanvas canvas, Viewport viewport, IWidget widget, RenderServi
_ => _informationTextPaint,
};

canvas.DrawText(entry.LogLevel.ToString(), (float)(marginX + paddingX), (float)(marginY + (paddingX * line) + loggingWidget.TextSize * (line + 1)), _font, paint);
canvas.DrawText(entry.Description, (float)(marginX + paddingX + _levelWidth + 2 * paddingX), (float)(marginY + (paddingY * line) + loggingWidget.TextSize * (line + 1)), _font, paint);
canvas.DrawText(entry.FormattedLogLine, (float)(marginX + paddingX), (float)(marginY + (paddingX * line) + loggingWidget.TextSize * (line + 1)), _font, paint);

line++;
}
Expand Down
2 changes: 1 addition & 1 deletion Mapsui.UI.Shared/MapControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ private void InvalidateTimerCallback(object? state)
}
catch (Exception ex)
{
Logger.Log(LogLevel.Error, ex.Message, ex);
Logger.Log(LogLevel.Error, $"Error in render loop", ex);
}
}

Expand Down
2 changes: 1 addition & 1 deletion Mapsui/RemoteMapInfoFetcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public static async Task<MapInfo> GetRemoteMapInfoAsync(ScreenPosition screenPos
}
catch (Exception exception)
{
Logger.Log(LogLevel.Error, "Unexpected error in skia renderer", exception);
Logger.Log(LogLevel.Error, $"Unexpected error in remote MapInfo skia renderer", exception);
}

return await Task.FromResult(mapInfo);
Expand Down
35 changes: 32 additions & 3 deletions Mapsui/Widgets/InfoWidgets/LoggingWidget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Mapsui.Widgets.BoxWidgets;
using System;
using System.Collections.Concurrent;
using System.Text;

namespace Mapsui.Widgets.InfoWidgets;

Expand Down Expand Up @@ -36,8 +37,7 @@ public class LoggingWidget : TextBoxWidget
public struct LogEntry
{
public LogLevel LogLevel;
public string Description;
public Exception? Exception;
public string FormattedLogLine;
}

public LoggingWidget()
Expand Down Expand Up @@ -67,7 +67,7 @@ public void Log(LogLevel level, string description, Exception? exception)
if (!ShouldLog(Enabled, ShowLoggingInMap))
return;

var entry = new LogEntry { LogLevel = level, Description = description, Exception = exception };
var entry = new LogEntry { LogLevel = level, FormattedLogLine = ToFormattedLogLine(level, description, exception) };

_listOfLogEntries.Enqueue(entry);

Expand All @@ -79,6 +79,25 @@ public void Log(LogLevel level, string description, Exception? exception)
Invalidate(nameof(Text));
}

private string ToFormattedLogLine(LogLevel level, string description, Exception? exception)
{
var builder = new StringBuilder();

builder.Append(ToString(level));
builder.Append(": ");
if (string.IsNullOrEmpty(description))
builder.Append("NO MESSAGE");
else
builder.Append(description);
if (exception != null)
{
builder.Append($" - EXCEPTION: {exception.GetType()}");
if (!string.IsNullOrEmpty(exception.Message))
builder.Append($" - EXCEPTION MESSAGE: {exception.Message}");
}
return builder.ToString();
}

public void Clear()
{
while (!_listOfLogEntries.IsEmpty)
Expand Down Expand Up @@ -184,4 +203,14 @@ private static bool ShouldLog(bool enabled, ShowLoggingInMap showLoggingInMap) =
ShowLoggingInMap.ShowOnlyInDebugMode => System.Diagnostics.Debugger.IsAttached,
_ => throw new NotSupportedException(nameof(InfoWidgets.ShowLoggingInMap))
};

private string ToString(LogLevel logLevel) => logLevel switch
{
LogLevel.Error => "ERROR",
LogLevel.Warning => "WARN",
LogLevel.Information => "INFO",
LogLevel.Debug => "DEBUG",
LogLevel.Trace => "TRACE",
_ => throw new NotSupportedException(nameof(LogLevel))
};
}

0 comments on commit 08adbbf

Please sign in to comment.