Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to wrap UnityEngine.Debug.Log but keeping the line of code when clicked

Tags:

c#

unity3d

I have this kind of code, with purpose is to wrap UnityEngine.Debug.Log so I can disable them all on production also so that I can look/filter up later.

using System;

public enum LogType
{
    DEBUG,
    CRITICAL
}

public class LogHelper
{
    public static void Log(LogType lt, string format, params object[] objs)
    {
        if (lt == LogType.CRITICAL)
        {
            //            StackTrace st = new StackTrace(new StackFrame(true));
            //            Console.WriteLine(" Stack trace for current level: {0}", st.ToString());
            //            StackFrame sf = st.GetFrame(0);
            //            Console.WriteLine(" File: {0}", sf.GetFileName());
            //            Console.WriteLine(" Method: {0}", sf.GetMethod().Name);
            //            Console.WriteLine(" Line Number: {0}", sf.GetFileLineNumber());
            //            Console.WriteLine(" Column Number: {0}", sf.GetFileColumnNumber());
        }
        // TODO: write to /tmp file too
        UnityEngine.Debug.Log("[" + lt + "] " + String.Format(format, objs));
    }

    public static void Critical(string format, params object[] objs)
    {
        Log(LogType.CRITICAL,format, objs);
    }
    public static void Debug(string format, params object[] objs)
    {
        Log(LogType.DEBUG,format, objs);
    }
}

The problem is, when i call those LogHelper.Debug("something"), the Unity Editor's Log when double clicked will go to that code (one that calls UnityEngine.Debug.Log) instead of the source that call that LogHelper.Debug. How to make it show the caller instead of the LogHelper when I doubleclick the log?

one that doubleclicked

like image 519
Kokizzu Avatar asked Jan 29 '23 06:01

Kokizzu


2 Answers

I am not sure, but try:

public static class MyDebug{    
public static delegate void TestDelegate(object message);
#if (NOEDITOR)
    public static TestDelegate Log =(x)=>{};
#else
    public static TestDelegate Log = Debug.Log;
#endif
}

then, control this defining NOEDITOR

like image 192
Mykola Zhyhallo Avatar answered Jan 31 '23 20:01

Mykola Zhyhallo


One possible solution is to use Editor Console Pro from the Unity Asset Store.

Here are some of the listed features that are relevant to your question (emphasis mine):

  • See the source code surrounding each method call in the stack, allowing you to see and jump to the code around the log.
  • Open your code editor to any method or line in a log's stack by clicking on it, rather than just to the Debug.Log call. [...]
  • Ignore custom Debug.Log classes in the stack, so double clicking never takes you to the wrong code.

However, it's not a free package (though the price is reasonable and it has excellent reviews).

You could also write your own Unity editor extension to implement something similar to Editor Console Pro using UnityEngine.Application.logMessageReceivedThreaded.

like image 34
sonny Avatar answered Jan 31 '23 20:01

sonny