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