feat(ui): complete Avalonia UI port with 7 main pages, tool settings & top MenuBar
This commit is contained in:
@@ -0,0 +1,154 @@
|
||||
using System.Collections.Concurrent;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace ClawdDotNet.App.Services;
|
||||
|
||||
/// <summary>Eine gelesene Logzeile.</summary>
|
||||
/// <param name="Module">Der Dateiname ohne Endung — jedes Modul schreibt in seine eigene Datei.</param>
|
||||
/// <param name="Level">INF, WRN, ERR … oder leer, wenn die Zeile kein bekanntes Format hat.</param>
|
||||
public readonly record struct LogLine(string Module, string Level, string Text);
|
||||
|
||||
/// <summary>Ab welcher Stufe angezeigt wird.</summary>
|
||||
public enum LogLevelFilter { All, Info, Warn, Error }
|
||||
|
||||
/// <summary>
|
||||
/// Liest neu hinzugekommene Zeilen aus den Logdateien.
|
||||
///
|
||||
/// <para>Die vorige Fassung (<c>LiveLogViewerService</c>) schrieb unmittelbar in eine
|
||||
/// <c>RichTextBox</c> und taktete über einen <c>System.Windows.Forms.Timer</c> — Lesen
|
||||
/// und Darstellen waren dasselbe Ding und ohne Fenster nicht zu haben. Hier bleibt nur
|
||||
/// das Lesen; was damit geschieht, entscheidet der Aufrufer.</para>
|
||||
///
|
||||
/// <para>Merkt sich je Datei die Leseposition, gibt also bei jedem Aufruf nur das
|
||||
/// Neue zurück. Wird eine Datei kürzer, gilt sie als rotiert und wird von vorn gelesen.</para>
|
||||
/// </summary>
|
||||
public sealed partial class LogTail(string logDirectory)
|
||||
{
|
||||
private readonly ConcurrentDictionary<string, long> _positions = new();
|
||||
|
||||
[GeneratedRegex(@"\[(TRC|DBG|INF|WRN|ERR|FTL)\]", RegexOptions.IgnoreCase)]
|
||||
private static partial Regex LevelPattern();
|
||||
|
||||
/// <summary>Die Module, für die heute Dateien vorliegen — füllt das Auswahlfeld.</summary>
|
||||
public IReadOnlyList<string> AvailableModules()
|
||||
{
|
||||
var directory = TodayDirectory();
|
||||
if (directory is null) return [];
|
||||
|
||||
return Directory.GetFiles(directory, "*.log")
|
||||
.Select(Path.GetFileNameWithoutExtension)
|
||||
.Where(name => !string.IsNullOrEmpty(name))
|
||||
.Select(name => name!)
|
||||
.OrderBy(name => name, StringComparer.OrdinalIgnoreCase)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Alles, was seit dem letzten Aufruf dazugekommen ist.
|
||||
///
|
||||
/// <paramref name="module"/> leer heißt: alle Module.
|
||||
/// </summary>
|
||||
public IReadOnlyList<LogLine> ReadNew(string? module = null, LogLevelFilter level = LogLevelFilter.All)
|
||||
{
|
||||
var directory = TodayDirectory();
|
||||
if (directory is null) return [];
|
||||
|
||||
var result = new List<LogLine>();
|
||||
|
||||
foreach (var path in Directory.GetFiles(directory, "*.log"))
|
||||
{
|
||||
var moduleName = Path.GetFileNameWithoutExtension(path);
|
||||
|
||||
if (!string.IsNullOrEmpty(module)
|
||||
&& !string.Equals(moduleName, module, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
// Position trotzdem nachziehen: Sonst käme beim Wechsel des Filters
|
||||
// die gesamte bisherige Datei auf einmal herein.
|
||||
TrackWithoutReading(path);
|
||||
continue;
|
||||
}
|
||||
|
||||
ReadFile(path, moduleName, level, result);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private void ReadFile(string path, string module, LogLevelFilter level, List<LogLine> into)
|
||||
{
|
||||
var lastPosition = _positions.GetOrAdd(path, 0L);
|
||||
|
||||
try
|
||||
{
|
||||
// FileShare.ReadWrite | Delete: Der Schreiber muss weiterarbeiten und die
|
||||
// Datei auch ersetzen können, während wir lesen.
|
||||
using var stream = new FileStream(path, FileMode.Open, FileAccess.Read,
|
||||
FileShare.ReadWrite | FileShare.Delete);
|
||||
|
||||
if (stream.Length < lastPosition)
|
||||
lastPosition = 0; // rotiert oder gekürzt
|
||||
|
||||
if (stream.Length == lastPosition)
|
||||
return;
|
||||
|
||||
stream.Seek(lastPosition, SeekOrigin.Begin);
|
||||
using var reader = new StreamReader(stream);
|
||||
|
||||
while (reader.ReadLine() is { } line)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(line)) continue;
|
||||
|
||||
var lineLevel = ExtractLevel(line);
|
||||
if (!Passes(lineLevel, level)) continue;
|
||||
|
||||
into.Add(new LogLine(module, lineLevel, line));
|
||||
}
|
||||
|
||||
_positions[path] = stream.Position;
|
||||
}
|
||||
catch (IOException)
|
||||
{
|
||||
// Wird gerade geschrieben — beim nächsten Takt erneut versuchen.
|
||||
}
|
||||
}
|
||||
|
||||
private void TrackWithoutReading(string path)
|
||||
{
|
||||
try { _positions[path] = new FileInfo(path).Length; }
|
||||
catch (IOException) { }
|
||||
}
|
||||
|
||||
private static string ExtractLevel(string line)
|
||||
{
|
||||
var match = LevelPattern().Match(line);
|
||||
return match.Success ? match.Groups[1].Value.ToUpperInvariant() : "";
|
||||
}
|
||||
|
||||
private static bool Passes(string lineLevel, LogLevelFilter filter) => filter switch
|
||||
{
|
||||
LogLevelFilter.All => true,
|
||||
// Unbekanntes Format durchlassen: Lieber eine Zeile zu viel als eine
|
||||
// Fehlermeldung, die der Filter verschluckt.
|
||||
_ when lineLevel.Length == 0 => true,
|
||||
LogLevelFilter.Info => lineLevel is "INF" or "WRN" or "ERR" or "FTL",
|
||||
LogLevelFilter.Warn => lineLevel is "WRN" or "ERR" or "FTL",
|
||||
LogLevelFilter.Error => lineLevel is "ERR" or "FTL",
|
||||
_ => true
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// Das Verzeichnis des heutigen Tages, oder <c>null</c>.
|
||||
///
|
||||
/// <c>DateTime.Now</c> und nicht UTC: Die Schreibseite legt die Verzeichnisse nach
|
||||
/// Ortszeit an, also muss hier dieselbe Rechnung gelten. Auf einem Server mit
|
||||
/// <c>TZ=UTC</c> wechselt der Ordner damit um Mitternacht UTC — richtig, aber
|
||||
/// erwähnenswert, wenn jemand die Umstellung um 02:00 Ortszeit sucht.
|
||||
/// </summary>
|
||||
private string? TodayDirectory()
|
||||
{
|
||||
if (!Directory.Exists(logDirectory)) return null;
|
||||
|
||||
var directory = Path.Combine(logDirectory, DateTime.Now.ToString("yyyy-MM-dd"));
|
||||
return Directory.Exists(directory) ? directory : null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user