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 enum Level
{ {
Trace, Trace,
Debug,
Info, Info,
Warning, Warning,
Error, Error,

View File

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

View File

@@ -9,7 +9,7 @@ using LiteNetLib.Utils;
namespace Engine.Systems.Network; 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 NetPacketProcessor netPacketProcessor = new();
protected readonly PacketCryptor cryptor = new("At4ywW9PGoWH3g==", "NmpMFTvd3pvUbA=="); // TODO implement public key exchange 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 = []; private readonly Dictionary<int, IConnection> localPeerIdToConnectionDictionary = [];
protected ILogger? logger = null;
public IReadOnlyDictionary<string, IConnection> Connections => _connections; public IReadOnlyDictionary<string, IConnection> Connections => _connections;
public EventBasedNetListener Listener { get; private set; } = null!; public EventBasedNetListener Listener { get; private set; } = null!;
public NetManager Manager { get; private set; } = null!; public NetManager Manager { get; private set; } = null!;
@@ -34,16 +32,7 @@ public abstract class LiteNetLibCommunicatorBase : Behaviour, IEnterUniverse, IE
return this; return this;
} }
public virtual void EnterUniverse(IUniverse universe) public virtual void ExitUniverse(IUniverse universe) => Stop();
{
logger = universe.FindBehaviour<ILogger>();
}
public virtual void ExitUniverse(IUniverse universe)
{
logger = null;
Stop();
}
protected virtual void OnPacketArrived<T>(T packet, NetPeer peer) where T : INetworkPacket 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); 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); protected abstract IConnection GetConnection(NetPeer peer);
@@ -80,7 +69,7 @@ public abstract class LiteNetLibCommunicatorBase : Behaviour, IEnterUniverse, IE
localPeerIdToConnectionDictionary.Add(peer.Id, connection); localPeerIdToConnectionDictionary.Add(peer.Id, connection);
_connections.Add(connection.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); OnConnectionEstablished.Invoke(this, connection);
} }
@@ -92,7 +81,7 @@ public abstract class LiteNetLibCommunicatorBase : Behaviour, IEnterUniverse, IE
localPeerIdToConnectionDictionary.Remove(peer.Id); localPeerIdToConnectionDictionary.Remove(peer.Id);
_connections.Remove(connection.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); OnConnectionAbolished.Invoke(this, connection);
} }

View File

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