Last active
December 6, 2022 21:40
-
-
Save mathias-bevers/6579c5a397c70ba061e57c1f90ffbc9f to your computer and use it in GitHub Desktop.
C#-console debug class. Basic console logs with some extra tags ang colors to make debugging a bit easier on the eyes.
This file contains hidden or 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
| using System.Runtime.CompilerServices; | |
| using System.Linq; | |
| using System; | |
| #pragma warning disable CS8625 | |
| namespace Mathias | |
| { | |
| /// <summary> | |
| /// A helper class to make logging information to the console easier. | |
| /// <see href="https://gist.github.com/mathias-bevers/6579c5a397c70ba061e57c1f90ffbc9f">Source</see> | |
| /// </summary> | |
| //todo: add null checks | |
| public static class Debug | |
| { | |
| private const int UNIT_TEST_SPACING = 20; | |
| /// <summary> | |
| /// Log a <paramref name="message" /> to the console with a red "[ERROR]" tag in front of it. | |
| /// </summary> | |
| /// <param name="message">The text that has to be displayed in the console</param> | |
| public static void LogError(object message, [CallerLineNumber] int lineNumber = 0, [CallerFilePath] string callerPath = null) | |
| { | |
| message ??= "null"; | |
| Console.ForegroundColor = ConsoleColor.Red; | |
| string fileName = callerPath.Split('\\').Last(); | |
| Console.Write($"[ERROR {fileName}:{lineNumber}] "); | |
| Console.ResetColor(); | |
| Console.WriteLine(message.ToString()); | |
| } | |
| /// <summary> | |
| /// Log a <paramref name="message" /> to the console with a gray-ish "[INFO]" tag in front of it. | |
| /// </summary> | |
| /// <param name="message">The text that has to be displayed in the console</param> | |
| public static void Log(object message, [CallerLineNumber] int lineNumber = 0, [CallerFilePath] string callerPath = null) | |
| { | |
| message ??= "null"; | |
| Console.ForegroundColor = ConsoleColor.DarkGray; | |
| string fileName = callerPath.Split('\\').Last(); | |
| Console.Write($"[INFO {fileName}:{lineNumber}] "); | |
| Console.ResetColor(); | |
| Console.WriteLine(message.ToString()); | |
| } | |
| /// <summary> | |
| /// Log a <paramref name="message" /> to the console with a yellow "[WARNING]" tag in front of it. | |
| /// </summary> | |
| /// <param name="message">The text that has to be displayed in the console</param> | |
| public static void LogWaring(object message, [CallerLineNumber] int lineNumber = 0, [CallerFilePath] string callerPath = null) | |
| { | |
| message ??= "null"; | |
| Console.ForegroundColor = ConsoleColor.Yellow; | |
| string fileName = callerPath.Split('\\').Last(); | |
| Console.Write($"[WARNING {fileName}:{lineNumber}] "); | |
| Console.ResetColor(); | |
| Console.WriteLine(message.ToString()); | |
| } | |
| /// <summary> | |
| /// Log a <paramref name="message" /> with a blue "[INITIALIZED]" tag in front of it and the | |
| /// <paramref name="elapsedTime" /> in milliseconds it took to initialize afterwards. | |
| /// </summary> | |
| /// <param name="message">The text that has to be displayed in the console</param> | |
| /// <param name="elapsedTime">The time it took to initialize the object.</param> | |
| public static void Initialized(object initializedObject, double elapsedTime) | |
| { | |
| if (initializedObject == null) | |
| { | |
| Debug.LogError("argument for \'initializedObject\' cannot be null"); | |
| return; | |
| } | |
| Console.ForegroundColor = ConsoleColor.Blue; | |
| Console.Write("[INITIALIZED] "); | |
| Console.ResetColor(); | |
| if (initializedObject is string) | |
| { | |
| Console.WriteLine($"{initializedObject} in {elapsedTime}ms..."); | |
| return; | |
| } | |
| Console.WriteLine($"{initializedObject.GetType().Name} in {elapsedTime}ms..."); | |
| } | |
| /// <summary> | |
| /// Log a <paramref name="message" /> to the console with a DarkMagenta "[UNIT-TEST]" tag in front of it and the show | |
| /// in color whether the unit test has passed or failed. | |
| /// </summary> | |
| /// <param name="message">The text that has to be displayed in the console</param> | |
| public static void UnitTest(object obj, bool condition) | |
| { | |
| string message = (obj == null || obj.ToString() == null) ? "null" : obj.ToString()!; | |
| Console.ForegroundColor = ConsoleColor.DarkMagenta; | |
| Console.Write("[UNIT-TEST] "); | |
| Console.ResetColor(); | |
| Console.Write(message); | |
| string result = string.Empty; | |
| if (condition) | |
| { | |
| Console.ForegroundColor = ConsoleColor.Green; | |
| result = "Passed!"; | |
| } | |
| else | |
| { | |
| Console.ForegroundColor = ConsoleColor.Red; | |
| result = "Failed!"; | |
| } | |
| string whitespace = new (' ', Console.WindowWidth - message.Length - UNIT_TEST_SPACING); | |
| Console.WriteLine(whitespace + result); | |
| Console.ResetColor(); | |
| } | |
| } | |
| } | |
| #pragma warning restore CS8625 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment