chore: added debug entry for logs

This commit is contained in:
2026-04-14 10:07:51 +03:00
parent 7e7b910dd3
commit 3295701664
5 changed files with 21 additions and 30 deletions

View File

@@ -11,6 +11,7 @@ public interface ILogger
enum Level
{
Trace,
Debug,
Info,
Warning,
Error,

View File

@@ -5,13 +5,16 @@ namespace Engine.Core.Debug;
public static class LoggerExtensions
{
public static void Log<T>(this ILogger logger, T caller, string message, ILogger.Level level = ILogger.Level.Info, bool force = false)
public static void Log<T>(this ILogger logger, T caller, string message, ILogger.Level level, bool force = false)
{
string body = $"{caller?.GetType().Name ?? typeof(T).Name}: {message}";
string body = $"[{caller?.GetType().Name ?? typeof(T).Name}] {message}";
logger.Log(body, level, force);
}
public static void LogWarning<T>(this ILogger logger, T caller, string message, bool force = false)
public static void LogDebug<T>(this ILogger logger, T caller, string message, bool force = false)
=> Log(logger, caller, message, ILogger.Level.Debug, force);
public static void LogInfo<T>(this ILogger logger, T caller, string message, bool force = false)
=> Log(logger, caller, message, ILogger.Level.Info, force);
public static void LogError<T>(this ILogger logger, T caller, string message, bool force = false)

View File

@@ -8,7 +8,7 @@ using Engine.Core.Debug;
namespace Engine.Systems.Network;
public class LiteNetLibClient : LiteNetLibCommunicatorBase, INetworkCommunicatorClient
public class LiteNetLibClient : LiteNetLibCommunicatorBase, INetworkCommunicatorClient, IEnterUniverse
{
private readonly NetDataWriter netDataWriter = new();
private readonly NetDataWriter netDataWriterEncrypted = new();
@@ -31,8 +31,8 @@ public class LiteNetLibClient : LiteNetLibCommunicatorBase, INetworkCommunicator
System.Net.IPAddress[] addresses = System.Net.Dns.GetHostAddresses(address);
string connectionAddress = addresses.FirstOrDefault(a => a.AddressFamily == AddressFamily.InterNetwork, addresses[0]).ToString();
logger?.Log(this, $"Connecting to server at '{address}:{port}' with password '{password}'");
logger?.Log(this, $"Resolved address for {address}: {connectionAddress}");
ILogger.Shared.LogInfo(this, $"Connecting to server at '{address}:{port}' with password '{password}'");
ILogger.Shared.LogInfo(this, $"Resolved address for {address}: {connectionAddress}");
Manager.Start();
Manager.Connect(connectionAddress, port, password);
@@ -70,10 +70,8 @@ public class LiteNetLibClient : LiteNetLibCommunicatorBase, INetworkCommunicator
return this;
}
public override void EnterUniverse(IUniverse universe)
public void EnterUniverse(IUniverse universe)
{
base.EnterUniverse(universe);
cancellationTokenSource = new CancellationTokenSource();
PollEvents(cancellationTokenSource.Token);
}
@@ -94,7 +92,7 @@ public class LiteNetLibClient : LiteNetLibCommunicatorBase, INetworkCommunicator
{
Thread.Sleep(1);
try { Manager.PollEvents(); }
catch (Exception e) { logger?.LogException(this, e, force: true); }
catch (Exception e) { ILogger.Shared.LogException(this, e, force: true); }
}
}, cancellationToken);
}

View File

