minor log parsing fixes and debug logging

This commit is contained in:
13xforever
2025-10-24 20:40:28 +05:00
parent 67481d34c5
commit fdc1ee91ae
8 changed files with 33 additions and 27 deletions

View File

@@ -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}
""";
}

View File

@@ -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));

View File

@@ -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));
}

View File

@@ -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;

View File

@@ -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();

View File

@@ -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;

View File

@@ -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;
}

View File

@@ -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;
}