using UnityEngine;
namespace Unity.Netcode
{
///
/// Helper class for logging
///
public static class NetworkLog
{
///
/// Gets the current log level.
///
/// The current log level.
public static LogLevel CurrentLogLevel => NetworkManager.Singleton == null ? LogLevel.Normal : NetworkManager.Singleton.LogLevel;
// internal logging
///
/// Locally logs a info log with Netcode prefixing.
///
/// The message to log
public static void LogInfo(string message) => Debug.Log($"[Netcode] {message}");
///
/// Locally logs a warning log with Netcode prefixing.
///
/// The message to log
public static void LogWarning(string message) => Debug.LogWarning($"[Netcode] {message}");
///
/// Locally logs a error log with Netcode prefixing.
///
/// The message to log
public static void LogError(string message) => Debug.LogError($"[Netcode] {message}");
///
/// Logs an info log locally and on the server if possible.
///
/// The message to log
public static void LogInfoServer(string message) => LogServer(message, LogType.Info);
///
/// Logs a warning log locally and on the server if possible.
///
/// The message to log
public static void LogWarningServer(string message) => LogServer(message, LogType.Warning);
///
/// Logs an error log locally and on the server if possible.
///
/// The message to log
public static void LogErrorServer(string message) => LogServer(message, LogType.Error);
private static void LogServer(string message, LogType logType)
{
// Get the sender of the local log
ulong localId = NetworkManager.Singleton != null ? NetworkManager.Singleton.LocalClientId : 0;
switch (logType)
{
case LogType.Info:
LogInfoServerLocal(message, localId);
break;
case LogType.Warning:
LogWarningServerLocal(message, localId);
break;
case LogType.Error:
LogErrorServerLocal(message, localId);
break;
}
if (NetworkManager.Singleton != null && !NetworkManager.Singleton.IsServer && NetworkManager.Singleton.NetworkConfig.EnableNetworkLogs)
{
var networkMessage = new ServerLogMessage
{
LogType = logType,
Message = message
};
var size = NetworkManager.Singleton.SendMessage(ref networkMessage, NetworkDelivery.ReliableFragmentedSequenced, NetworkManager.ServerClientId);
NetworkManager.Singleton.NetworkMetrics.TrackServerLogSent(NetworkManager.ServerClientId, (uint)logType, size);
}
}
internal static void LogInfoServerLocal(string message, ulong sender) => Debug.Log($"[Netcode-Server Sender={sender}] {message}");
internal static void LogWarningServerLocal(string message, ulong sender) => Debug.LogWarning($"[Netcode-Server Sender={sender}] {message}");
internal static void LogErrorServerLocal(string message, ulong sender) => Debug.LogError($"[Netcode-Server Sender={sender}] {message}");
internal enum LogType : byte
{
Info,
Warning,
Error,
None
}
}
}