5

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

Daxtron2
  • 1,239
  • 1
  • 11
  • 19
Kokizzu
  • 24,974
  • 37
  • 137
  • 233
  • Maybe if you extend the Unity.Debug class and override Log? Not sure if its possible but have you tried it? Or maybe an register the event handler Application.logMessageReceived += HandleLog; to do the extra work when the log message fires up – S.Fragkos Apr 19 '18 at 11:01
  • Your question: how can I double click the log message and go to where my logging call was made? Answer: You can't. – Draco18s no longer trusts SE Apr 19 '18 at 18:18

4 Answers4

2

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

2

//U can use "LogPlayerBuildError" for this

  /// ================================
    /// Checks if the object of type T is null or not, 
    /// If object is null, prints a log message if 'enableLog' set to 'true'
    /// https://answers.unity.com/questions/238229/debugconsole-console-clicking.html
    /// ================================
    public static bool IsNull<T>( this T classType, bool enableLog = true, MonoBehaviour monoInstance = null) where T : class
    {
        if(classType == null)
        {   if(enableLog)
            {   //if(classType.GetType().IsSubclassOf(typeof(MonoBehaviour)))
                var frame = new System.Diagnostics.StackTrace(true).GetFrame(1);

                string fileName = FormatFileName(frame.GetFileName());
                int lineNum = frame.GetFileLineNumber();
                int colomn = frame.GetFileColumnNumber();


                string msg = "WARNING:- The instance of type " + typeof(T) + " is null!"
                    + "\nFILE: " + fileName + " LINE: " + lineNum;

                //Debug.LogWarning(msg, (UnityEngine.Object)monoInstance.gameObject);
                var mUnityLog = typeof(UnityEngine.Debug).GetMethod("LogPlayerBuildError", BindingFlags.NonPublic | BindingFlags.Static);
                mUnityLog.Invoke(null, new object[] { msg, fileName, lineNum, colomn });
            }
            return true;
        }

        return false;
    } 
krishx007
  • 21
  • 4
  • This is in the right direction, doubleclick goes to correct location, but does not show the full stacktrace. – karmington Sep 08 '21 at 22:25
2

Create a .dll of your extended Log classes and remove the source .cs files from the project folder. This will cause Unity to take you to the original code line rather than the helper class when double clicking in the default Unity console.

As outlined by user on Unity Forums here: Hide Method in Stack Trace Log

For a simple "how to .dll" you can follow this tutorial: Unity and Dlls

Recently had to solve this as was having the same frustration with Unity taking me to helper class rather than the source line. Old Post but still high in search engines so hopefully will help someone out.

Benjamin
  • 21
  • 1
1

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.

sonnyb
  • 3,194
  • 2
  • 32
  • 42