Skip to content

Commit

Permalink
Feature/ipc console log (#3463)
Browse files Browse the repository at this point in the history
* ๐ŸŽจ LogConsole

* ๐Ÿ› ref

* ๐Ÿ’„ ProxyLog UI

* ๐Ÿ› WebApplication Logging

* ๐Ÿ› Misc

* ๐ŸŽจ Log format

* ๐ŸŽจ LogConsoleService.ConsoleMaxCharCount

* ๐Ÿ› Misc

---------

Co-authored-by: Aigio Liu <[email protected]>
  • Loading branch information
rmbadmin and AigioL authored Aug 30, 2024
1 parent 6a3c8f6 commit 3574725
Show file tree
Hide file tree
Showing 18 changed files with 636 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@
<Compile Include="..\BD.WTTS.Client\Services\Platform\IPlatformService.OS.Linux.Etc.Os_Release.cs">
<LinkBase>Services\Platform</LinkBase>
</Compile>
<Compile Include="..\BD.WTTS.Client\Services\Mvvm\LogConsoleService.Logger.cs">
<LinkBase>Services\Mvvm</LinkBase>
</Compile>
</ItemGroup>

<ItemGroup>
Expand Down Expand Up @@ -104,6 +107,7 @@
<PackageReference Include="System.Drawing.Common" />
<PackageReference Include="System.Runtime.Caching" />
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" />
<PackageReference Include="Utf8StringInterpolation" />
</ItemGroup>

<ItemGroup Condition="$([MSBuild]::IsOSPlatform('windows')) AND ($(RuntimeIdentifier.StartsWith('win-')) OR $(RuntimeIdentifier) == '')">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ static void ConfigureServices(IServiceCollection services)
#if DEBUG
l.AddConsole();
#endif
l.AddProvider(new LogConsoleService.Utf8StringLoggerProvider(moduleName));
});

// ่ฎพ็ฝฎไป“ๅ‚จๅฑ‚ๆ•ฐๆฎๅบ“ๆ–‡ไปถๅญ˜ๆ”พ่ทฏๅพ„
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public IReadOnlyCollection<ScriptIPCDTO>? Scripts

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

public string? GetLogAllMessage() => impl().GetLogAllMessage();

public async Task<StartProxyResult> StartProxyAsync(byte[] reverseProxySettings)
{
var result = await impl().StartProxyAsync(reverseProxySettings);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ StartProxyResult StartProxyCore()
WebRootPath = RootPath,
});

builder.Logging.AddProvider(new LogConsoleService.Utf8StringLoggerProvider(AssemblyInfo.Accelerator));

builder.Services.Configure<HostFilteringOptions>(static o =>
{
o.AllowEmptyHosts = true;
Expand Down Expand Up @@ -167,6 +169,12 @@ public async Task StopProxyAsync()
return bytes;
}

public string? GetLogAllMessage()
{
var result = LogConsoleService.Builder;
return result.ToString();
}

// IDisposable

protected override void DisposeCore()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ partial interface IReverseProxyService
/// </summary>
/// <returns></returns>
byte[]? GetFlowStatistics_Bytes();

/// <summary>
/// ่Žทๅ–ๆ‰€ๆœ‰ๆ—ฅๅฟ—ไฟกๆฏ
/// </summary>
/// <returns></returns>
string? GetLogAllMessage();
}

public static partial class ReverseProxyServiceExtensions
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Data;
using Avalonia.Threading;
using Avalonia.VisualTree;
using LiveChartsCore;
using LiveChartsCore.Motion;
using LiveChartsCore.SkiaSharpView;
using LiveChartsCore.SkiaSharpView.Avalonia;
using LiveChartsCore.SkiaSharpView.Drawing;
using SkiaSharp;

namespace BD.WTTS.UI.Views.Controls;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<UserControl
x:Class="BD.WTTS.UI.Views.Controls.ProxyLog"
xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:AvaloniaEdit="using:AvaloniaEdit"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:s="https://steampp.net/services"
xmlns:spp="https://steampp.net/ui"
d:DesignHeight="450"
d:DesignWidth="800"
mc:Ignorable="d">
<Border Background="{DynamicResource CardBackgroundFillColorDefaultBrush}" CornerRadius="{DynamicResource ControlCornerRadius}">
<AvaloniaEdit:TextEditor
x:Name="LogTextbox"
HorizontalScrollBarVisibility="Disabled"
IsReadOnly="True"
ShowLineNumbers="True" />
</Border>
</UserControl>
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
using Avalonia.Controls;
using Avalonia.Threading;
using Avalonia.VisualTree;
using AvaloniaEdit;
using AvaloniaEdit.TextMate;
using Org.BouncyCastle.Math;
using TextMateSharp.Grammars;

namespace BD.WTTS.UI.Views.Controls;

