Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public ContextualLogger(Object inspectorObject, [NotNull] NetworkManager network
}

/// Used for the NetworkLog
internal ContextualLogger(NetworkManager networkManager, bool useCompatibilityMode)
internal ContextualLogger(NetworkManager networkManager, bool useCompatibilityMode = false)
{
m_UseCompatibilityMode = useCompatibilityMode;
m_ManagerContext = new LogContextNetworkManager(networkManager);
Expand Down Expand Up @@ -125,6 +125,19 @@ public void Exception(Exception exception)
{
Debug.unityLogger.LogException(exception, m_Object);
}
[HideInCallstack]
public void Exception(Exception exception, Context context)
{
// Don't act if the LogLevel is higher than the level of this log
if (m_ManagerContext.LogLevel > context.Level)
{
return;
}

var message = BuildLog(context);
Debug.unityLogger.LogException(new Exception(message, exception), context.RelevantObjectOverride ?? m_Object);
}


[HideInCallstack]
private void Log(LogType logType, Context context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public void AppendInfo(object key, object value)
}

public void Append(string value) => m_Builder.Append(value);
public void AppendLine(string value) => m_Builder.AppendLine(value);

public string Build() => m_Builder.ToString();
}
Expand Down
76 changes: 0 additions & 76 deletions com.unity.netcode.gameobjects/Runtime/Logging/LogContext.cs

This file was deleted.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System.Collections.Generic;

namespace Unity.Netcode.Logging
{
internal delegate string LogCollectionBuilder<in TItem>(TItem item);
internal readonly struct CollectionContext<TItem> : ILogContext
{
private readonly LogCollectionBuilder<TItem> m_Delegate;
private readonly IEnumerable<TItem> m_Collection;

public CollectionContext(IEnumerable<TItem> collection, LogCollectionBuilder<TItem> builder)
{
m_Delegate = builder;
m_Collection = collection;
}

public void AppendTo(LogBuilder builder)
{
foreach (var item in m_Collection)
{
builder.AppendLine(m_Delegate(item));
}
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
using System;
using System.Collections.Generic;
using UnityEngine;

namespace Unity.Netcode.Logging
{
internal readonly struct GenericContext : ILogContext, IDisposable
{
private readonly List<string> m_Contexts;
private readonly List<string> m_Tags;
private readonly Dictionary<object, object> m_Info;

private GenericContext(List<string> contexts, Dictionary<object, object> info)
private GenericContext(List<string> tags, Dictionary<object, object> info)
{
m_Contexts = contexts;
m_Tags = tags;
m_Info = info;
}

public void AppendTo(LogBuilder builder)
{
if (m_Contexts != null)
if (m_Tags != null)
{
foreach (var ctx in m_Contexts)
foreach (var ctx in m_Tags)
{
builder.AppendTag(ctx);
}
Expand All @@ -33,21 +34,26 @@ public void AppendTo(LogBuilder builder)
}
}

public void StoreTag(string msg)
public void StoreTag(string tag)
{
m_Contexts.Add(msg);
m_Tags.Add(tag);
}

public void StoreInfo(object key, object value)
{
m_Info.Add(key, value);
}

public void ClearInfo(object key)
public void RemoveInfo(object key)
{
m_Info?.Remove(key);
}

public void RemoveTag(string tag)
{
m_Tags?.Remove(tag);
}

public void Dispose()
{
PreallocatedStore.Free(this);
Expand Down Expand Up @@ -76,7 +82,7 @@ internal static GenericContext GetPreallocated()

internal static void Free(GenericContext ctx)
{
ctx.m_Contexts.Clear();
ctx.m_Tags.Clear();
ctx.m_Info.Clear();
k_Preallocated.Enqueue(ctx);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using Object = UnityEngine.Object;

namespace Unity.Netcode.Logging
{
internal interface ILogContext
{
public void AppendTo(LogBuilder builder);
}

internal struct Context : ILogContext, IDisposable
{
public readonly LogLevel Level;
private readonly string m_CallingFunction;
internal readonly string Message;
internal Object RelevantObjectOverride;

private readonly GenericContext m_Other;
private List<ILogContext> m_Prepend;
private List<ILogContext> m_Postpend;

public Context(LogLevel level, string msg, [CallerMemberName] string memberName = "")
{
Level = level;
Message = msg;
m_CallingFunction = memberName;

m_Other = GenericContext.Create();
RelevantObjectOverride = null;
m_Prepend = null;
m_Postpend = null;
}

internal Context(LogLevel level, string msg, bool noCaller)
{
Level = level;
Message = msg;
m_CallingFunction = null;

m_Other = GenericContext.Create();
RelevantObjectOverride = null;
m_Prepend = null;
m_Postpend = null;
}

public void AppendTo(LogBuilder builder)
{
// [CallingFunction]
if (!string.IsNullOrEmpty(m_CallingFunction))
{
builder.AppendTag(m_CallingFunction);
}


// [SomeContext][SomeName:SomeValue]
m_Other.AppendTo(builder);

if (m_Prepend != null)
{
foreach (var context in m_Prepend)
{
context.AppendTo(builder);
}
}

// Human-readable log message
builder.Append(" ");
builder.Append(Message);

if (m_Postpend != null)
{
foreach (var context in m_Postpend)
{
context.AppendTo(builder);
}
}
}

public Context AddInfo(object key, object value)
{
m_Other.StoreInfo(key, value);
return this;
}

public Context AddTag(string msg)
{
m_Other.StoreTag(msg);
return this;
}

public Context AddObject(Object obj)
{
RelevantObjectOverride = obj;
return this;
}

public Context AddNetworkObject(NetworkObject networkObject)
{
AddPrepend(new LogContextNetworkObject(networkObject));
RelevantObjectOverride = networkObject;
return this;
}

public Context AddNetworkBehaviour(NetworkBehaviour networkBehaviour)
{
AddPrepend(new LogContextNetworkBehaviour(networkBehaviour));
RelevantObjectOverride = networkBehaviour;
return this;
}

public Context AddCollection<TItem>(IEnumerable<TItem> collection, LogCollectionBuilder<TItem> builder)
{
AddPostpend(new CollectionContext<TItem>(collection, builder));
return this;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void AddPrepend(ILogContext prepend)
{
if (m_Prepend == null)
{
m_Prepend = PreallocatedStore.GetPreallocated();
}
m_Prepend.Add(prepend);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void AddPostpend(ILogContext postpend)
{
if (m_Postpend == null)
{
m_Postpend = PreallocatedStore.GetPreallocated();
}
m_Postpend.Add(postpend);
}

public void Dispose()
{
m_Other.Dispose();
PreallocatedStore.Free(m_Prepend);
PreallocatedStore.Free(m_Postpend);
m_Prepend = null;
m_Postpend = null;
}

private static class PreallocatedStore
{
private static readonly Queue<List<ILogContext>> k_Preallocated = new();

internal static List<ILogContext> GetPreallocated()
{
if (k_Preallocated.Count > 0)
{
k_Preallocated.Dequeue();
}

return new List<ILogContext>();
}

internal static void Free(List<ILogContext> collection)
{
collection.Clear();
k_Preallocated.Enqueue(collection);
}
}
}
}
Loading
Loading