@@ -9,7 +9,7 @@ using LiteNetLib.Utils;
namespace Engine.Systems.Network;
public abstract class LiteNetLibCommunicatorBase : Behaviour, IEnterUniverse, IExitUniverse, INetworkCommunicator
public abstract class LiteNetLibCommunicatorBase : Behaviour, IExitUniverse, INetworkCommunicator
{
protected readonly NetPacketProcessor netPacketProcessor = new();
protected readonly PacketCryptor cryptor = new("At4ywW9PGoWH3g==", "NmpMFTvd3pvUbA=="); // TODO implement public key exchange
@@ -19,8 +19,6 @@ public abstract class LiteNetLibCommunicatorBase : Behaviour, IEnterUniverse, IE
private readonly Dictionary<int, IConnection> localPeerIdToConnectionDictionary = [];
protected ILogger? logger = null;
public IReadOnlyDictionary<string, IConnection> Connections => _connections;
public EventBasedNetListener Listener { get; private set; } = null!;
public NetManager Manager { get; private set; } = null!;
@@ -34,16 +32,7 @@ public abstract class LiteNetLibCommunicatorBase : Behaviour, IEnterUniverse, IE
return this;
}
public virtual void EnterUniverse(IUniverse universe)
{
logger = universe.FindBehaviour<ILogger>();
}
public virtual void ExitUniverse(IUniverse universe)
{
logger = null;
Stop();
}
public virtual void ExitUniverse(IUniverse universe) => Stop();
protected virtual void OnPacketArrived<T>(T packet, NetPeer peer) where T : INetworkPacket
{
@@ -69,7 +58,7 @@ public abstract class LiteNetLibCommunicatorBase : Behaviour, IEnterUniverse, IE
netPacketProcessor.ReadAllPackets(reader, peer);
}
catch (Exception exception) { logger?.LogException(this, exception, force: true); }
catch (Exception exception) { ILogger.Shared.LogException(this, exception, force: true); }
}
protected abstract IConnection GetConnection(NetPeer peer);
@@ -80,7 +69,7 @@ public abstract class LiteNetLibCommunicatorBase : Behaviour, IEnterUniverse, IE
localPeerIdToConnectionDictionary.Add(peer.Id, connection);
_connections.Add(connection.Id, connection);
logger?.Log(this, $"Connection established with ip '{peer.Address}' and id '{connection.Id}'");
ILogger.Shared.LogInfo(this, $"Connection established with ip '{peer.Address}' and id '{connection.Id}'");
OnConnectionEstablished.Invoke(this, connection);
}
@@ -92,7 +81,7 @@ public abstract class LiteNetLibCommunicatorBase : Behaviour, IEnterUniverse, IE
localPeerIdToConnectionDictionary.Remove(peer.Id);
_connections.Remove(connection.Id);
logger?.Log(this, $"Connection abolished with ip '{peer.Address}' and id '{connection.Id}'");
ILogger.Shared.LogInfo(this, $"Connection abolished with ip '{peer.Address}' and id '{connection.Id}'");
OnConnectionAbolished.Invoke(this, connection);
}

View File

@@ -28,8 +28,8 @@ public class LiteNetLibServer : LiteNetLibCommunicatorBase, INetworkCommunicator
private void OnConnectionRequest(ConnectionRequest request)
{
logger?.Log(this, $"Connection request from ip {request.RemoteEndPoint}");
logger?.Log(this, $"Current connection count: {Connections.Count}");
ILogger.Shared.LogInfo(this, $"Connection request from ip {request.RemoteEndPoint}");
ILogger.Shared.LogInfo(this, $"Current connection count: {Connections.Count}");
if (Manager.ConnectedPeersCount < MaxConnectionCount)
request.AcceptIfKey(Password);
else
@@ -45,8 +45,8 @@ public class LiteNetLibServer : LiteNetLibCommunicatorBase, INetworkCommunicator
MaxConnectionCount = maxConnectionCount;
Port = port;
logger?.Log(this, $"Starting server on port '{port}' with password '{Password}' and max connection count '{maxConnectionCount}'");
logger?.Log(this, $"Server status: {(Manager.Start(port) ? "Active" : "Failed")}");
ILogger.Shared.LogInfo(this, $"Starting server on port '{port}' with password '{Password}' and max connection count '{maxConnectionCount}'");
ILogger.Shared.LogInfo(this, $"Server status: {(Manager.Start(port) ? "Active" : "Failed")}");
return this;
}