|
| 1 | +/* |
| 2 | + * COPYRIGHT: See COPYING in the top level directory |
| 3 | + * PROJECT: <Your Project Name> |
| 4 | + * FILE: DebugLogger.cs |
| 5 | + * PURPOSE: A lightweight wrapper for debug-only logging. |
| 6 | + * Provides zero-cost removal in Release builds. |
| 7 | + * Uses IEngineLogger as its backend, allowing any logging |
| 8 | + * implementation to be plugged in. |
| 9 | + * PROGRAMMER: Peter Geinitz (Wayfarer) |
| 10 | + */ |
| 11 | + |
| 12 | +using System.Diagnostics; |
| 13 | +using System.IO; |
| 14 | +using System.Runtime.CompilerServices; |
| 15 | +using CoreMemoryLog; |
| 16 | + |
| 17 | +/// <summary> |
| 18 | +/// Provides a static, debug-only logging wrapper that is **entirely removed** |
| 19 | +/// from Release builds using <see cref="ConditionalAttribute"/>. |
| 20 | +/// <para> |
| 21 | +/// This allows all engine subsystems (renderer, memory manager, physics, |
| 22 | +/// scene graph, tilemap, etc.) to call logging code without incurring |
| 23 | +/// **any runtime overhead** in Release builds. |
| 24 | +/// </para> |
| 25 | +/// <para> |
| 26 | +/// All logging work and parameter evaluation is skipped at compile time. |
| 27 | +/// In Debug builds, calls are routed to <see cref="Backend"/>, |
| 28 | +/// which can be swapped at runtime. |
| 29 | +/// </para> |
| 30 | +/// </summary> |
| 31 | +public static class DebugLogger |
| 32 | +{ |
| 33 | + /// <summary> |
| 34 | + /// Gets or sets the backend logger instance used for debug logging. |
| 35 | + /// <para> |
| 36 | + /// Must implement <see cref="IEngineLogger"/>. |
| 37 | + /// Defaults to <see cref="InMemoryLogger.Instance"/>. |
| 38 | + /// </para> |
| 39 | + /// <para> |
| 40 | + /// You can replace this with any logger (console, file, network, UI) |
| 41 | + /// during engine initialization, as long as <c>DEBUG</c> is defined. |
| 42 | + /// </para> |
| 43 | + /// <para> |
| 44 | + /// In Release builds, this property has **no effect**, since all debug |
| 45 | + /// logging calls are removed at compile time. |
| 46 | + /// </para> |
| 47 | + /// </summary> |
| 48 | + public static IEngineLogger Backend { get; set; } = InMemoryLogger.Instance; |
| 49 | + |
| 50 | + /// <summary> |
| 51 | + /// Writes a debug-level message to the configured backend. |
| 52 | + /// <para>This method is **compiled out in Release builds**.</para> |
| 53 | + /// </summary> |
| 54 | + /// <param name="message">The log message format string.</param> |
| 55 | + /// <param name="args">Optional format arguments.</param> |
| 56 | + [Conditional("DEBUG")] |
| 57 | + public static void Debug(string message, params object[] args) |
| 58 | + => Backend.LogDebug(message, args); |
| 59 | + |
| 60 | + /// <summary> |
| 61 | + /// Writes a trace-level message to the configured backend. |
| 62 | + /// <para>This method is **compiled out in Release builds**.</para> |
| 63 | + /// </summary> |
| 64 | + /// <param name="message">The log message format string.</param> |
| 65 | + /// <param name="args">Optional format arguments.</param> |
| 66 | + [Conditional("DEBUG")] |
| 67 | + public static void Trace(string message, params object[] args) |
| 68 | + => Backend.LogTrace(message, args); |
| 69 | + |
| 70 | + /// <summary> |
| 71 | + /// Writes an information-level message to the configured backend. |
| 72 | + /// <para>This method is **compiled out in Release builds**.</para> |
| 73 | + /// </summary> |
| 74 | + /// <param name="message">The log message format string.</param> |
| 75 | + /// <param name="args">Optional format arguments.</param> |
| 76 | + [Conditional("DEBUG")] |
| 77 | + public static void Info(string message, params object[] args) |
| 78 | + => Backend.LogInformation(message, args); |
| 79 | + |
| 80 | + /// <summary> |
| 81 | + /// Writes a warning-level message to the configured backend. |
| 82 | + /// <para>This method is **compiled out in Release builds**.</para> |
| 83 | + /// </summary> |
| 84 | + /// <param name="message">The log message format string.</param> |
| 85 | + /// <param name="args">Optional format arguments.</param> |
| 86 | + [Conditional("DEBUG")] |
| 87 | + public static void Warn(string message, params object[] args) |
| 88 | + => Backend.LogWarning(message, args); |
| 89 | + |
| 90 | + /// <summary> |
| 91 | + /// Writes an error-level message to the configured backend. |
| 92 | + /// <para>This method is **compiled out in Release builds**.</para> |
| 93 | + /// </summary> |
| 94 | + /// <param name="message">The log message format string.</param> |
| 95 | + /// <param name="args">Optional format arguments.</param> |
| 96 | + [Conditional("DEBUG")] |
| 97 | + public static void Error(string message, params object[] args) |
| 98 | + => Backend.LogError(message, args); |
| 99 | + |
| 100 | + /// <summary> |
| 101 | + /// Writes a debug-level message including caller metadata |
| 102 | + /// (file name, line number and calling method). |
| 103 | + /// <para> |
| 104 | + /// Useful for debugging the engine’s execution flow, |
| 105 | + /// especially in hot paths where stack traces are too slow. |
| 106 | + /// </para> |
| 107 | + /// <para> |
| 108 | + /// This method is **compiled out in Release**. |
| 109 | + /// </para> |
| 110 | + /// </summary> |
| 111 | + /// <param name="message">The main log message format string.</param> |
| 112 | + /// <param name="caller">Automatically filled with the calling member name.</param> |
| 113 | + /// <param name="file">Automatically filled with the calling source file path.</param> |
| 114 | + /// <param name="line">Automatically filled with the source line number.</param> |
| 115 | + /// <param name="args">Optional format arguments.</param> |
| 116 | + [Conditional("DEBUG")] |
| 117 | + public static void Debug( |
| 118 | + string message, |
| 119 | + [CallerMemberName] string caller = "", |
| 120 | + [CallerFilePath] string file = "", |
| 121 | + [CallerLineNumber] int line = 0, |
| 122 | + params object[] args) |
| 123 | + { |
| 124 | + string prefix = $"{Path.GetFileName(file)}:{line} {caller} → "; |
| 125 | + Backend.LogDebug(prefix + message, args); |
| 126 | + } |
| 127 | +} |
0 commit comments