diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/slskd/Application.cs | 7 | ||||
| -rw-r--r-- | src/slskd/Common/ConcurrentFixedSizeQueue.cs | 50 | ||||
| -rw-r--r-- | src/slskd/Common/Logging/DelegatingSink.cs | 38 | ||||
| -rw-r--r-- | src/slskd/Common/Logging/LogRecord.cs | 29 | ||||
| -rw-r--r-- | src/slskd/Core/API/Controllers/LogsController.cs | 44 | ||||
| -rw-r--r-- | src/slskd/Core/API/Hubs/ApplicationHub.cs | 1 | ||||
| -rw-r--r-- | src/slskd/Core/API/Hubs/LogsHub.cs | 58 | ||||
| -rw-r--r-- | src/slskd/Program.cs | 23 | ||||
| -rw-r--r-- | src/slskd/Startup.cs | 1 |
9 files changed, 249 insertions, 2 deletions
diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs index dfdd2d78..1027d7f3 100644 --- a/src/slskd/Application.cs +++ b/src/slskd/Application.cs @@ -64,7 +64,8 @@ namespace slskd IRoomService roomService, ISharedFileCache sharedFileCache, IPushbulletService pushbulletService, - IHubContext<ApplicationHub> applicationHub) + IHubContext<ApplicationHub> applicationHub, + IHubContext<LogsHub> logHub) { OptionsAtStartup = optionsAtStartup; @@ -87,6 +88,9 @@ namespace slskd RoomService = roomService; ApplicationHub = applicationHub; + LogHub = logHub; + Program.LogEmitted += (_, log) => LogHub.EmitLogAsync(log); + Client = soulseekClient; Client.DiagnosticGenerated += Client_DiagnosticGenerated; @@ -128,6 +132,7 @@ namespace slskd private IManagedState<State> State { get; } private ITransferTracker TransferTracker { get; set; } private IHubContext<ApplicationHub> ApplicationHub { get; set; } + private IHubContext<LogsHub> LogHub { get; set; } /// <summary> /// Gets the version of the latest application release. diff --git a/src/slskd/Common/ConcurrentFixedSizeQueue.cs b/src/slskd/Common/ConcurrentFixedSizeQueue.cs new file mode 100644 index 00000000..f0420f2a --- /dev/null +++ b/src/slskd/Common/ConcurrentFixedSizeQueue.cs @@ -0,0 +1,50 @@ +// <copyright file="ConcurrentFixedSizeQueue.cs" company="slskd Team"> +// Copyright (c) slskd Team. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published +// by the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see https://www.gnu.org/licenses/. +// </copyright> + +namespace slskd +{ + using System.Collections; + using System.Collections.Concurrent; + using System.Collections.Generic; + + public class ConcurrentFixedSizeQueue<T> : IReadOnlyCollection<T> + { + public ConcurrentFixedSizeQueue(int size) + { + Size = size; + } + + public int Count => Queue.Count; + + private ConcurrentQueue<T> Queue { get; } = new ConcurrentQueue<T>(); + private int Size { get; } + + public void Enqueue(T item) + { + Queue.Enqueue(item); + + if (Queue.Count > Size) + { + Queue.TryDequeue(out _); + } + } + + public IEnumerator<T> GetEnumerator() => Queue.GetEnumerator(); + + IEnumerator IEnumerable.GetEnumerator() => Queue.GetEnumerator(); + } +}
\ No newline at end of file diff --git a/src/slskd/Common/Logging/DelegatingSink.cs b/src/slskd/Common/Logging/DelegatingSink.cs new file mode 100644 index 00000000..75b0e60f --- /dev/null +++ b/src/slskd/Common/Logging/DelegatingSink.cs @@ -0,0 +1,38 @@ +// <copyright file="DelegatingSink.cs" company="slskd Team"> +// Copyright (c) slskd Team. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published +// by the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see https://www.gnu.org/licenses/. +// </copyright> + +namespace slskd +{ + using System; + using Serilog.Core; + using Serilog.Events; + + public class DelegatingSink : ILogEventSink + { + public DelegatingSink(Action<LogEvent> action) + { + Action = action; + } + + private Action<LogEvent> Action { get; } + + public void Emit(LogEvent logEvent) + { + Action(logEvent); + } + } +}
\ No newline at end of file diff --git a/src/slskd/Common/Logging/LogRecord.cs b/src/slskd/Common/Logging/LogRecord.cs new file mode 100644 index 00000000..49e00276 --- /dev/null +++ b/src/slskd/Common/Logging/LogRecord.cs @@ -0,0 +1,29 @@ +// <copyright file="LogRecord.cs" company="slskd Team"> +// Copyright (c) slskd Team. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published +// by the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see https://www.gnu.org/licenses/. +// </copyright> + +namespace slskd +{ + using System; + + public record LogRecord + { + public DateTime Timestamp { get; init; } + public string Context { get; init; } + public string Level { get; init; } + public string Message { get; init; } + } +} diff --git a/src/slskd/Core/API/Controllers/LogsController.cs b/src/slskd/Core/API/Controllers/LogsController.cs new file mode 100644 index 00000000..57366867 --- /dev/null +++ b/src/slskd/Core/API/Controllers/LogsController.cs @@ -0,0 +1,44 @@ +// <copyright file="LogsController.cs" company="slskd Team"> +// Copyright (c) slskd Team. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published +// by the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see https://www.gnu.org/licenses/. +// </copyright> + +namespace slskd.Core.API +{ + using Microsoft.AspNetCore.Authorization; + using Microsoft.AspNetCore.Mvc; + + /// <summary> + /// Logs. + /// </summary> + [Route("api/v{version:apiVersion}/[controller]")] + [ApiVersion("0")] + [ApiController] + [Produces("application/json")] + [Consumes("application/json")] + public class LogsController : ControllerBase + { + /// <summary> + /// Gets the last few application logs. + /// </summary> + /// <returns></returns> + [HttpGet] + [Authorize] + public IActionResult Logs() + { + return Ok(Program.LogBuffer); + } + } +} diff --git a/src/slskd/Core/API/Hubs/ApplicationHub.cs b/src/slskd/Core/API/Hubs/ApplicationHub.cs index 1900a8d0..c02f625f 100644 --- a/src/slskd/Core/API/Hubs/ApplicationHub.cs +++ b/src/slskd/Core/API/Hubs/ApplicationHub.cs @@ -17,7 +17,6 @@ namespace slskd.Core.API { - using System; using System.Threading.Tasks; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.SignalR; diff --git a/src/slskd/Core/API/Hubs/LogsHub.cs b/src/slskd/Core/API/Hubs/LogsHub.cs new file mode 100644 index 00000000..7c14cc17 --- /dev/null +++ b/src/slskd/Core/API/Hubs/LogsHub.cs @@ -0,0 +1,58 @@ +// <copyright file="LogsHub.cs" company="slskd Team"> +// Copyright (c) slskd Team. All rights reserved. +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published +// by the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see https://www.gnu.org/licenses/. +// </copyright> + +namespace slskd.Core.API +{ + using System.Threading.Tasks; + using Microsoft.AspNetCore.Authorization; + using Microsoft.AspNetCore.SignalR; + + public static class LogHubMethods + { + public static readonly string Buffer = "BUFFER"; + public static readonly string Log = "LOG"; + } + + /// <summary> + /// Extension methods for the logs SignalR hub. + /// </summary> + public static class LogHubExtensions + { + /// <summary> + /// Broadcast a log record. + /// </summary> + /// <param name="hub">The hub.</param> + /// <param name="record">The log record to broadcast.</param> + /// <returns>The operation context.</returns> + public static Task EmitLogAsync(this IHubContext<LogsHub> hub, LogRecord record) + { + return hub.Clients.All.SendAsync(LogHubMethods.Log, record); + } + } + + /// <summary> + /// The logs SignalR hub. + /// </summary> + [Authorize] + public class LogsHub : Hub + { + public override async Task OnConnectedAsync() + { + await Clients.Caller.SendAsync(LogHubMethods.Buffer, Program.LogBuffer); + } + } +}
\ No newline at end of file diff --git a/src/slskd/Program.cs b/src/slskd/Program.cs index 9590ed48..b5e628cc 100644 --- a/src/slskd/Program.cs +++ b/src/slskd/Program.cs @@ -84,6 +84,11 @@ namespace slskd public static readonly string XmlDocumentationFile = Path.Combine(AppContext.BaseDirectory, "etc", $"{AppName}.xml"); /// <summary> + /// Occurs when a new log event is emitted. + /// </summary> + public static event EventHandler<LogRecord> LogEmitted; + + /// <summary> /// Gets the assembly version of the application. /// </summary> /// <remarks> @@ -119,6 +124,11 @@ namespace slskd /// </summary> public static bool IsCanary { get; } = InformationalVersion.EndsWith("65534"); + /// <summary> + /// Gets a buffer containing the last few log events. + /// </summary> + public static ConcurrentFixedSizeQueue<LogRecord> LogBuffer { get; } = new ConcurrentFixedSizeQueue<LogRecord>(size: 500); + private static IConfigurationRoot Configuration { get; set; } private static OptionsAtStartup OptionsAtStartup { get; } = new OptionsAtStartup(); @@ -233,6 +243,19 @@ namespace slskd config => config.GrafanaLoki( OptionsAtStartup.Logger.Loki ?? string.Empty, outputTemplate: "[{Timestamp:HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}")) + .WriteTo.Sink(new DelegatingSink(logEvent => + { + var record = new LogRecord() + { + Timestamp = logEvent.Timestamp.LocalDateTime, + Context = logEvent.Properties["SourceContext"].ToString().TrimStart('"').TrimEnd('"'), + Level = logEvent.Level.ToString(), + Message = logEvent.RenderMessage().TrimStart('"').TrimEnd('"'), + }; + + LogBuffer.Enqueue(record); + LogEmitted?.Invoke(null, record); + })) .CreateLogger(); var logger = Log.ForContext(typeof(Program)); diff --git a/src/slskd/Startup.cs b/src/slskd/Startup.cs index 2098ee3c..322d8ed8 100644 --- a/src/slskd/Startup.cs +++ b/src/slskd/Startup.cs @@ -333,6 +333,7 @@ namespace slskd app.UseEndpoints(endpoints => { endpoints.MapHub<ApplicationHub>("/hub/application"); + endpoints.MapHub<LogsHub>("/hub/logs"); endpoints.MapControllers(); endpoints.MapHealthChecks("/health"); |
