Skip to content

Instantly share code, notes, and snippets.

@CheyiLin
Last active September 29, 2020 07:37
Show Gist options
  • Save CheyiLin/e4b9e3ca4e6559c525e0 to your computer and use it in GitHub Desktop.
Save CheyiLin/e4b9e3ca4e6559c525e0 to your computer and use it in GitHub Desktop.
//#define LOG_DEBUG
//#define LOG_THREAD_STATE
using UnityEngine;
using System;
using System.Text;
using System.Threading;
using System.Collections;
using System.Reflection;
using System.Diagnostics;
namespace TitanUtil {
public static class Log {
private static StringBuilder output_sb(string fmt, params object[] args) {
StringBuilder sb = new StringBuilder();
#if LOG_THREAD_STATE
sb.Append(Thread.CurrentThread.ThreadState.ToString()[0]);
sb.Append(Thread.CurrentThread.ManagedThreadId.ToString("X2"));
sb.Append(" [");
#else
sb.Append("[");
#endif
MethodBase method = new StackFrame(2).GetMethod();
sb.Append(method.DeclaringType.Name);
sb.Append("] ");
sb.Append(method.Name);
sb.Append(": ");
sb.Append((args.Length == 0) ? fmt : String.Format(fmt, args));
return sb;
}
[Conditional("LOG_DEBUG")]
public static void Debug(string fmt, params object[] args) {
UnityEngine.Debug.Log(output_sb(fmt, args).ToString());
}
public static void Info(string fmt, params object[] args) {
UnityEngine.Debug.Log(output_sb(fmt, args).ToString());
}
public static void Warn(string fmt, params object[] args) {
UnityEngine.Debug.LogWarning(output_sb(fmt, args).ToString());
}
public static void Error(string fmt, params object[] args) {
UnityEngine.Debug.LogError(output_sb(fmt, args).ToString());
}
public static void Exception(Exception ex, string fmt, params object[] args) {
UnityEngine.Debug.LogError(output_sb(fmt, args).ToString());
UnityEngine.Debug.LogException(ex);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment