using System;
using System.Diagnostics;
using System.Reflection;
using System.Text;
using UnityEngine;
public class Log
{
// private static StringBuilder _builder = new StringBuilder(1024);
public static void I(string tag, string format, params object[] args)
{
UnityEngine.Debug.Log(buildString(tag, format, args));
}
public static void W(string tag, string format, params object[] args)
{
UnityEngine.Debug.LogWarning(buildString(tag, format, args));
}
public static void E(string tag, string format, params object[] args)
{
UnityEngine.Debug.LogError(buildString(tag, format, args));
}
private static StringBuilder buildString(string tag, string format, params object[] args)
{
StringBuilder _builder = new StringBuilder(1024);
_builder.Clear();
_builder.Append("Snake_");
_builder.Append(tag);
_builder.Append(": ");
if (args != null && args.Length > 0)
{
_builder.AppendFormat(format, args);
}
else
{
_builder.Append(format);
}
return _builder;
}
}
namespace Unity.Log
{
public static class Xlog
{
private const string c_str_project_name = “Snake”;
public static void Log(this MonoBehaviour mono, string _str_log, bool _b_is_error = false)
{
// 获取传入实例所属类的名称
string className = mono.GetType().Name;
// 获取当前执行的函数的名称
string methodName = new System.Diagnostics.StackTrace().GetFrame(1).GetMethod().Name;
if (_b_is_error)
{
UnityEngine.Debug.LogError($"【{c_str_project_name}】/【{className}】/【{methodName}】:){_str_log}");
}
else
{
UnityEngine.Debug.Log($"【{c_str_project_name}】/【{className}】/【{methodName}】:){_str_log}");
}
}
public static void Log(string _str_log, bool _b_is_error = false)
{
StackTrace stackTrace = new StackTrace();
StackFrame[] stackFrames = stackTrace.GetFrames();
if (stackFrames.Length >= 2)
{
// 获取调用堆栈的第一个方法(当前方法是第二个)
StackFrame callerFrame = stackFrames[1];
MethodBase callerMethod = callerFrame.GetMethod();
Type callerType = callerMethod.DeclaringType;
if (_b_is_error)
{
// 打印调用类和函数的信息
UnityEngine.Debug.LogError($"【Snake】/【{callerType.Name} 】/【{callerMethod.Name}】:){_str_log}");
}
else
{
// 打印调用类和函数的信息
UnityEngine.Debug.Log($"【Snake】/【{callerType.Name} 】/【{callerMethod.Name}】:){_str_log}");
}
}
}
}
}