Skip to content

Commit

Permalink
logging extension methods
Browse files Browse the repository at this point in the history
  • Loading branch information
drewcassidy committed Nov 9, 2024
1 parent 3c11bfe commit bcf518b
Showing 1 changed file with 72 additions and 12 deletions.
84 changes: 72 additions & 12 deletions include/Log.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,98 @@

namespace KSPBuildTools
{
internal static class Log
{
internal static string Prefix = "[" + Assembly.GetExecutingAssembly().GetName().Name + "] ";
interface ILogContextProvider {
string context();
}
internal static class Log
{
internal static string ModPrefix = "[" + Assembly.GetExecutingAssembly().GetName().Name + "]";

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static string ObjPrefix(this UnityEngine.Object obj)
{
return "[" + obj.name + "]";
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static string ObjPrefix(this ILogContextProvider obj)
{
return "[" + obj.context() + "]";
}

[System.Diagnostics.Conditional("DEBUG")]
internal static void Debug(string message)
internal static void Debug(string message, string prefix = "") {
UnityEngine.Debug.Log("[DEBUG]" + ModPrefix + prefix + " " + message);
}

[System.Diagnostics.Conditional("DEBUG")]
internal static void LogDebug(this UnityEngine.Object obj, string message, string prefix = "") {
UnityEngine.Debug.Log( "[DEBUG]" + ModPrefix + obj.ObjPrefix() + prefix + " " + message, obj);
}

[System.Diagnostics.Conditional("DEBUG")]
internal static void LogDebug(this ILogContextProvider obj, string message, string prefix = "") {
UnityEngine.Debug.Log("[DEBUG]" + ModPrefix + obj.ObjPrefix() + prefix + " " + message);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static void Message(string message, string prefix = "")
{
UnityEngine.Debug.Log(Prefix + message);
UnityEngine.Debug.Log(ModPrefix + prefix + " " + message);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static void Message(string message)
internal static void LogMessage(this UnityEngine.Object obj, string message, string prefix = "")
{
UnityEngine.Debug.Log(Prefix + message);
UnityEngine.Debug.Log(ModPrefix + obj.ObjPrefix() + prefix + " " + message, obj);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static void LogMessage(this ILogContextProvider obj, string message, string prefix = "")
{
UnityEngine.Debug.Log(ModPrefix + obj.ObjPrefix() + prefix + " " + message);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static void Warning(string message)
internal static void Warning(string message, string prefix = "")
{
UnityEngine.Debug.LogWarning(Prefix + message);
UnityEngine.Debug.LogWarning(ModPrefix + prefix + " " + message);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static void Error(string message)
internal static void LogWarning(this UnityEngine.Object obj, string message, string prefix = "")
{
UnityEngine.Debug.LogError(Prefix + message);
UnityEngine.Debug.LogWarning(ModPrefix + obj.ObjPrefix() + prefix + " " + message, obj);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static void LogWarning(this ILogContextProvider obj, string message, string prefix = "")
{
UnityEngine.Debug.LogWarning(ModPrefix + obj.ObjPrefix() + prefix + " " + message);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static void Error(string message, string prefix = "")
{
UnityEngine.Debug.LogError(ModPrefix + prefix + " " + message);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static void LogError(UnityEngine.Object obj, string message, string prefix = "")
{
UnityEngine.Debug.LogError(ModPrefix + obj.ObjPrefix() + prefix + " " + message, obj);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static void LogError(this ILogContextProvider obj, string message, string prefix = "")
{
UnityEngine.Debug.LogError(ModPrefix + obj.ObjPrefix() + prefix + " " + message);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static void Exception(Exception ex)
{
UnityEngine.Debug.LogException(ex);
}
}
}
}

0 comments on commit bcf518b

Please sign in to comment.