Skip to content

Commit 0c44742

Browse files
committed
feat: implement the log for rendering intercepted packets
Consume PacketLog objects from the injected IPacketLogHandlerService type. Handle duplicate/repeated packets by instead appending an 'xN' marker by the separator to reduce drawing events for repeated packet logs.
1 parent d109e7e commit 0c44742

1 file changed

Lines changed: 67 additions & 4 deletions

File tree

Tanji/Views/PacketLoggerView.cs

Lines changed: 67 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,84 @@
11
using System;
2+
using System.Drawing;
23
using System.Windows.Forms;
4+
using System.Threading.Tasks;
35

4-
using Tanji.Properties;
5-
using Tanji.Core.Infrastructure.ViewModels;
6-
6+
using Microsoft.Extensions.Logging;
77
using Microsoft.Extensions.DependencyInjection;
88

99
using CommunityToolkit.Mvvm.ComponentModel;
1010

11+
using Tanji.Properties;
12+
using Tanji.Core.Infrastructure.Models;
13+
using Tanji.Core.Infrastructure.Services;
14+
using Tanji.Core.Infrastructure.ViewModels;
15+
1116
namespace Tanji.Views;
1217

1318
public partial class PacketLoggerView : Form
1419
{
15-
public PacketLoggerView()
20+
private readonly ILogger<PacketLoggerView> _logger;
21+
private IPacketLogHandlerService _packetLogHandler;
22+
private readonly PacketLoggerViewModel _packetLoggerViewModel;
23+
24+
public PacketLoggerView(ILogger<PacketLoggerView> logger,
25+
PacketLoggerViewModel packetLoggerViewModel,
26+
IPacketLogHandlerService packetLogHandler)
1627
{
28+
_logger = logger;
29+
_packetLogHandler = packetLogHandler;
30+
_packetLoggerViewModel = packetLoggerViewModel;
31+
1732
InitializeComponent();
1833
Icon = Resources.Tanji_256;
34+
35+
_ = RenderPacketLogsAsync();
36+
}
37+
38+
private async Task RenderPacketLogsAsync()
39+
{
40+
while (await _packetLogHandler.WaitForPacketLogsAsync())
41+
{
42+
PacketLog pLog = await _packetLogHandler.ReadPacketLogAsync();
43+
44+
// Suspend Text Drawing
45+
loggerVw.SuspendPaint();
46+
47+
if (pLog.Repetitions < 2)
48+
{
49+
int start = loggerVw.TextLength;
50+
loggerVw.AppendText(pLog.ToString());
51+
foreach ((int length, Color highlight) in pLog.GetHighlights())
52+
{
53+
HighlightText(start, length, highlight);
54+
start += length;
55+
}
56+
}
57+
else
58+
{
59+
int previousLineIndex = loggerVw.Lines.Length - 2;
60+
string previousLine = loggerVw.Lines[previousLineIndex];
61+
int previousLineFirstCharIndex = loggerVw.Find(previousLine, RichTextBoxFinds.Reverse | RichTextBoxFinds.NoHighlight);
62+
63+
HighlightText(previousLineFirstCharIndex, previousLine.Length, _packetLoggerViewModel.PacketLoggingOptions.RepeatedHighlight);
64+
loggerVw.SelectedText = $"--------------- x{pLog.Repetitions}";
65+
}
66+
67+
// Resume Text Drawing
68+
loggerVw.ResumePaint();
69+
70+
if (_packetLoggerViewModel.PacketLoggingOptions.IsAutoScrolling)
71+
{
72+
loggerVw.ScrollToBottom();
73+
}
74+
}
75+
}
76+
77+
private void HighlightText(int start, int length, Color highlight)
78+
{
79+
loggerVw.SelectionStart = start;
80+
loggerVw.SelectionLength = length;
81+
loggerVw.SelectionColor = highlight;
1982
}
2083

2184
protected override void OnLoad(EventArgs e)

0 commit comments

Comments
 (0)