Fixed certain scenario that could break the event viewer

This commit is contained in:
cake
2025-12-15 19:37:22 +01:00
parent 44e91bef8f
commit ee1fcb5661
2 changed files with 42 additions and 16 deletions

View File

@@ -26,12 +26,12 @@ public sealed class PerformanceCollectorService : IHostedService
{
if (!_lightlessConfigService.Current.LogPerformance) return func.Invoke();
string cn = sender.GetType().Name + _counterSplit + counterName.BuildMessage();
var owner = sender.GetType().Name;
var counter = counterName.BuildMessage();
var cn = string.Concat(owner, _counterSplit, counter);
if (!PerformanceCounters.TryGetValue(cn, out var list))
{
list = PerformanceCounters[cn] = new(maxEntries);
}
var dt = DateTime.UtcNow.Ticks;
try
@@ -53,12 +53,12 @@ public sealed class PerformanceCollectorService : IHostedService
{
if (!_lightlessConfigService.Current.LogPerformance) { act.Invoke(); return; }
var cn = sender.GetType().Name + _counterSplit + counterName.BuildMessage();
var owner = sender.GetType().Name;
var counter = counterName.BuildMessage();
var cn = string.Concat(owner, _counterSplit, counter);
if (!PerformanceCounters.TryGetValue(cn, out var list))
{
list = PerformanceCounters[cn] = new(maxEntries);
}
var dt = DateTime.UtcNow.Ticks;
try
@@ -72,7 +72,7 @@ public sealed class PerformanceCollectorService : IHostedService
if (TimeSpan.FromTicks(elapsed) > TimeSpan.FromMilliseconds(10))
_logger.LogWarning(">10ms spike on {counterName}: {time}", cn, TimeSpan.FromTicks(elapsed));
#endif
list.Add(new(TimeOnly.FromDateTime(DateTime.Now), elapsed));
list.Add((TimeOnly.FromDateTime(DateTime.Now), elapsed));
}
}
@@ -121,11 +121,11 @@ public sealed class PerformanceCollectorService : IHostedService
sb.Append('|');
sb.Append("-Counter Name".PadRight(longestCounterName, '-'));
sb.AppendLine();
var orderedData = data.OrderBy(k => k.Key, StringComparer.OrdinalIgnoreCase).ToList();
var previousCaller = orderedData[0].Key.Split(_counterSplit, StringSplitOptions.RemoveEmptyEntries)[0];
var orderedData = data.OrderBy(k => k.Key, StringComparer.OrdinalIgnoreCase).ToList();
var previousCaller = SplitCounterKey(orderedData[0].Key).Owner;
foreach (var entry in orderedData)
{
var newCaller = entry.Key.Split(_counterSplit, StringSplitOptions.RemoveEmptyEntries)[0];
var newCaller = SplitCounterKey(entry.Key).Owner;
if (!string.Equals(previousCaller, newCaller, StringComparison.Ordinal))
{
DrawSeparator(sb, longestCounterName);
@@ -157,6 +157,12 @@ public sealed class PerformanceCollectorService : IHostedService
_logger.LogInformation("{perf}", sb.ToString());
}
private static (string Owner, string Counter) SplitCounterKey(string cn)
{
var parts = cn.Split(_counterSplit, 2, StringSplitOptions.None);
return (parts[0], parts.Length > 1 ? parts[1] : string.Empty);
}
private static void DrawSeparator(StringBuilder sb, int longestCounterName)
{
sb.Append("".PadRight(15, '-'));

View File

@@ -205,17 +205,37 @@ internal class EventViewerUI : WindowMediatorSubscriberBase
var posX = ImGui.GetCursorPosX();
var maxTextLength = ImGui.GetWindowContentRegionMax().X - posX;
var textSize = ImGui.CalcTextSize(ev.Message).X;
var msg = ev.Message;
while (textSize > maxTextLength)
var msg = ev.Message ?? string.Empty;
var maxEventTextLength = ImGui.GetContentRegionAvail().X;
if (maxEventTextLength <= 0f)
{
msg = msg[..^5] + "...";
textSize = ImGui.CalcTextSize(msg).X;
ImGui.TextUnformatted(string.Empty);
return;
}
var eventTextSize = ImGui.CalcTextSize(msg).X;
if (eventTextSize > maxEventTextLength)
{
const string ellipsis = "...";
while (eventTextSize > maxTextLength && msg.Length > ellipsis.Length)
{
var cut = Math.Min(5, msg.Length - ellipsis.Length);
msg = msg[..^cut] + ellipsis;
eventTextSize = ImGui.CalcTextSize(msg).X;
}
if (textSize > maxEventTextLength)
msg = ellipsis;
}
ImGui.TextUnformatted(msg);
if (!string.Equals(msg, ev.Message, StringComparison.Ordinal))
{
UiSharedService.AttachToolTip(ev.Message);
}
}
}
}