| | | 1 | | using System; |
| | | 2 | | using System.IO; |
| | | 3 | | |
| | | 4 | | namespace GridForge; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Provides a configurable logging system for GridForge with support for log levels, formatting, and file output. |
| | | 8 | | /// </summary> |
| | | 9 | | public static class GridForgeLogger |
| | | 10 | | { |
| | 1 | 11 | | private static readonly GridForgeDiagnosticLogger _logger = new(); |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Gets the diagnostic channel used by GridForge logging. |
| | | 15 | | /// </summary> |
| | 684 | 16 | | public static DiagnosticChannel Channel => _logger.Channel; |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Gets the diagnostic channel used for verbose debug diagnostics. |
| | | 20 | | /// </summary> |
| | 3 | 21 | | public static DiagnosticChannel DebugChannel => _logger.DebugChannel; |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Gets or sets a value indicating whether verbose debug diagnostics should be emitted. |
| | | 25 | | /// </summary> |
| | | 26 | | public static bool EnableDebugLogging |
| | | 27 | | { |
| | 3 | 28 | | get => _logger.EnableDebugLogging; |
| | 6 | 29 | | set => _logger.EnableDebugLogging = value; |
| | | 30 | | } |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// Gets or sets the delegate used to write formatted log messages. |
| | | 34 | | /// Assigning <see langword="null"/> restores <see cref="DefaultLogHandler"/>. |
| | | 35 | | /// </summary> |
| | | 36 | | public static Action<DiagnosticLevel, string, string> LogHandler |
| | | 37 | | { |
| | 14 | 38 | | get => _logger.LogHandler; |
| | 27 | 39 | | set => _logger.LogHandler = value; |
| | | 40 | | } |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// Gets or sets the formatter used to transform log arguments into a final log entry. |
| | | 44 | | /// Assigning <see langword="null"/> restores <see cref="DefaultLogFormatter"/>. |
| | | 45 | | /// </summary> |
| | | 46 | | public static Func<DiagnosticLevel, string, string, string> CustomFormatter |
| | | 47 | | { |
| | 8 | 48 | | get => _logger.CustomFormatter; |
| | 9 | 49 | | set => _logger.CustomFormatter = value; |
| | | 50 | | } |
| | | 51 | | |
| | | 52 | | /// <summary> |
| | | 53 | | /// Gets or sets the file path for logging. If null, file logging is disabled. |
| | | 54 | | /// </summary> |
| | | 55 | | public static string? LogFilePath |
| | | 56 | | { |
| | 2 | 57 | | get => _logger.LogFilePath; |
| | 4 | 58 | | set => _logger.LogFilePath = value; |
| | | 59 | | } |
| | | 60 | | |
| | | 61 | | /// <summary> |
| | | 62 | | /// Gets or sets the minimum log level required for messages to be logged. |
| | | 63 | | /// </summary> |
| | | 64 | | public static DiagnosticLevel MinimumLevel |
| | | 65 | | { |
| | 14 | 66 | | get => _logger.MinimumLevel; |
| | 29 | 67 | | set => _logger.MinimumLevel = value; |
| | | 68 | | } |
| | | 69 | | |
| | | 70 | | /// <summary> |
| | | 71 | | /// Determines whether diagnostics at the specified level are currently enabled. |
| | | 72 | | /// </summary> |
| | | 73 | | /// <param name="level">The diagnostic level to evaluate.</param> |
| | | 74 | | /// <returns><see langword="true"/> when messages at <paramref name="level"/> will be emitted; otherwise, <see langw |
| | | 75 | | public static bool IsEnabled(DiagnosticLevel level) |
| | | 76 | | { |
| | 0 | 77 | | return _logger.IsEnabled(level); |
| | | 78 | | } |
| | | 79 | | |
| | | 80 | | /// <summary> |
| | | 81 | | /// The default handler for logging messages, writing them to the console and optionally to a file. |
| | | 82 | | /// </summary> |
| | | 83 | | /// <param name="level">The severity level of the log message.</param> |
| | | 84 | | /// <param name="message">The log message.</param> |
| | | 85 | | /// <param name="source">The source of the log message.</param> |
| | | 86 | | public static void DefaultLogHandler(DiagnosticLevel level, string message, string source) |
| | | 87 | | { |
| | 0 | 88 | | _logger.DefaultLogHandler(level, message, source); |
| | 0 | 89 | | } |
| | | 90 | | |
| | | 91 | | /// <summary> |
| | | 92 | | /// The default log formatter that formats log messages with timestamp, log level, and source information. |
| | | 93 | | /// </summary> |
| | | 94 | | /// <param name="level">The severity level of the log message.</param> |
| | | 95 | | /// <param name="message">The log message.</param> |
| | | 96 | | /// <param name="source">The source of the log message.</param> |
| | | 97 | | /// <returns>A formatted log entry as a string.</returns> |
| | | 98 | | public static string DefaultLogFormatter(DiagnosticLevel level, string message, string source) |
| | | 99 | | { |
| | 0 | 100 | | return _logger.DefaultLogFormatter(level, message, source); |
| | | 101 | | } |
| | | 102 | | |
| | | 103 | | private sealed class GridForgeDiagnosticLogger : DiagnosticLogger |
| | | 104 | | { |
| | 1 | 105 | | private readonly object _lock = new(); |
| | | 106 | | |
| | 2 | 107 | | public GridForgeDiagnosticLogger() : base("GridForge") { } |
| | | 108 | | |
| | 28 | 109 | | public string? LogFilePath { get; set; } |
| | | 110 | | |
| | | 111 | | public override void DefaultLogHandler(DiagnosticLevel level, string message, string source) |
| | | 112 | | { |
| | 20 | 113 | | lock (_lock) |
| | | 114 | | { |
| | 20 | 115 | | string logEntry = CustomFormatter(level, message, source); |
| | 20 | 116 | | Console.WriteLine(logEntry); |
| | | 117 | | |
| | 20 | 118 | | if (!string.IsNullOrEmpty(LogFilePath)) |
| | | 119 | | { |
| | | 120 | | try |
| | | 121 | | { |
| | 2 | 122 | | File.AppendAllText(LogFilePath, logEntry + Environment.NewLine); |
| | 1 | 123 | | } |
| | 1 | 124 | | catch (Exception ex) |
| | | 125 | | { |
| | 1 | 126 | | Console.WriteLine($"[ERROR] Failed to write to log file: {ex.Message}"); |
| | 1 | 127 | | } |
| | | 128 | | } |
| | 18 | 129 | | } |
| | 20 | 130 | | } |
| | | 131 | | |
| | | 132 | | public override string DefaultLogFormatter(DiagnosticLevel level, string message, string source) |
| | | 133 | | { |
| | 23 | 134 | | var timestamp = DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss.fff"); |
| | 23 | 135 | | var levelTag = level switch |
| | 23 | 136 | | { |
| | 1 | 137 | | DiagnosticLevel.None => "[NONE]", |
| | 2 | 138 | | DiagnosticLevel.Info => "[INFO]", |
| | 1 | 139 | | DiagnosticLevel.Warning => "[WARN]", |
| | 18 | 140 | | DiagnosticLevel.Error => "[ERROR]", |
| | 1 | 141 | | _ => "[LOG]" |
| | 23 | 142 | | }; |
| | | 143 | | |
| | 23 | 144 | | return $"{timestamp} {levelTag} [{source}] {message}"; |
| | | 145 | | } |
| | | 146 | | } |
| | | 147 | | } |