Skip to content

Commit 3574725

Browse files
rmbadminAigioL
andauthored
Feature/ipc console log (#3463)
* ๐ŸŽจ LogConsole * ๐Ÿ› ref * ๐Ÿ’„ ProxyLog UI * ๐Ÿ› WebApplication Logging * ๐Ÿ› Misc * ๐ŸŽจ Log format * ๐ŸŽจ LogConsoleService.ConsoleMaxCharCount * ๐Ÿ› Misc --------- Co-authored-by: Aigio Liu <lysoft.pw@foxmail.com>
1 parent 6a3c8f6 commit 3574725

18 files changed

Lines changed: 636 additions & 12 deletions

File tree

โ€Žsrc/BD.WTTS.Client.Plugins.Accelerator.ReverseProxy/BD.WTTS.Client.Plugins.Accelerator.ReverseProxy.csprojโ€Ž

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@
7676
<Compile Include="..\BD.WTTS.Client\Services\Platform\IPlatformService.OS.Linux.Etc.Os_Release.cs">
7777
<LinkBase>Services\Platform</LinkBase>
7878
</Compile>
79+
<Compile Include="..\BD.WTTS.Client\Services\Mvvm\LogConsoleService.Logger.cs">
80+
<LinkBase>Services\Mvvm</LinkBase>
81+
</Compile>
7982
</ItemGroup>
8083

8184
<ItemGroup>
@@ -104,6 +107,7 @@
104107
<PackageReference Include="System.Drawing.Common" />
105108
<PackageReference Include="System.Runtime.Caching" />
106109
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" />
110+
<PackageReference Include="Utf8StringInterpolation" />
107111
</ItemGroup>
108112

109113
<ItemGroup Condition="$([MSBuild]::IsOSPlatform('windows')) AND ($(RuntimeIdentifier.StartsWith('win-')) OR $(RuntimeIdentifier) == '')">

โ€Žsrc/BD.WTTS.Client.Plugins.Accelerator.ReverseProxy/Program.csโ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ static void ConfigureServices(IServiceCollection services)
8181
#if DEBUG
8282
l.AddConsole();
8383
#endif
84+
l.AddProvider(new LogConsoleService.Utf8StringLoggerProvider(moduleName));
8485
});
8586

8687
// ่ฎพ็ฝฎไป“ๅ‚จๅฑ‚ๆ•ฐๆฎๅบ“ๆ–‡ไปถๅญ˜ๆ”พ่ทฏๅพ„

โ€Žsrc/BD.WTTS.Client.Plugins.Accelerator.ReverseProxy/Services.Implementation/LazyReverseProxyServiceImpl.csโ€Ž

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ public IReadOnlyCollection<ScriptIPCDTO>? Scripts
2323

2424
public byte[]? GetFlowStatistics_Bytes() => impl().GetFlowStatistics_Bytes();
2525

26+
public string? GetLogAllMessage() => impl().GetLogAllMessage();
27+
2628
public async Task<StartProxyResult> StartProxyAsync(byte[] reverseProxySettings)
2729
{
2830
var result = await impl().StartProxyAsync(reverseProxySettings);

โ€Žsrc/BD.WTTS.Client.Plugins.Accelerator.ReverseProxy/Services.Implementation/YarpReverseProxyServiceImpl.csโ€Ž

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ StartProxyResult StartProxyCore()
6565
WebRootPath = RootPath,
6666
});
6767

68+
builder.Logging.AddProvider(new LogConsoleService.Utf8StringLoggerProvider(AssemblyInfo.Accelerator));
69+
6870
builder.Services.Configure<HostFilteringOptions>(static o =>
6971
{
7072
o.AllowEmptyHosts = true;
@@ -167,6 +169,12 @@ public async Task StopProxyAsync()
167169
return bytes;
168170
}
169171

172+
public string? GetLogAllMessage()
173+
{
174+
var result = LogConsoleService.Builder;
175+
return result.ToString();
176+
}
177+
170178
// IDisposable
171179

172180
protected override void DisposeCore()

โ€Žsrc/BD.WTTS.Client.Plugins.Accelerator.ReverseProxy/Services/IReverseProxyService.csโ€Ž

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ partial interface IReverseProxyService
1515
/// </summary>
1616
/// <returns></returns>
1717
byte[]? GetFlowStatistics_Bytes();
18+
19+
/// <summary>
20+
/// ่Žทๅ–ๆ‰€ๆœ‰ๆ—ฅๅฟ—ไฟกๆฏ
21+
/// </summary>
22+
/// <returns></returns>
23+
string? GetLogAllMessage();
1824
}
1925

2026
public static partial class ReverseProxyServiceExtensions

โ€Žsrc/BD.WTTS.Client.Plugins.Accelerator/UI/Views/Controls/ProxyChartView.axaml.csโ€Ž

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
using Avalonia;
22
using Avalonia.Controls;
3-
using Avalonia.Data;
43
using Avalonia.Threading;
54
using Avalonia.VisualTree;
65
using LiveChartsCore;
7-
using LiveChartsCore.Motion;
86
using LiveChartsCore.SkiaSharpView;
97
using LiveChartsCore.SkiaSharpView.Avalonia;
10-
using LiveChartsCore.SkiaSharpView.Drawing;
11-
using SkiaSharp;
128

139
namespace BD.WTTS.UI.Views.Controls;
1410

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<UserControl
2+
x:Class="BD.WTTS.UI.Views.Controls.ProxyLog"
3+
xmlns="https://github.com/avaloniaui"
4+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
5+
xmlns:AvaloniaEdit="using:AvaloniaEdit"
6+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
7+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
8+
xmlns:s="https://steampp.net/services"
9+
xmlns:spp="https://steampp.net/ui"
10+
d:DesignHeight="450"
11+
d:DesignWidth="800"
12+
mc:Ignorable="d">
13+
<Border Background="{DynamicResource CardBackgroundFillColorDefaultBrush}" CornerRadius="{DynamicResource ControlCornerRadius}">
14+
<AvaloniaEdit:TextEditor
15+
x:Name="LogTextbox"
16+
HorizontalScrollBarVisibility="Disabled"
17+
IsReadOnly="True"
18+
ShowLineNumbers="True" />
19+
</Border>
20+
</UserControl>
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
using Avalonia.Controls;
2+
using Avalonia.Threading;
3+
using Avalonia.VisualTree;
4+
using AvaloniaEdit;
5+
using AvaloniaEdit.TextMate;
6+
using Org.BouncyCastle.Math;
7+
using TextMateSharp.Grammars;
8+
9+
namespace BD.WTTS.UI.Views.Controls;
10+
11+
public partial class ProxyLog : UserControl
12+
{
13+
CancellationTokenSource cancellation = new();
14+
15+
public ProxyLog()
16+
{
17+
InitializeComponent();
18+
19+
var logTextbox = this.FindControl<TextEditor>("LogTextbox")!;
20+
21+
//Here we initialize RegistryOptions with the theme we want to use.
22+
var registryOptions = new TextMateSharp.Grammars.RegistryOptions(ThemeName.Dark);
23+
24+
//Initial setup of TextMate.
25+
var textMateInstallation = logTextbox.InstallTextMate(registryOptions);
26+
27+
//Here we are getting the language by the extension and right after that we are initializing grammar with this language.
28+
//And that's all ๐Ÿ˜€, you are ready to use AvaloniaEdit with syntax highlighting!
29+
textMateInstallation.SetGrammar(registryOptions.GetScopeByLanguageId(registryOptions.GetLanguageByExtension(".log").Id));
30+
31+
//logTextbox.Text = LogText;
32+
//logTextbox.ScrollToLine(logTextbox.Document.LineCount);
33+
34+
logTextbox.TextChanged += LogTextbox_TextChanged;
35+
36+
ProxyService.Current.WhenAnyValue(x => x.ProxyStatus)
37+
.Subscribe(x =>
38+
{
39+
if (x)
40+
{
41+
cancellation = new CancellationTokenSource();
42+
Task2.InBackground(FlushLogsAsync, true);
43+
}
44+
else
45+
{
46+
if (cancellation != null)
47+
{
48+
cancellation.Cancel();
49+
cancellation.Dispose();
50+
}
51+
}
52+
});
53+
}
54+
55+
private void LogTextbox_TextChanged(object? sender, EventArgs e)
56+
{
57+
LogTextbox.ScrollToLine(LogTextbox.Document.LineCount);
58+
}
59+
60+
void FlushLogsAsync()
61+
{
62+
while (!cancellation.IsCancellationRequested)
63+
{
64+
try
65+
{
66+
var isAttachedToVisualTree = this.IsAttachedToVisualTree();
67+
if (isAttachedToVisualTree)
68+
{
69+
var logtext = IReverseProxyService.Constants.Instance.GetLogAllMessage();
70+
if (string.IsNullOrEmpty(logtext))
71+
{
72+
Thread.Sleep(1000);
73+
continue;
74+
}
75+
76+
Dispatcher.UIThread.Post(() =>
77+
{
78+
if (LogTextbox.Text.Length != logtext.Length)
79+
LogTextbox.Text = logtext;
80+
});
81+
}
82+
}
83+
catch
84+
{
85+
}
86+
finally
87+
{
88+
Thread.Sleep(1000);
89+
}
90+
}
91+
}
92+
}

โ€Žsrc/BD.WTTS.Client.Plugins.Accelerator/UI/Views/Pages/AcceleratorPage2.axamlโ€Ž

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,7 @@
449449
</Border>
450450
</WrapPanel>-->
451451
</TabStripItem>
452+
<TabStripItem Content="{x:Static spp:Strings.CommunityFix_ProxyLog}" IsVisible="{Binding Source={x:Static s:ProxyService.Current}, Path=ProxyStatus, Mode=OneWay}" />
452453
<TabStripItem Content="{x:Static spp:Strings.CommunityFix_FlowStatistics}" IsVisible="{Binding Source={x:Static s:ProxyService.Current}, Path=ProxyStatus, Mode=OneWay}" />
453454
<TabStripItem Content="{x:Static spp:Strings.CommunityFix_Services}" IsVisible="{Binding Source={x:Static s:ProxyService.Current}, Path=ProxyStatus, Mode=OneWay}" />
454455
<TabStripItem Name="GameAccTab">
@@ -557,6 +558,8 @@
557558
</ItemsControl>
558559
</spp:ContentLoader>
559560
</ScrollViewer>
561+
<!-- ๅŠ ้€Ÿๆ—ฅๅฟ— -->
562+
<spp:ProxyLog Margin="5,0" />
560563

561564
<!-- ๆต้‡็ปŸ่ฎก -->
562565
<spp:ProxyChartView Margin="5,0" />

โ€Žsrc/BD.WTTS.Client/App/IApplication.Log.csโ€Ž

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,22 @@ public static Action<ILoggingBuilder> ConfigureLogging(LogLevel minLevel = Defau
8686
//#elif MACOS || MACCATALYST || (IOS && DEBUG)
8787
// builder.AddProvider(Logging.OSLogLoggerProvider.Instance);
8888
//#endif
89+
90+
//#if !ANDROID && !IOS
91+
// var isBackend = false;
92+
// try
93+
// {
94+
// isBackend = Startup.Instance.HasIPCRoot;
95+
// }
96+
// catch
97+
// {
98+
// }
99+
100+
// if (isBackend)
101+
// {
102+
// builder.AddProvider(new LogConsoleService.Utf8StringLoggerProvider(IPlatformService.IPCRoot.moduleName));
103+
// }
104+
//#endif
89105
};
90106
}
91107

0 commit comments

Comments
ย (0)