Exemplo n.º 1
0
        public static bool TryFormat(object obj, out string formattedString)
        {
            if (obj is UnityEngine.Object)
            {
                formattedString = LogFormat.UnityObject(obj as UnityEngine.Object);
            }
            else if (obj is Exception)
            {
                formattedString = LogFormat.Exception(obj as Exception);
            }
            else if (obj is MemberInfo)
            {
                formattedString = LogFormat.MemberInfo(obj as MemberInfo);
            }
            else if (obj is Assembly)
            {
                formattedString = LogFormat.Assembly(obj as Assembly);
            }
            else if (obj is StackFrame)
            {
                formattedString = LogFormat.StackFrame(obj as StackFrame);
            }
            else
            {
                formattedString = null;
                return(false);
            }

            return(true);
        }
Exemplo n.º 2
0
        private static string RetrieveEditorContextInfo(object context)
        {
            // Do a stack trace in order to find one. Don't do this outside
            // the editor. It's too expensive and might not be supported on
            // some platforms
            //
            // We can skip two frames, since one is this one and the next
            // is definitely a Log method, since FindContext is private.
            System.Diagnostics.StackFrame stackFrame = null;
            try {
                System.Diagnostics.StackTrace   trace  = new System.Diagnostics.StackTrace(2);
                System.Diagnostics.StackFrame[] frames = trace.GetFrames();
                for (int i = 0; i < frames.Length; i++)
                {
                    System.Reflection.MethodBase method = frames[i].GetMethod();
                    Type type          = method.DeclaringType;
                    bool isLoggingType =
                        !string.IsNullOrEmpty(type.Namespace) &&
                        type.Namespace.StartsWith(typeof(Log).Namespace);

                    // Select the first stack frame that is not part of the
                    // logging code, which is defined as everything in the
                    // same namespace as the Log class
                    if (!isLoggingType)
                    {
                        stackFrame = frames[i];
                        break;
                    }
                }
            }
            catch (Exception) {}

            // Select what to display based on the kind of context provided
            if (stackFrame != null)
            {
                return(LogFormat.StackFrame(stackFrame));
            }
            else if (context is UnityEngine.Object)
            {
                return(LogFormat.UnityObject(context as UnityEngine.Object));
            }
            else
            {
                return(null);
            }
        }