| | | 1 | | using System; |
| | | 2 | | using System.IO; |
| | | 3 | | |
| | | 4 | | namespace SwitchBlade.Core |
| | | 5 | | { |
| | | 6 | | public class Logger : SwitchBlade.Contracts.ILogger |
| | | 7 | | { |
| | 166 | 8 | | public static bool IsDebugEnabled { get; set; } = false; |
| | 1 | 9 | | private static readonly object _lock = new object(); |
| | | 10 | | |
| | | 11 | | // Singleton instance for static bridge |
| | 7 | 12 | | public static Logger Instance { get; } = new Logger(); |
| | | 13 | | |
| | | 14 | | // Allow tests to override the log path |
| | 35 | 15 | | public static string LogFilePath { get; set; } = Path.Combine(Path.GetTempPath(), "switchblade_debug.log"); |
| | | 16 | | |
| | 9 | 17 | | private static string LogPath => LogFilePath; |
| | | 18 | | |
| | | 19 | | // Instance methods implementing ILogger |
| | | 20 | | bool SwitchBlade.Contracts.ILogger.IsDebugEnabled |
| | | 21 | | { |
| | 2 | 22 | | get => IsDebugEnabled; |
| | 2 | 23 | | set => IsDebugEnabled = value; |
| | | 24 | | } |
| | | 25 | | |
| | 7 | 26 | | void SwitchBlade.Contracts.ILogger.Log(string message) => LogStatic(message); |
| | 1 | 27 | | void SwitchBlade.Contracts.ILogger.LogError(string context, Exception ex) => LogErrorStatic(context, ex); |
| | | 28 | | |
| | | 29 | | // Static methods for backward compatibility |
| | | 30 | | public static void LogStatic(string message) |
| | 59 | 31 | | { |
| | 115 | 32 | | if (!IsDebugEnabled) return; |
| | | 33 | | |
| | | 34 | | try |
| | 3 | 35 | | { |
| | 3 | 36 | | var line = $"[{DateTime.Now:yyyy-MM-dd HH:mm:ss.fff}] {message}{Environment.NewLine}"; |
| | 3 | 37 | | lock (_lock) |
| | 3 | 38 | | { |
| | 3 | 39 | | File.AppendAllText(LogPath, line); |
| | 2 | 40 | | } |
| | 2 | 41 | | } |
| | 1 | 42 | | catch (Exception) |
| | 1 | 43 | | { |
| | | 44 | | // Silently fail to avoid recursive crashes |
| | 1 | 45 | | } |
| | 59 | 46 | | } |
| | | 47 | | |
| | | 48 | | // Bridge for old static calls |
| | 52 | 49 | | public static void Log(string message) => LogStatic(message); |
| | | 50 | | |
| | | 51 | | |
| | | 52 | | public static void LogErrorStatic(string message) |
| | 2 | 53 | | { |
| | | 54 | | try |
| | 2 | 55 | | { |
| | 2 | 56 | | var line = $"[{DateTime.Now:yyyy-MM-dd HH:mm:ss.fff}] ERROR: {message}{Environment.NewLine}"; |
| | 2 | 57 | | lock (_lock) |
| | 2 | 58 | | { |
| | 2 | 59 | | File.AppendAllText(LogPath, line); |
| | 1 | 60 | | } |
| | 1 | 61 | | } |
| | 3 | 62 | | catch { } |
| | 2 | 63 | | } |
| | | 64 | | |
| | | 65 | | // Bridge for old static calls |
| | 2 | 66 | | public static void LogError(string message) => LogErrorStatic(message); |
| | | 67 | | |
| | | 68 | | |
| | | 69 | | public static void LogErrorStatic(string context, Exception ex) |
| | 4 | 70 | | { |
| | | 71 | | // We force logging for errors even if debug is disabled, |
| | | 72 | | // but we might want to reconsider if strict silence is required. |
| | | 73 | | // For now, let's allow errors to be logged to the new temp location for diagnostics. |
| | | 74 | | try |
| | 4 | 75 | | { |
| | 4 | 76 | | var line = $"[{DateTime.Now:yyyy-MM-dd HH:mm:ss.fff}] ERROR [{context}]: {ex.Message}\nStack: {ex.StackT |
| | 4 | 77 | | lock (_lock) |
| | 4 | 78 | | { |
| | 4 | 79 | | File.AppendAllText(LogPath, line); |
| | 3 | 80 | | } |
| | 3 | 81 | | } |
| | 3 | 82 | | catch { } |
| | 4 | 83 | | } |
| | | 84 | | |
| | | 85 | | // Bridge for old static calls |
| | 3 | 86 | | public static void LogError(string context, Exception ex) => LogErrorStatic(context, ex); |
| | | 87 | | } |
| | | 88 | | |
| | | 89 | | } |