-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
=> Added a public getter to know if Debug logs are enabled. => Adjusted Logger so that it can be used by mods.
- Loading branch information
Showing
1 changed file
with
175 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,127 +1,232 @@ | ||
namespace QModManager.Utility | ||
{ | ||
using System; | ||
using System.Diagnostics; | ||
using System.IO; | ||
|
||
internal static class Logger | ||
/// <summary>A simple logging class. Can be used for basic logging or to know which logging level is enabled.</summary> | ||
public static class Logger | ||
{ | ||
internal enum Level | ||
/// <summary>Possible logging levels.</summary> | ||
public enum Level | ||
{ | ||
/// <summary>Debugging log level</summary> | ||
Debug, | ||
/// <summary>Informational log level</summary> | ||
Info, | ||
/// <summary>Warning log level</summary> | ||
Warn, | ||
/// <summary>Error log level</summary> | ||
Error, | ||
/// <summary>Fatal log level</summary> | ||
Fatal | ||
} | ||
|
||
private static void Log(string logLevel, params string[] text) | ||
{ | ||
if (text == null || text.Length < 1) | ||
return; | ||
|
||
string from; | ||
Type classType = GetCallingClass(); | ||
private const string AssemblyName = "QModManager"; | ||
|
||
if (classType == null) | ||
from = null; | ||
else if (classType.Namespace.Contains("SMLHelper")) | ||
from = "SMLHelper"; | ||
else | ||
from = classType.Name; | ||
#region Private functions (Used by the Logger) | ||
|
||
string toWrite = "[QModManager] "; | ||
if (!string.IsNullOrEmpty(from) && !from.Contains("<>")) | ||
toWrite += $"[{from}] "; | ||
else if (!string.IsNullOrEmpty(from) && from.Contains("<>")) | ||
toWrite += $"[Anonymous] "; | ||
if (!string.IsNullOrEmpty(logLevel)) | ||
toWrite += $"[{logLevel}] "; | ||
private static string GetCallingAssemblyName() => ReflectionHelper.CallingAssemblyByStackTrace()?.GetName().Name; | ||
|
||
int length = toWrite.Length; | ||
private static void Debug(string msg, bool showOnScreen = false, string callingAssembly = null, bool force = false) | ||
{ | ||
if (!force && !Config.EnableDebugLogs) | ||
return; | ||
|
||
Console.WriteLine($"{toWrite}{text[0]}"); | ||
Console.WriteLine($"[{callingAssembly ?? GetCallingAssemblyName()}:DEBUG] {msg}"); | ||
|
||
for (int i = 1; i < text.Length; i++) | ||
Console.WriteLine($"{text[i]}"); | ||
if (showOnScreen) | ||
ErrorMessage.AddDebug(msg); | ||
} | ||
|
||
internal static void Log(params string[] text) | ||
private static void Debug(bool selfAssembly = false, params string[] msgs) | ||
{ | ||
Log("", text); | ||
if (Config.EnableDebugLogs && msgs != null && msgs.Length > 0) | ||
{ | ||
string callingAssembly = selfAssembly ? AssemblyName : GetCallingAssemblyName(); | ||
foreach (string msg in msgs) | ||
Debug(msg, false, callingAssembly, false); | ||
} | ||
} | ||
|
||
internal static void Log(Level logLevel, params string[] text) | ||
private static void DebugForce(bool selfAssembly = false, params string[] msgs) | ||
{ | ||
switch (logLevel) | ||
if (msgs != null && msgs.Length > 0) | ||
{ | ||
case Level.Debug: | ||
Debug(text); | ||
break; | ||
case Level.Info: | ||
Info(text); | ||
break; | ||
case Level.Warn: | ||
Warn(text); | ||
break; | ||
case Level.Error: | ||
Error(text); | ||
break; | ||
case Level.Fatal: | ||
Fatal(text); | ||
break; | ||
string callingAssembly = selfAssembly ? AssemblyName : GetCallingAssemblyName(); | ||
foreach (string msg in msgs) | ||
Debug(msg, false, callingAssembly, true); | ||
} | ||
} | ||
|
||
internal static void Debug(params string[] text) | ||
private static void Info(string msg, bool showOnScreen = false, string callingAssembly = null) | ||
{ | ||
if (Config.EnableDebugLogs) | ||
Log("Debug", text); | ||
Console.WriteLine($"[{callingAssembly ?? GetCallingAssemblyName()}:INFO] {msg}"); | ||
|
||
if (showOnScreen) | ||
ErrorMessage.AddMessage(msg); | ||
} | ||
|
||
internal static void DebugForce(params string[] text) | ||
private static void Info(bool selfAssembly = false, params string[] msgs) | ||
{ | ||
Log("Debug", text); | ||
if (msgs != null && msgs.Length > 0) | ||
{ | ||
string callingAssembly = selfAssembly ? AssemblyName : GetCallingAssemblyName(); | ||
foreach (string msg in msgs) | ||
Info(msg, false, callingAssembly); | ||
} | ||
} | ||
|
||
internal static void Info(params string[] text) | ||
private static void Warn(string msg, bool showOnScreen = false, string callingAssembly = null) | ||
{ | ||
Log("Info", text); | ||
Console.WriteLine($"[{callingAssembly ?? GetCallingAssemblyName()}:WARN] {msg}"); | ||
|
||
if (showOnScreen) | ||
ErrorMessage.AddWarning(msg); | ||
} | ||
|
||
internal static void Warn(params string[] text) | ||
private static void Warn(bool selfAssembly = false, params string[] msgs) | ||
{ | ||
Log("Warn", text); | ||
if (msgs != null && msgs.Length > 0) | ||
{ | ||
string callingAssembly = selfAssembly ? AssemblyName : GetCallingAssemblyName(); | ||
foreach (string msg in msgs) | ||
Warn(msg, false, callingAssembly); | ||
} | ||
} | ||
|
||
private static void Error(string msg = null, Exception ex = null, bool showOnScreen = false, string callingAssembly = null) | ||
{ | ||
if (ex != null) | ||
msg = (string.IsNullOrEmpty(msg) ? ex.ToString() : msg + Environment.NewLine + ex.ToString()); | ||
|
||
Console.WriteLine($"[{callingAssembly ?? GetCallingAssemblyName()}:ERROR] {msg}"); | ||
|
||
if (showOnScreen && !string.IsNullOrEmpty(msg)) | ||
ErrorMessage.AddError(msg); | ||
} | ||
|
||
internal static void Error(params string[] text) | ||
private static void Error(bool selfAssembly = false, params string[] msgs) | ||
{ | ||
Log("Error", text); | ||
if (msgs != null && msgs.Length > 0) | ||
{ | ||
string callingAssembly = selfAssembly ? AssemblyName : GetCallingAssemblyName(); | ||
foreach (string msg in msgs) | ||
Error(msg, null, false, callingAssembly); | ||
} | ||
} | ||
|
||
internal static void Exception(Exception e) | ||
private static void Exception(Exception e, bool selfAssembly = false) => Error(null, e, false, selfAssembly ? AssemblyName : GetCallingAssemblyName()); | ||
|
||
private static void Fatal(string msg = null, Exception ex = null, bool showOnScreen = false, string callingAssembly = null) | ||
{ | ||
Log("Exception", e.ToString()); | ||
if (ex != null) | ||
msg = (string.IsNullOrEmpty(msg) ? ex.ToString() : msg + Environment.NewLine + ex.ToString()); | ||
Console.WriteLine($"[{callingAssembly ?? GetCallingAssemblyName()}:FATAL] {msg}"); | ||
|
||
if (showOnScreen && !string.IsNullOrEmpty(msg)) | ||
ErrorMessage.AddError(msg); | ||
} | ||
|
||
internal static void Fatal(params string[] text) | ||
private static void Fatal(bool selfAssembly = false, params string[] msgs) | ||
{ | ||
Log("Fatal", text); | ||
if (msgs != null && msgs.Length > 0) | ||
{ | ||
string callingAssembly = selfAssembly ? AssemblyName : GetCallingAssemblyName(); | ||
foreach (string msg in msgs) | ||
Fatal(msg, null, false, callingAssembly); | ||
} | ||
} | ||
|
||
private static Type GetCallingClass() | ||
#endregion | ||
|
||
#region Internal functions (used by QModManager) | ||
|
||
internal static void Debug(params string[] msgs) => Debug(true, msgs); | ||
|
||
internal static void DebugForce(params string[] msgs) => DebugForce(true, msgs); | ||
|
||
internal static void Info(params string[] msgs) => Info(true, msgs); | ||
|
||
internal static void Warn(params string[] msgs) => Warn(true, msgs); | ||
|
||
internal static void Error(params string[] msgs) => Error(true, msgs); | ||
|
||
internal static void Exception(Exception e) => Exception(e, true); | ||
|
||
internal static void Fatal(params string[] msgs) => Fatal(true, msgs); | ||
|
||
#endregion | ||
|
||
#region Public functions (used by mods and QModManager) | ||
|
||
/// <summary>Used to know if debug logging is enabled or not.</summary> | ||
public static bool DebugLogsEnabled => Config.EnableDebugLogs; | ||
|
||
/// <summary> | ||
/// This function will log given message and/or exception. It can optionally show the message on screen. | ||
/// Note: You need to provide a message and/or an exception (this function will do nothing if both are set to null). | ||
/// </summary> | ||
/// <param name="logLevel">The level of the log.</param> | ||
/// <param name="msg">Optional: The message that needs to be logged.</param> | ||
/// <param name="ex">Optional: The exception that needs to be logged.</param> | ||
/// <param name="showOnScreen">Optional: Whether to show the message on screen or not.</param> | ||
public static void Log(Level logLevel, string msg = null, Exception ex = null, bool showOnScreen = false) | ||
{ | ||
var stackTrace = new StackTrace(); | ||
StackFrame[] frames = stackTrace.GetFrames(); | ||
if (ex != null) | ||
{ | ||
// If exception was provided, concatenate its message to the log message | ||
if (logLevel != Level.Error && logLevel != Level.Fatal) | ||
msg = (msg == null) ? ex.Message : msg + Environment.NewLine + ex.Message; | ||
} | ||
else if (msg == null) // Return if both given message and exception were null | ||
return; | ||
|
||
foreach (StackFrame stackFrame in frames) | ||
switch (logLevel) | ||
{ | ||
Type declaringClass = stackFrame.GetMethod().DeclaringType; | ||
if (declaringClass != typeof(Logger)) | ||
return declaringClass; | ||
case Level.Debug: | ||
Debug(msg, showOnScreen); | ||
break; | ||
case Level.Warn: | ||
Warn(msg, showOnScreen); | ||
break; | ||
case Level.Error: | ||
Error(msg, ex, showOnScreen); | ||
break; | ||
case Level.Fatal: | ||
Fatal(msg, ex, showOnScreen); | ||
break; | ||
default: // Defaults to informational logging | ||
Info(msg, showOnScreen); | ||
break; | ||
} | ||
} | ||
|
||
return null; | ||
/// <summary> | ||
/// This function will log given array of strings (each string of the array is logged as an individual log line). | ||
/// </summary> | ||
/// <param name="logLevel">The level of the log.</param> | ||
/// <param name="text">The array of strings that needs to be logged (each string will be logged individually).</param> | ||
public static void Log(Level logLevel, params string[] text) | ||
{ | ||
switch (logLevel) | ||
{ | ||
case Level.Debug: | ||
Debug(false, text); | ||
break; | ||
case Level.Warn: | ||
Warn(false, text); | ||
break; | ||
case Level.Error: | ||
Error(false, text); | ||
break; | ||
case Level.Fatal: | ||
Fatal(false, text); | ||
break; | ||
default: // Defaults to informational logging | ||
Info(false, text); | ||
break; | ||
} | ||
} | ||
|
||
#endregion | ||
} | ||
} |