Forest_Client/Forest/Assets/PhxhSDK/Phxh/Others/DebugUtil.cs

235 lines
6.0 KiB
C#

using System;
using System.Diagnostics;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Text;
#if UNITY_EDITOR
using UnityEditor;
using UnityEditorInternal;
#endif
using Debug = UnityEngine.Debug;
public static class DebugUtil
{
public static bool LogEnable
{
get;
set;
} = true;
[Conditional("UNITY_EDITOR")]
public static void EditorAssert(bool test, string assertString)
{
#if UNITY_EDITOR
if (!test)
{
var trace = new StackTrace(true);
var frame = trace.GetFrame(1);
string assertInformation;
assertInformation = "Filename: " + frame.GetFileName() + "\n";
assertInformation += "Method: " + frame.GetMethod() + "\n";
assertInformation += "Line: " + frame.GetFileLineNumber();
Debug.Break();
var assertMessage = assertString + "\n\n" + assertInformation;
if (EditorUtility.DisplayDialog("Assert!", assertMessage, "OK"))
{
InternalEditorUtility.OpenFileAtLineExternal(frame.GetFileName(), frame.GetFileLineNumber());
Debug.Log(assertInformation);
}
}
#endif
}
[Conditional("DEBUG_LOG")]
[Conditional("DEVELOPMENT_BUILD")]
[Conditional("UNITY_EDITOR")]
public static void Log(object obj, params object[] args)
{
#if DEBUG_LOG || DEVELOPMENT_BUILD || UNITY_EDITOR
if (LogEnable)
{
var msg = obj == null ? "NULL" : obj.ToString();
Log(msg, args);
}
#endif
}
[Conditional("DEBUG_LOG")]
[Conditional("DEVELOPMENT_BUILD")]
[Conditional("UNITY_EDITOR")]
public static void Log(string str, params object[] args)
{
#if DEBUG_LOG || DEVELOPMENT_BUILD || UNITY_EDITOR
if (LogEnable)
{
if (args != null && args.Length > 0) str = GetLogString(str, args);
Debug.Log("[I]> " + str);
}
#endif
}
public static void LogWarning(string str, params object[] args)
{
if (LogEnable)
{
if (args != null && args.Length > 0) str = GetLogString(str, args);
Debug.LogWarning("[W]> " + str);
}
}
public static void LogError(object obj, params object[] args)
{
if (LogEnable)
{
var msg = obj == null ? "NULL" : obj.ToString();
LogError(msg, args);
}
}
public static void LogError(string str, params object[] args)
{
if (LogEnable)
{
if (args != null && args.Length > 0) str = GetLogString(str, args);
Debug.LogError("[E]> " + str);
}
}
public static void LogError(string str)
{
if (LogEnable)
{
str = GetLogString(str);
Debug.LogError("[E]> " + str);
}
}
public static void LogError(string str, object args1)
{
if (LogEnable)
{
if (args1 != null) str = GetLogString(str, args1);
Debug.LogError("[E]> " + str);
}
}
public static void LogError(string str, object args1, object args2)
{
if (LogEnable)
{
if (args1 != null || args2 != null) str = GetLogString(str, args1, args2);
Debug.LogError("[E]> " + str);
}
}
public static void LogError(string str, object args1, object args2, object args3)
{
if (LogEnable)
{
if (args1 != null || args2 != null || args3 != null) str = GetLogString(str, args1, args2, args3);
Debug.LogError("[E]> " + str);
}
}
private static string GetLogString(string str, params object[] args)
{
var sb = new StringBuilder();
var now = DateTime.Now;
sb.Append(now.ToString("HH:mm:ss.fff", CultureInfo.CreateSpecificCulture("en-US")));
sb.Append(" ");
sb.AppendFormat(str, args);
return sb.ToString();
}
private static string GetLogString(string str)
{
var sb = new StringBuilder();
var now = DateTime.Now;
sb.Append(now.ToString("HH:mm:ss.fff", CultureInfo.CreateSpecificCulture("en-US")));
sb.Append(" ");
sb.Append(str);
return sb.ToString();
}
private static string GetLogString(string str, object args1)
{
var sb = new StringBuilder();
var now = DateTime.Now;
sb.Append(now.ToString("HH:mm:ss.fff", CultureInfo.CreateSpecificCulture("en-US")));
sb.Append(" ");
sb.AppendFormat(str, args1);
return sb.ToString();
}
private static string GetLogString(string str, object args1, object args2)
{
var sb = new StringBuilder();
var now = DateTime.Now;
sb.Append(now.ToString("HH:mm:ss.fff", CultureInfo.CreateSpecificCulture("en-US")));
sb.Append(" ");
sb.AppendFormat(str, args1, args2);
return sb.ToString();
}
private static string GetLogString(string str, object args1, object args2, object args3)
{
var sb = new StringBuilder();
var now = DateTime.Now;
sb.Append(now.ToString("HH:mm:ss.fff", CultureInfo.CreateSpecificCulture("en-US")));
sb.Append(" ");
sb.AppendFormat(str, args1, args2, args3);
return sb.ToString();
}
//------Colorful Log-----
public static void LogG(string str, [CallerMemberName] string caller = "")
{
#if DEBUG_LOG || DEVELOPMENT_BUILD || UNITY_EDITOR
if (LogEnable)
Debug.Log($"<color=#00BB00>({caller}) {str}</color>");
#endif
}
public static void LogP(string str, [CallerMemberName] string caller = "")
{
#if DEBUG_LOG || DEVELOPMENT_BUILD || UNITY_EDITOR
if (LogEnable)
Debug.Log($"<color=#ff99ff>({caller}) {str}</color>");
#endif
}
public static void LogY(string str, [CallerMemberName] string caller = "")
{
#if DEBUG_LOG || DEVELOPMENT_BUILD || UNITY_EDITOR
if (LogEnable)
Debug.Log($"<color=#FFFF66>({caller}) {str}</color>");
#endif
}
}