From fdc1ee91ae68a7926912f931520950bdd865c9a4 Mon Sep 17 00:00:00 2001 From: 13xforever Date: Fri, 24 Oct 2025 20:40:28 +0500 Subject: [PATCH] minor log parsing fixes and debug logging --- CompatBot/Commands/Warnings.cs | 2 +- .../LogParsing/ArchiveHandlers/GzipHandler.cs | 4 ++-- .../LogParsing/ArchiveHandlers/PlainText.cs | 3 ++- .../LogParsing/ArchiveHandlers/RarHandler.cs | 5 ++--- .../ArchiveHandlers/SevenZipHandler.cs | 5 ++--- .../LogParsing/ArchiveHandlers/ZipHandler.cs | 18 ++++++++++++------ .../LogParsing/LogParser.PipeReader.cs | 5 +++++ CompatBot/EventHandlers/LogParsingHandler.cs | 18 +++++++----------- 8 files changed, 33 insertions(+), 27 deletions(-) diff --git a/CompatBot/Commands/Warnings.cs b/CompatBot/Commands/Warnings.cs index 3cdcf203..93f7f140 100644 --- a/CompatBot/Commands/Warnings.cs +++ b/CompatBot/Commands/Warnings.cs @@ -188,7 +188,7 @@ internal static partial class Warnings **Read the {rulesCh.Mention} before continuing to chat** Refusing to read/follow the server rules *will* result in a server ban - -# You have {recentWarnCount} recent warning{StringUtils.GetSuffix(recentWarnCount)} ({totalWarnCount} total) + You have {recentWarnCount} recent warning{StringUtils.GetSuffix(recentWarnCount)} ({totalWarnCount} total) -# Warning added by {moderator.Mention} """; } diff --git a/CompatBot/EventHandlers/LogParsing/ArchiveHandlers/GzipHandler.cs b/CompatBot/EventHandlers/LogParsing/ArchiveHandlers/GzipHandler.cs index 1f4cc18b..0f12bc14 100644 --- a/CompatBot/EventHandlers/LogParsing/ArchiveHandlers/GzipHandler.cs +++ b/CompatBot/EventHandlers/LogParsing/ArchiveHandlers/GzipHandler.cs @@ -21,7 +21,6 @@ internal sealed class GzipHandler: IArchiveHandler else if (fileName.EndsWith(".log.gz", StringComparison.InvariantCultureIgnoreCase) && !fileName.Contains("tty.log", StringComparison.InvariantCultureIgnoreCase)) return (true, null); - return (false, null); } @@ -37,7 +36,8 @@ internal sealed class GzipHandler: IArchiveHandler { var memory = writer.GetMemory(Config.MinimumBufferSize); read = await gzipStream.ReadAsync(memory, cancellationToken); - writer.Advance(read); + if (read > 0) + writer.Advance(read); SourcePosition = statsStream.Position; flushed = await writer.FlushAsync(cancellationToken).ConfigureAwait(false); } while (read > 0 && !(flushed.IsCompleted || flushed.IsCanceled || cancellationToken.IsCancellationRequested)); diff --git a/CompatBot/EventHandlers/LogParsing/ArchiveHandlers/PlainText.cs b/CompatBot/EventHandlers/LogParsing/ArchiveHandlers/PlainText.cs index 944eff39..f711a013 100644 --- a/CompatBot/EventHandlers/LogParsing/ArchiveHandlers/PlainText.cs +++ b/CompatBot/EventHandlers/LogParsing/ArchiveHandlers/PlainText.cs @@ -30,7 +30,8 @@ internal sealed class PlainTextHandler: IArchiveHandler { var memory = writer.GetMemory(Config.MinimumBufferSize); read = await sourceStream.ReadAsync(memory, cancellationToken); - writer.Advance(read); + if (read > 0) + writer.Advance(read); flushed = await writer.FlushAsync(cancellationToken).ConfigureAwait(false); } while (read > 0 && !(flushed.IsCompleted || flushed.IsCanceled || cancellationToken.IsCancellationRequested)); } diff --git a/CompatBot/EventHandlers/LogParsing/ArchiveHandlers/RarHandler.cs b/CompatBot/EventHandlers/LogParsing/ArchiveHandlers/RarHandler.cs index 2a6c70dc..6f558e02 100644 --- a/CompatBot/EventHandlers/LogParsing/ArchiveHandlers/RarHandler.cs +++ b/CompatBot/EventHandlers/LogParsing/ArchiveHandlers/RarHandler.cs @@ -19,10 +19,8 @@ internal sealed class RarHandler: IArchiveHandler var firstEntry = Encoding.ASCII.GetString(header); if (!firstEntry.Contains(".log", StringComparison.InvariantCultureIgnoreCase)) return (false, "Archive doesn't contain any logs."); - return (true, null); } - return (false, null); } @@ -46,7 +44,8 @@ internal sealed class RarHandler: IArchiveHandler { var memory = writer.GetMemory(Config.MinimumBufferSize); read = await rarStream.ReadAsync(memory, cancellationToken); - writer.Advance(read); + if (read > 0) + writer.Advance(read); SourcePosition = statsStream.Position; flushed = await writer.FlushAsync(cancellationToken).ConfigureAwait(false); SourcePosition = statsStream.Position; diff --git a/CompatBot/EventHandlers/LogParsing/ArchiveHandlers/SevenZipHandler.cs b/CompatBot/EventHandlers/LogParsing/ArchiveHandlers/SevenZipHandler.cs index a66b274e..dc720329 100644 --- a/CompatBot/EventHandlers/LogParsing/ArchiveHandlers/SevenZipHandler.cs +++ b/CompatBot/EventHandlers/LogParsing/ArchiveHandlers/SevenZipHandler.cs @@ -19,10 +19,8 @@ internal sealed class SevenZipHandler: IArchiveHandler { if (fileSize > Config.AttachmentSizeLimit) return (false, $"Log size is too large for 7z format: {fileSize.AsStorageUnit()} (max allowed is {Config.AttachmentSizeLimit.AsStorageUnit()})"); - return (true, null); } - return (false, null); } @@ -48,7 +46,8 @@ internal sealed class SevenZipHandler: IArchiveHandler { var memory = writer.GetMemory(Config.MinimumBufferSize); read = await entryStream.ReadAsync(memory, cancellationToken); - writer.Advance(read); + if (read > 0) + writer.Advance(read); flushed = await writer.FlushAsync(cancellationToken).ConfigureAwait(false); } while (read > 0 && !(flushed.IsCompleted || flushed.IsCanceled || cancellationToken.IsCancellationRequested)); await writer.CompleteAsync(); diff --git a/CompatBot/EventHandlers/LogParsing/ArchiveHandlers/ZipHandler.cs b/CompatBot/EventHandlers/LogParsing/ArchiveHandlers/ZipHandler.cs index c8f30bbf..b5499025 100644 --- a/CompatBot/EventHandlers/LogParsing/ArchiveHandlers/ZipHandler.cs +++ b/CompatBot/EventHandlers/LogParsing/ArchiveHandlers/ZipHandler.cs @@ -40,19 +40,25 @@ internal sealed class ZipHandler: IArchiveHandler && !zipReader.Entry.Key.Contains("tty.log", StringComparison.InvariantCultureIgnoreCase)) { LogSize = zipReader.Entry.Size; - await using var rarStream = zipReader.OpenEntryStream(); - int read; + await using var zipStream = zipReader.OpenEntryStream(); + int read, totalRead = 0; FlushResult flushed; do { var memory = writer.GetMemory(Config.MinimumBufferSize); - read = await rarStream.ReadAsync(memory, cancellationToken); - writer.Advance(read); + read = await zipStream.ReadAsync(memory, cancellationToken).ConfigureAwait(false); + Config.Log.Debug($"{nameof(ZipHandler)}: read {read} bytes from source stream"); + if (read > 0) + writer.Advance(read); + totalRead += read; + Config.Log.Debug($"{nameof(ZipHandler)}: advanced the writer by {read} (total read {totalRead})"); SourcePosition = statsStream.Position; + Config.Log.Debug($"{nameof(ZipHandler)}: current source position is {SourcePosition}"); flushed = await writer.FlushAsync(cancellationToken).ConfigureAwait(false); - SourcePosition = statsStream.Position; + Config.Log.Debug($"{nameof(ZipHandler)}: flushed the writer"); } while (read > 0 && !(flushed.IsCompleted || flushed.IsCanceled || cancellationToken.IsCancellationRequested)); - await writer.CompleteAsync(); + await writer.CompleteAsync().ConfigureAwait(false); + Config.Log.Debug($"{nameof(ZipHandler)}: writer completed"); return; } SourcePosition = statsStream.Position; diff --git a/CompatBot/EventHandlers/LogParsing/LogParser.PipeReader.cs b/CompatBot/EventHandlers/LogParsing/LogParser.PipeReader.cs index 23a81b8b..83aa555e 100644 --- a/CompatBot/EventHandlers/LogParsing/LogParser.PipeReader.cs +++ b/CompatBot/EventHandlers/LogParsing/LogParser.PipeReader.cs @@ -22,6 +22,7 @@ internal static partial class LogParser { result = await reader.ReadAsync(cancellationToken).ConfigureAwait(false); var buffer = result.Buffer; + Config.Log.Debug($"{nameof(LogParser)}: Read {buffer.Length} bytes from reader"); if (!skippedBom) { if (buffer.Length < 3) @@ -31,6 +32,7 @@ internal static partial class LogParser if (potentialBom.ToArray().SequenceEqual(Bom)) { reader.AdvanceTo(potentialBom.End); + Config.Log.Debug($"{nameof(LogParser)}: Reader advanced to {potentialBom.End} to skip BOM"); totalReadBytes += potentialBom.Length; skippedBom = true; continue; @@ -50,6 +52,7 @@ internal static partial class LogParser if (state.Error != LogParseState.ErrorCode.None) { await reader.CompleteAsync(); + Config.Log.Debug($"{nameof(LogParser)}: Reader completed (error: {state.Error})"); return state; } @@ -70,6 +73,7 @@ internal static partial class LogParser var sectionStart = currentSectionLines.First is {} firstLine ? firstLine.Value : buffer; totalReadBytes += result.Buffer.Slice(0, sectionStart.Start).Length; reader.AdvanceTo(sectionStart.Start); + Config.Log.Debug($"{nameof(LogParser)}: Reader advanced to {sectionStart.Start} (section start)"); } catch (Exception e) { @@ -82,6 +86,7 @@ internal static partial class LogParser await TaskScheduler.WaitForClearTagAsync(state).ConfigureAwait(false); state.ReadBytes = totalReadBytes; await reader.CompleteAsync(); + Config.Log.Debug($"{nameof(LogParser)}: Reader completed"); return state; } diff --git a/CompatBot/EventHandlers/LogParsingHandler.cs b/CompatBot/EventHandlers/LogParsingHandler.cs index 8c170a81..7c3fd7b8 100644 --- a/CompatBot/EventHandlers/LogParsingHandler.cs +++ b/CompatBot/EventHandlers/LogParsingHandler.cs @@ -108,8 +108,7 @@ public static class LogParsingHandler try { - Config.Log.Debug( - $">>>>>>> {message.Id % 100} Parsing log '{source.FileName}' from {message.Author.Username}#{message.Author.Discriminator} ({message.Author.Id}) using {source.GetType().Name} ({source.SourceFileSize} bytes)…"); + Config.Log.Debug($">>>>>>> {message.Id % 100} Parsing log '{source.FileName}' from {message.Author.Username}#{message.Author.Discriminator} ({message.Author.Id}) using {source.GetType().Name} ({source.SourceFileSize} bytes)…"); var analyzingProgressEmbed = GetAnalyzingMsgEmbed(client); var msgBuilder = new DiscordMessageBuilder() .AddEmbed(await analyzingProgressEmbed.AddAuthorAsync(client, message, source).ConfigureAwait(false)) @@ -120,8 +119,7 @@ public static class LogParsingHandler LogParseState? result = null, tmpResult; using (var timeout = new CancellationTokenSource(Config.LogParsingTimeoutInSec)) { - using var combinedTokenSource = - CancellationTokenSource.CreateLinkedTokenSource(timeout.Token, Config.Cts.Token); + using var combinedTokenSource = CancellationTokenSource.CreateLinkedTokenSource(timeout.Token, Config.Cts.Token); var tries = 0; do { @@ -147,9 +145,8 @@ public static class LogParsingHandler Please run the game again and re-upload a new copy. """, Color = Config.Colors.LogResultFailed, - } - .AddAuthorAsync(client, message, source).ConfigureAwait(false)) - .Build() + }.AddAuthorAsync(client, message, source).ConfigureAwait(false)) + .Build() ).ConfigureAwait(false); Config.TelemetryClient?.TrackRequest(nameof(LogParsingHandler), start, DateTimeOffset.UtcNow - start, HttpStatusCode.InternalServerError.ToString(), false); @@ -248,8 +245,7 @@ public static class LogParsingHandler { if (result.SelectedFilter != null) { - var ignoreFlags = FilterAction.IssueWarning | FilterAction.SendMessage | - FilterAction.ShowExplain; + var ignoreFlags = FilterAction.IssueWarning | FilterAction.SendMessage | FilterAction.ShowExplain; await ContentFilter.PerformFilterActions(client, message, result.SelectedFilter, ignoreFlags, result.SelectedFilterContext!).ConfigureAwait(false); } @@ -382,9 +378,9 @@ public static class LogParsingHandler } catch (Exception pre) { - if (!(pre is OperationCanceledException)) + if (pre is not OperationCanceledException) Config.Log.Error(pre); - if (result == null) + if (result is null) throw; }