diff options
| author | JP Dillingham <jp@dillingham.me> | 2022-03-15 07:36:22 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-03-15 07:36:22 -0500 |
| commit | 0464030d853df284b97c8e12fd1338f9bf9649a3 (patch) | |
| tree | 3967eb7d33f1a073effe98754e5f6c418522f9cd /src | |
| parent | f28d1171d11007bbb931425e246f6d232341163a (diff) | |
| parent | 88850904cb1af6b92b7d9dad50c89fc1899d7d1a (diff) | |
| download | slskd-0464030d853df284b97c8e12fd1338f9bf9649a3.tar.gz | |
Merge pull request #386 from slskd/dnet-logs
Log distributed messages as debug, add distributed state
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Application.cs | 34 | ||||
| -rw-r--r-- | src/slskd/Common/Logging/LogRecord.cs | 1 | ||||
| -rw-r--r-- | src/slskd/Program.cs | 7 | ||||
| -rw-r--r-- | src/slskd/State.cs | 14 | ||||
| -rw-r--r-- | src/slskd/slskd.csproj | 2 |
5 files changed, 51 insertions, 7 deletions
diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs index 618027bc..72478bde 100644 --- a/src/slskd/Application.cs +++ b/src/slskd/Application.cs @@ -136,6 +136,7 @@ namespace slskd Client.Connected += Client_Connected; Client.LoggedIn += Client_LoggedIn; Client.StateChanged += Client_StateChanged; + Client.DistributedNetworkStateChanged += Client_DistributedNetworkStateChanged; Client.DownloadDenied += (e, args) => Log.Information("Download of {Filename} from {Username} was denied: {Message}", args.Filename, args.Username, args.Message); Client.DownloadFailed += (e, args) => Log.Information("Download of {Filename} from {Username} failed", args.Filename, args.Username); } @@ -366,13 +367,13 @@ namespace slskd static LogEventLevel TranslateLogLevel(DiagnosticLevel diagnosticLevel) => diagnosticLevel switch { DiagnosticLevel.Debug => LogEventLevel.Debug, - DiagnosticLevel.Info => LogEventLevel.Information, + DiagnosticLevel.Info => LogEventLevel.Debug, DiagnosticLevel.Warning => LogEventLevel.Warning, DiagnosticLevel.None => default, _ => default, }; - var logger = Loggers.GetOrAdd(sender.GetType().FullName, Log.ForContext("SourceContext", "Soulseek").ForContext("SoulseekContext", sender.GetType().FullName)); + var logger = Loggers.GetOrAdd(sender.GetType().FullName, Log.ForContext("Context", "Soulseek").ForContext("SubContext", sender.GetType().FullName)); logger.Write(TranslateLogLevel(args.Level), "{@Message}", args.Message); } @@ -483,7 +484,34 @@ namespace slskd private void Client_StateChanged(object sender, SoulseekClientStateChangedEventArgs e) { - State.SetValue(state => state with { Server = state.Server with { Address = Client.Address, IPEndPoint = Client.IPEndPoint, State = Client.State, Username = Client.Username } }); + State.SetValue(state => state with + { + Server = state.Server with + { + Address = Client.Address, + IPEndPoint = Client.IPEndPoint, + State = Client.State, + Username = Client.Username, + }, + }); + } + + private void Client_DistributedNetworkStateChanged(object sender, DistributedNetworkInfo e) + { + State.SetValue(state => state with + { + DistributedNetwork = new DistributedNetworkState() + { + BranchLevel = e.BranchLevel, + BranchRoot = e.BranchRoot, + CanAcceptChildren = e.CanAcceptChildren, + ChildLimit = e.ChildLimit, + Children = e.Children.Select(c => c.Username).ToList().AsReadOnly(), + HasParent = e.HasParent, + IsBranchRoot = e.IsBranchRoot, + Parent = e.Parent.Username, + }, + }); } private void Client_TransferProgressUpdated(object sender, TransferProgressUpdatedEventArgs args) diff --git a/src/slskd/Common/Logging/LogRecord.cs b/src/slskd/Common/Logging/LogRecord.cs index 49e00276..0ca3534c 100644 --- a/src/slskd/Common/Logging/LogRecord.cs +++ b/src/slskd/Common/Logging/LogRecord.cs @@ -23,6 +23,7 @@ namespace slskd { public DateTime Timestamp { get; init; } public string Context { get; init; } + public string SubContext { get; init; } public string Level { get; init; } public string Message { get; init; } } diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index 6a399a02..0857ef4d 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -277,11 +277,11 @@ namespace slskd .Enrich.WithProperty("ProcessId", ProcessId) .Enrich.FromLogContext() .WriteTo.Console( - outputTemplate: (OptionsAtStartup.Debug ? "[{SourceContext}] [{SoulseekContext}] " : string.Empty) + "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}") + outputTemplate: (OptionsAtStartup.Debug ? "[{SubContext}] " : string.Empty) + "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}") .WriteTo.Async(config => config.File( Path.Combine(AppDirectory, "logs", $"{AppName}-.log"), - outputTemplate: (OptionsAtStartup.Debug ? "[{SourceContext}] " : string.Empty) + "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}", + outputTemplate: (OptionsAtStartup.Debug ? "[{SubContext}] " : string.Empty) + "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}", rollingInterval: RollingInterval.Day)) .WriteTo.Conditional( e => !string.IsNullOrEmpty(OptionsAtStartup.Logger.Loki), @@ -293,7 +293,8 @@ namespace slskd var record = new LogRecord() { Timestamp = logEvent.Timestamp.LocalDateTime, - Context = logEvent.Properties["SourceContext"].ToString().TrimStart('"').TrimEnd('"'), + Context = logEvent.Properties["Context"].ToString().TrimStart('"').TrimEnd('"'), + SubContext = logEvent.Properties["SubContext"].ToString().TrimStart('"').TrimEnd('"'), Level = logEvent.Level.ToString(), Message = logEvent.RenderMessage().TrimStart('"').TrimEnd('"'), }; diff --git a/src/slskd/State.cs b/src/slskd/State.cs index bd82f8d7..0daa5d76 100644 --- a/src/slskd/State.cs +++ b/src/slskd/State.cs @@ -18,6 +18,7 @@ namespace slskd { using System; + using System.Collections.Generic; using System.Net; using System.Text.Json.Serialization; using slskd.Users; @@ -33,6 +34,7 @@ namespace slskd public bool PendingRestart { get; init; } public bool PendingShareRescan { get; init; } public ServerState Server { get; init; } = new ServerState(); + public DistributedNetworkState DistributedNetwork { get; init; } = new DistributedNetworkState(); public SharedFileCacheState SharedFileCache { get; init; } = new SharedFileCacheState(); public string[] Rooms { get; init; } = Array.Empty<string>(); public User[] Users { get; init; } = Array.Empty<User>(); @@ -61,6 +63,18 @@ namespace slskd public bool IsTransitioning => State.HasFlag(SoulseekClientStates.Connecting) || State.HasFlag(SoulseekClientStates.Disconnecting) || State.HasFlag(SoulseekClientStates.LoggingIn); } + public record DistributedNetworkState + { + public int BranchLevel { get; init; } + public string BranchRoot { get; init; } + public bool CanAcceptChildren { get; init; } + public int ChildLimit { get; init; } + public IReadOnlyCollection<string> Children { get; init; } + public bool HasParent { get; init; } + public bool IsBranchRoot { get; init; } + public string Parent { get; init; } + } + /// <summary> /// Share cache state. /// </summary> diff --git a/src/slskd/slskd.csproj b/src/slskd/slskd.csproj index 4b7ebe1d..70c689b8 100644 --- a/src/slskd/slskd.csproj +++ b/src/slskd/slskd.csproj @@ -69,7 +69,7 @@ <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> </PackageReference> - <PackageReference Include="Soulseek" Version="4.4.5" /> + <PackageReference Include="Soulseek" Version="4.5.0" /> <PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.354"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> |
