Improved logging system.

This commit is contained in:
flash 2025-04-28 12:29:11 +00:00
commit 98d13ebbbb
Signed by: flash
GPG key ID: 2C9C2C574D47FE3E
24 changed files with 202 additions and 142 deletions
SharpChat

View file

@ -1,3 +1,4 @@
using Microsoft.Extensions.Logging;
using SharpChat.Channels;
using SharpChat.Events;
using SharpChat.Messages;
@ -6,6 +7,7 @@ using SharpChat.SockChat;
using SharpChat.SockChat.S2CPackets;
using System.Dynamic;
using System.Net;
using ZLogger;
namespace SharpChat;
@ -14,6 +16,9 @@ public class Context {
public readonly SemaphoreSlim ContextAccess = new(1, 1);
public ILoggerFactory LoggerFactory { get; }
private readonly ILogger Logger;
public SnowflakeGenerator SnowflakeGenerator { get; } = new();
public RandomSnowflake RandomSnowflake { get; }
@ -25,7 +30,9 @@ public class Context {
public Dictionary<string, RateLimiter> UserRateLimiters { get; } = [];
public Dictionary<string, Channel> UserLastChannel { get; } = [];
public Context(MessageStorage msgs) {
public Context(ILoggerFactory logFactory, MessageStorage msgs) {
LoggerFactory = logFactory;
Logger = logFactory.CreateLogger("ctx");
Messages = msgs;
RandomSnowflake = new(SnowflakeGenerator);
}
@ -86,15 +93,15 @@ public class Context {
public async Task Update() {
foreach(Connection conn in Connections)
if(!conn.IsDisposed && conn.HasTimedOut) {
conn.Logger.ZLogInformation($"Nuking connection associated with user {conn.User?.UserId ?? "no-one"}");
conn.Dispose();
Logger.Write($"Nuked connection {conn.Id} associated with {conn.User}.");
}
Connections.RemoveWhere(conn => conn.IsDisposed);
foreach(User user in Users)
if(!Connections.Any(conn => conn.User == user)) {
Logger.Write($"Timing out {user} (no more connections).");
Logger.ZLogInformation($"Timing out user {user.UserId} (no more connections).");
await HandleDisconnect(user, UserDisconnectS2CPacket.Reason.TimeOut);
}
}
@ -140,8 +147,6 @@ public class Context {
UserPermissions? perms = null,
bool silent = false
) {
ArgumentNullException.ThrowIfNull(user);
bool hasChanged = false;
string previousName = string.Empty;
@ -313,8 +318,6 @@ public class Context {
}
public async Task ForceChannel(User user, Channel? chan = null) {
ArgumentNullException.ThrowIfNull(user);
if(chan == null && !UserLastChannel.TryGetValue(user.UserId, out chan))
throw new ArgumentException("no channel???");