Skip to content

Commit 329762a

Browse files
committed
fixes based off of code review
1 parent 182aff7 commit 329762a

4 files changed

Lines changed: 36 additions & 23 deletions

File tree

com.unity.netcode.gameobjects/Runtime/Logging/ContextualLogger.cs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,20 @@ namespace Unity.Netcode.Logging
1818
/// </summary>
1919
internal class ContextualLogger
2020
{
21+
private const string k_CompilationCondition = "UNITY_ASSERTIONS";
22+
2123
private const string k_NetcodeHeader = "[Netcode] ";
22-
private readonly bool m_UseCompatibilityMode;
2324
private readonly Object m_Object;
2425
private readonly LogBuilder m_Builder = new();
2526

2627
private LogContextNetworkManager m_ManagerContext;
2728
private readonly GenericContext m_LoggerContext;
2829

29-
private const string k_CompilationCondition = "UNITY_ASSERTIONS";
30+
/// <summary>
31+
/// Compatibility mode with the old behavior of NetworkLog
32+
/// TODO: remove this when enough of the codebase is using the new log system
33+
/// </summary>
34+
private readonly bool m_UseCompatibilityMode;
3035

3136
/// <summary>
3237
/// Creates a minimally configured contextual logger
@@ -54,11 +59,13 @@ public ContextualLogger(Object inspectorObject, [NotNull] NetworkManager network
5459
m_LoggerContext = GenericContext.Create();
5560
}
5661

57-
[Conditional(k_CompilationCondition)]
58-
internal void UpdateNetworkManagerContext(NetworkManager manager)
62+
/// Used for the NetworkLog
63+
internal ContextualLogger(NetworkManager networkManager, bool useCompatibilityMode)
5964
{
60-
m_ManagerContext.Dispose();
61-
m_ManagerContext = new LogContextNetworkManager(manager);
65+
m_UseCompatibilityMode = useCompatibilityMode;
66+
m_ManagerContext = new LogContextNetworkManager(networkManager);
67+
m_Object = networkManager;
68+
m_LoggerContext = GenericContext.Create();
6269
}
6370

6471
[Conditional(k_CompilationCondition)]

com.unity.netcode.gameobjects/Runtime/Logging/NetworkLog.cs

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,24 @@ internal struct LogConfiguration
1717
public static class NetworkLog
1818
{
1919
internal static LogConfiguration Config;
20-
private static readonly ContextualLogger k_Log = new(true);
20+
private static ContextualLogger s_Log = new(true);
2121

22-
internal static void SetNetworkManager(NetworkManager networkManager)
22+
/// <summary>
23+
/// Configures the NetworkLog for integration tests.
24+
/// </summary>
25+
internal static void ConfigureIntegrationTestLogging(NetworkManager networkManager, bool enableVerboseDebug = false)
2326
{
24-
k_Log.UpdateNetworkManagerContext(networkManager);
27+
// useCompatibilityMode when verboseDebug is not enabled
28+
s_Log = new ContextualLogger(networkManager, !enableVerboseDebug);
29+
// This setting will do nothing if the logger is created with useCompatibilityMode=true
30+
Config.LogNetworkManagerRole = enableVerboseDebug;
2531
}
32+
2633
/// <summary>
2734
/// Gets the current log level.
2835
/// </summary>
2936
/// <value>The current log level.</value>
30-
// [Obsolete("Use the LogLevel directly on the NetworkManager instead")]
37+
// TODO: Work on deprecating this field.
3138
public static LogLevel CurrentLogLevel => NetworkManager.Singleton == null ? LogLevel.Normal : NetworkManager.Singleton.LogLevel;
3239

3340
// internal logging
@@ -37,27 +44,27 @@ internal static void SetNetworkManager(NetworkManager networkManager)
3744
/// </summary>
3845
/// <param name="message">The message to log</param>
3946
[HideInCallstack]
40-
public static void LogInfo(string message) => k_Log.Info(new Context(LogLevel.Normal, message, true));
47+
public static void LogInfo(string message) => s_Log.Info(new Context(LogLevel.Normal, message, true));
4148
[HideInCallstack]
42-
internal static void LogInfo(Context context) => k_Log.Info(context);
49+
internal static void LogInfo(Context context) => s_Log.Info(context);
4350

4451
/// <summary>
4552
/// Locally logs a warning log with Netcode prefixing.
4653
/// </summary>
4754
/// <param name="message">The message to log</param>
4855
[HideInCallstack]
49-
public static void LogWarning(string message) => k_Log.Warning(new Context(LogLevel.Error, message, true));
56+
public static void LogWarning(string message) => s_Log.Warning(new Context(LogLevel.Error, message, true));
5057
[HideInCallstack]
51-
internal static void LogWarning(Context context) => k_Log.Warning(context);
58+
internal static void LogWarning(Context context) => s_Log.Warning(context);
5259

5360
/// <summary>
5461
/// Locally logs a error log with Netcode prefixing.
5562
/// </summary>
5663
/// <param name="message">The message to log</param>
5764
[HideInCallstack]
58-
public static void LogError(string message) => k_Log.Error(new Context(LogLevel.Error, message, true));
65+
public static void LogError(string message) => s_Log.Error(new Context(LogLevel.Error, message, true));
5966
[HideInCallstack]
60-
internal static void LogError(Context context) => k_Log.Error(context);
67+
internal static void LogError(Context context) => s_Log.Error(context);
6168

6269
// internal static void Log(LogLevel level, object message, Object gameObject) => Logger.Log($"[Netcode] {message} ({(int)level})");
6370

@@ -66,28 +73,28 @@ internal static void SetNetworkManager(NetworkManager networkManager)
6673
/// </summary>
6774
/// <param name="message">The message to log</param>
6875
[HideInCallstack]
69-
public static void LogInfoServer(string message) => k_Log.InfoServer(new Context(LogLevel.Normal, message, true));
76+
public static void LogInfoServer(string message) => s_Log.InfoServer(new Context(LogLevel.Normal, message, true));
7077

7178
/// <summary>
7279
/// Logs an info log locally and on the session owner if possible.
7380
/// </summary>
7481
/// <param name="message">The message to log</param>
7582
[HideInCallstack]
76-
public static void LogInfoSessionOwner(string message) => k_Log.InfoServer(new Context(LogLevel.Normal, message, true));
83+
public static void LogInfoSessionOwner(string message) => s_Log.InfoServer(new Context(LogLevel.Normal, message, true));
7784

7885
/// <summary>
7986
/// Logs a warning log locally and on the server if possible.
8087
/// </summary>
8188
/// <param name="message">The message to log</param>
8289
[HideInCallstack]
83-
public static void LogWarningServer(string message) => k_Log.WarningServer(new Context(LogLevel.Error, message, true));
90+
public static void LogWarningServer(string message) => s_Log.WarningServer(new Context(LogLevel.Error, message, true));
8491

8592
/// <summary>
8693
/// Logs an error log locally and on the server if possible.
8794
/// </summary>
8895
/// <param name="message">The message to log</param>
8996
[HideInCallstack]
90-
public static void LogErrorServer(string message) => k_Log.ErrorServer(new Context(LogLevel.Error, message, true));
97+
public static void LogErrorServer(string message) => s_Log.ErrorServer(new Context(LogLevel.Error, message, true));
9198

9299
internal static LogType GetMessageLogType(UnityEngine.LogType engineLogType)
93100
{

com.unity.netcode.gameobjects/Tests/Runtime/NetworkObject/NetworkObjectDestroyTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ public IEnumerator TestNetworkObjectClientDestroy([Values] ClientDestroyObject c
152152
// The non-authority client is =NOT= allowed to destroy any spawned object it does not
153153
// have authority over during runtime.
154154
LogAssert.ignoreFailingMessages = true;
155-
NetworkLog.SetNetworkManager(nonAuthorityClient);
155+
NetworkLog.ConfigureIntegrationTestLogging(nonAuthorityClient);
156156
Object.Destroy(clientPlayerClone.gameObject);
157157
}
158158

com.unity.netcode.gameobjects/Tests/Runtime/TestHelpers/NetcodeIntegrationTest.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,6 @@ private void InternalOnOneTimeSetup()
594594
IntegrationTestSceneHandler.VerboseDebugMode = m_EnableVerboseDebug;
595595
NetworkManagerHelper.VerboseDebugMode = m_EnableVerboseDebug;
596596
VerboseDebug($"Entering {nameof(OneTimeSetup)}");
597-
// NetworkLog.Config.LogNetworkManagerRole = true;
598597

599598
m_NetworkManagerInstatiationMode = OnSetIntegrationTestMode();
600599

@@ -809,7 +808,7 @@ protected void CreateServerAndClients(int numberOfClients)
809808
m_NumberOfClients = numberOfClients;
810809
m_ClientNetworkManagers = clients;
811810
m_ServerNetworkManager = server;
812-
NetworkLog.SetNetworkManager(server);
811+
NetworkLog.ConfigureIntegrationTestLogging(server, m_EnableVerboseDebug);
813812

814813
var managers = clients.ToList();
815814
if (!m_UseCmbService)

0 commit comments

Comments
 (0)