Last active
November 1, 2023 18:38
-
-
Save jeffvella/dee1a82bd5edfcdc9d3067d8c038f95c to your computer and use it in GitHub Desktop.
BurstLogger
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// MIT LICENSE Copyright (c) 2020 Jeffrey Vella | |
// https://gist.github.com/jeffvella/dee1a82bd5edfcdc9d3067d8c038f95c/edit | |
using Unity.Burst; | |
using Unity.Collections; | |
using Unity.Collections.LowLevel.Unsafe; | |
using Unity.Entities; | |
using Unity.Jobs.LowLevel.Unsafe; | |
using UnityEngine; | |
public unsafe struct NativeLogger | |
{ | |
public UnsafeList* Threads; | |
public LoggerConfig Config; | |
public LoggerEntry* GetEntryPtr(int threadIndex, int entryIndex) | |
=> (LoggerEntry*)((byte*)Threads[threadIndex].Ptr + sizeof(LoggerEntry) * entryIndex); | |
public struct LoggerConfig | |
{ | |
public int ThreadCount; | |
} | |
public struct LoggerEntry | |
{ | |
public FixedString512 Text; | |
} | |
} | |
public unsafe class BurstLogger | |
{ | |
public static readonly SharedStatic<NativeLogger> Logger = SharedStatic<NativeLogger>.GetOrCreate<Key>(); | |
public static void Debug(FixedString512 template, FixedString512 arg0, int threadIndex = -1) | |
=> Debug(FixedString.Format(template, arg0), threadIndex); | |
public static void Debug(FixedString512 template, FixedString512 arg0, FixedString512 arg1, int threadIndex = -1) | |
=> Debug(FixedString.Format(template, arg0, arg1), threadIndex); | |
public static void Debug(FixedString512 template, FixedString512 arg0, int arg1, int threadIndex = -1) | |
=> Debug(FixedString.Format(template, arg0, arg1), threadIndex); | |
public static void Debug(FixedString512 message, int threadIndex = -1) | |
{ | |
int i = threadIndex == -1 ? Logger.Data.Config.ThreadCount - 1 : threadIndex; | |
Logger.Data.Threads[i].Add(message); | |
} | |
public class Key { } | |
} | |
[UpdateInGroup(typeof(InitializationSystemGroup))] | |
public unsafe class DebugLogSystem : SystemBase | |
{ | |
protected override void OnCreate() | |
{ | |
ref var logger = ref BurstLogger.Logger.Data; | |
logger.Config = new NativeLogger.LoggerConfig | |
{ | |
ThreadCount = JobsUtility.MaxJobThreadCount + 1 | |
}; | |
int containerSize = UnsafeUtility.SizeOf<UnsafeList>(); | |
int elementSize = UnsafeUtility.SizeOf<NativeLogger.LoggerEntry>(); | |
int startingElementCount = 10; | |
logger.Threads = (UnsafeList*)UnsafeUtility.Malloc(containerSize * logger.Config.ThreadCount, 4, Allocator.Persistent); | |
for (int i = 0; i < logger.Config.ThreadCount; i++) | |
{ | |
logger.Threads[i] = new UnsafeList(elementSize, 4, startingElementCount, Allocator.Persistent, NativeArrayOptions.ClearMemory); | |
} | |
} | |
protected override void OnUpdate() | |
{ | |
ref var logger = ref BurstLogger.Logger.Data; | |
for (int i = 0; i < logger.Config.ThreadCount; i++) | |
{ | |
ref var thread = ref logger.Threads[i]; | |
if (thread.Length > 0) | |
{ | |
for (int j = thread.Length - 1; j != -1; j--) | |
{ | |
Debug.Log(logger.GetEntryPtr(i, j)->Text); | |
} | |
Debug.Log($"Clearing threadIndex {i}"); | |
thread.Clear(); | |
} | |
} | |
} | |
} | |
public class LogTester1 : SystemBase | |
{ | |
protected override void OnUpdate() | |
{ | |
Job.WithCode(() => | |
{ | |
BurstLogger.Debug("Wow! hello cruel world?"); | |
}).WithoutBurst().Run(); | |
} | |
} | |
public class LogTester2 : SystemBase | |
{ | |
protected override void OnUpdate() | |
{ | |
FixedString512 message = "Shocking! a scheduled hello!"; | |
Job.WithCode(() => | |
{ | |
BurstLogger.Debug(message); | |
}).Schedule(); | |
} | |
} | |
public class LogTester3 : SystemBase | |
{ | |
private NativeArray<Entity> _entities; | |
private EntityArchetype _archetype; | |
protected override void OnCreate() | |
{ | |
_entities = new NativeArray<Entity>(10, Allocator.Persistent); | |
_archetype = EntityManager.CreateArchetype(ComponentType.ReadWrite<TestLogMessage>()); | |
EntityManager.CreateEntity(_archetype, _entities); | |
for (int i = 0; i < _entities.Length; i++) | |
{ | |
EntityManager.SetComponentData(_entities[i], new TestLogMessage | |
{ | |
Id = $"Message_#{i}" | |
}); | |
} | |
} | |
protected override void OnUpdate() | |
{ | |
Entities.ForEach((int nativeThreadIndex, in TestLogMessage message) => | |
{ | |
BurstLogger.Debug("Amazing! a ScheduleParallel hello world? Id={0} ThreadIndex={1}", message.Id, nativeThreadIndex, nativeThreadIndex); | |
}).ScheduleParallel(); | |
} | |
public struct TestLogMessage : IComponentData | |
{ | |
public FixedString64 Id; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
So with Collections 0.11.0 onwards FixedString.Format uses
FixedString128
.You can change all
FixedString512
instances toFixedString128
, but the easiest way is to just change all the Debug functions to append the args to the template.