도구의발견

[Unity] 콜스택 알아 오기

kukuta 2018. 9. 19. 14:07

유니티를 사용하다 보면 디버깅을 위해 로그를 찍을때 리스트 뷰에서는 콜스택에 대한 정보를 볼수 없어 귀찮은 경우가 많다. 어셋 스토에서 판매하고 있는 'Console Pro'라는 어셋을 사용하면 편리하긴하지만..

using UnityEngine;

// example :
//     Debug.Log(LogHeader.Function + "any log text will be here");
// result :    
//    CallStackLogMain:func_2() (at Assets/CallStackLogMain.cs:17)    
//    any log text will be here

public static class LogHeader 
{
    public static string Path 
    {        
        get	
        {
#if UNITY_EDITOR || USE_DEBUGGING
            string callStack = StackTraceUtility.ExtractStackTrace();
            callStack = callStack.Substring(callStack.IndexOf("\n") + 1);
            callStack = callStack.Substring(0, callStack.IndexOf("\n") + 1);
            return callStack;
#else
            return "";
#endif
        }    
    }    
    
    public static string Function    
    {
        get
        {
#if UNITY_EDITOR || USE_DEBUGGING
            string callStack = StackTraceUtility.ExtractStackTrace();
            callStack = callStack.Substring(callStack.IndexOf("\n") + 1);
            callStack = callStack.Substring(0, callStack.IndexOf("("));
            return "[" + callStack + "] ";
#else
            return "";
#endif        
        }
    }    
}