Custom debug logging with burst

I would like to use a custom debug log function with burst.

So far, the best way that I can think of doing this in one line is something like this:

{FixedString128 a = $"pathHash {pathHash}, refcount: {pathData.m_refcount}"; jdx.debug.Log (LogEnabled, a);}

(Note: I haven’t actually tested this in Burst yet!)

Is there a better way? I would like to not have to put in multiple lines for each debug log call. Obviously the above it quite ugly and doesn’t start with Log or whatever so obscures code readability a bit. There are good reasons I don’t want to use the standard Unity.Debug.Log.

How long until we can declare the string directly as a parameter rather than declare it outside the parameter list of the function?

I’d be curious to hear your reasoning

After thinking a bit more half the things I’d like to do with a custom log function probably won’t work in burst jobs environment anyway, such as conditional log to file.

The most basic thing I’d like is to be able to use a conditional for the log. I suppose I could use standard Debug.Log and wrap it in an if at the calling site, although it’s a bit tedious.

Maybe not important reasons after all.

So this is what I was kind of expecting. The thing is, you can override the Unity Debug Log and handle it however you want.

You can either
a) A basic version is simply subscribing to the log messages and forwarding them
Application.logMessageReceived
Application.logMessageReceivedThreaded

b) Much more powerful is implementing your own ILogger to replace the default logger completely
https://docs.unity3d.com/ScriptReference/ILogger.html
https://docs.unity3d.com/ScriptReference/Debug-unityLogger.html

For example
Debug.unityLogger = new jdx.debug(Debug.unityLogger);

If you keep a reference to the previous logger, you can filter messages and only forward ones you want to the console.

1 Like

Cool, thanks for the info. I wasn’t aware you could do those things. I’ll take a look.

I think I’ve got a custom logger working with Debug.Log in burst which is good.

The only way I can see to do conditional logging is by entering codes at the start of the string and then processing them in my custom log function. I’d still much rather be able to use my own custom log function directly with customised arguments rather than hacking conditions/verbosity levels into a string. Am I missing something?

It appears that UnityEngine.Debug.Assert (with a string) or LogError also don’t work in Burst, just Debug.Log?