public partial class ProxyLog : UserControl
{
CancellationTokenSource cancellation = new();

public ProxyLog()
{
InitializeComponent();

var logTextbox = this.FindControl<TextEditor>("LogTextbox")!;

//Here we initialize RegistryOptions with the theme we want to use.
var registryOptions = new TextMateSharp.Grammars.RegistryOptions(ThemeName.Dark);

//Initial setup of TextMate.
var textMateInstallation = logTextbox.InstallTextMate(registryOptions);

//Here we are getting the language by the extension and right after that we are initializing grammar with this language.
//And that's all ๐Ÿ˜€, you are ready to use AvaloniaEdit with syntax highlighting!
textMateInstallation.SetGrammar(registryOptions.GetScopeByLanguageId(registryOptions.GetLanguageByExtension(".log").Id));

//logTextbox.Text = LogText;
//logTextbox.ScrollToLine(logTextbox.Document.LineCount);

logTextbox.TextChanged += LogTextbox_TextChanged;

ProxyService.Current.WhenAnyValue(x => x.ProxyStatus)
.Subscribe(x =>
{
if (x)
{
cancellation = new CancellationTokenSource();
Task2.InBackground(FlushLogsAsync, true);
}
else
{
if (cancellation != null)
{
cancellation.Cancel();
cancellation.Dispose();
}
}
});
}

private void LogTextbox_TextChanged(object? sender, EventArgs e)
{
LogTextbox.ScrollToLine(LogTextbox.Document.LineCount);
}

void FlushLogsAsync()
{
while (!cancellation.IsCancellationRequested)
{
try
{
var isAttachedToVisualTree = this.IsAttachedToVisualTree();
if (isAttachedToVisualTree)
{
var logtext = IReverseProxyService.Constants.Instance.GetLogAllMessage();
if (string.IsNullOrEmpty(logtext))
{
Thread.Sleep(1000);
continue;
}

Dispatcher.UIThread.Post(() =>
{
if (LogTextbox.Text.Length != logtext.Length)
LogTextbox.Text = logtext;
});
}
}
catch
{
}
finally
{
Thread.Sleep(1000);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,7 @@
</Border>
</WrapPanel>-->
</TabStripItem>
<TabStripItem Content="{x:Static spp:Strings.CommunityFix_ProxyLog}" IsVisible="{Binding Source={x:Static s:ProxyService.Current}, Path=ProxyStatus, Mode=OneWay}" />
<TabStripItem Content="{x:Static spp:Strings.CommunityFix_FlowStatistics}" IsVisible="{Binding Source={x:Static s:ProxyService.Current}, Path=ProxyStatus, Mode=OneWay}" />
<TabStripItem Content="{x:Static spp:Strings.CommunityFix_Services}" IsVisible="{Binding Source={x:Static s:ProxyService.Current}, Path=ProxyStatus, Mode=OneWay}" />
<TabStripItem Name="GameAccTab">
Expand Down Expand Up @@ -557,6 +558,8 @@
</ItemsControl>
</spp:ContentLoader>
</ScrollViewer>
<!-- ๅŠ ้€Ÿๆ—ฅๅฟ— -->
<spp:ProxyLog Margin="5,0" />

<!-- ๆต้‡็ปŸ่ฎก -->
<spp:ProxyChartView Margin="5,0" />
Expand Down
16 changes: 16 additions & 0 deletions src/BD.WTTS.Client/App/IApplication.Log.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,22 @@ public static Action<ILoggingBuilder> ConfigureLogging(LogLevel minLevel = Defau
//#elif MACOS || MACCATALYST || (IOS && DEBUG)
// builder.AddProvider(Logging.OSLogLoggerProvider.Instance);
//#endif

//#if !ANDROID && !IOS
// var isBackend = false;
// try
// {
// isBackend = Startup.Instance.HasIPCRoot;
// }
// catch
// {
// }

// if (isBackend)
// {
// builder.AddProvider(new LogConsoleService.Utf8StringLoggerProvider(IPlatformService.IPCRoot.moduleName));
// }
//#endif
};
}

Expand Down
1 change: 1 addition & 0 deletions src/BD.WTTS.Client/BD.WTTS.Client.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@
<PackageReference Include="SkiaSharp" />
<PackageReference Include="SkiaSharp.HarfBuzz" />
<PackageReference Include="HarfBuzzSharp" />
<PackageReference Include="Utf8StringInterpolation" />
</ItemGroup>

<ItemGroup Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) != 'windows'">
Expand Down
10 changes: 2 additions & 8 deletions src/BD.WTTS.Client/Logging/ClientLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,8 @@ namespace BD.WTTS.Logging;
/// </summary>
public abstract class ClientLogger : ILogger
{
static readonly string _messagePadding;
static readonly string _newLineWithMessagePadding;

static ClientLogger()
{
_messagePadding = new string(' ', 6);
_newLineWithMessagePadding = Environment.NewLine + _messagePadding;
}
static readonly string _messagePadding = new string(' ', 6);
static readonly string _newLineWithMessagePadding = Environment.NewLine + _messagePadding;

protected readonly string name;

Expand Down
9 changes: 9 additions & 0 deletions src/BD.WTTS.Client/Resources/Strings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/BD.WTTS.Client/Resources/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -2611,6 +2611,9 @@
<data name="CommunityFix_AlreadyProxy" xml:space="preserve">
<value>ๅทฒๅŠ ้€Ÿ </value>
<comment>Comment=ๆœซๅฐพๅธฆ็ฉบๆ ผ;</comment>
</data>
<data name="CommunityFix_ProxyLog" xml:space="preserve">
<value>ๅŠ ้€Ÿๆ—ฅๅฟ—</value>
</data>
<data name="CommunityFix_FlowStatistics" xml:space="preserve">
<value>ๆต้‡็ปŸ่ฎก</value>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,13 @@ public async Task<bool> ExitModules(IEnumerable<string> moduleNames)
return result.All(static x => x.result);
}

public void WriteMessage(string? moduleName, byte[] bytes)
{
#if !ANDROID && !IOS
LogConsoleService.Current.WriteMessage(moduleName, bytes);
#endif
}

/// <summary>
/// ้…็ฝฎๆœๅŠก
/// </summary>
Expand Down
2 changes: 2 additions & 0 deletions src/BD.WTTS.Client/Services/IPC/IPCMainProcessService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,6 @@ public interface IPCMainProcessService : IAsyncDisposable
/// <param name="moduleName"></param>
/// <returns></returns>
ValueTask<T?> GetServiceAsync<T>(string moduleName) where T : class;

void WriteMessage(string? moduleName, byte[] bytes);
}
Loading

0 comments on commit 3574725

Please sign in to comment.