|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | +using System.Runtime.CompilerServices; |
| 4 | + |
| 5 | +namespace ExampleShared |
| 6 | +{ |
| 7 | + /// <summary> |
| 8 | + /// Simple debug logging utility that writes to out.txt for crash investigation. |
| 9 | + /// </summary> |
| 10 | + public static class DebugLog |
| 11 | + { |
| 12 | + private static readonly object _lock = new object(); |
| 13 | + private static readonly string LogFile = "out.txt"; |
| 14 | + private static bool _initialized = false; |
| 15 | + |
| 16 | + /// <summary> |
| 17 | + /// Initialize the log file (clears previous content). |
| 18 | + /// </summary> |
| 19 | + public static void Init() |
| 20 | + { |
| 21 | + lock (_lock) |
| 22 | + { |
| 23 | + try |
| 24 | + { |
| 25 | + File.WriteAllText(LogFile, $"=== NuklearDotNet Debug Log - {DateTime.Now:yyyy-MM-dd HH:mm:ss} ===\n\n"); |
| 26 | + _initialized = true; |
| 27 | + Log("Debug logging initialized"); |
| 28 | + } |
| 29 | + catch (Exception ex) |
| 30 | + { |
| 31 | + Console.WriteLine($"Failed to initialize debug log: {ex.Message}"); |
| 32 | + } |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + /// <summary> |
| 37 | + /// Log a message with timestamp and optional caller info. |
| 38 | + /// </summary> |
| 39 | + public static void Log(string message, |
| 40 | + [CallerMemberName] string memberName = "", |
| 41 | + [CallerFilePath] string filePath = "", |
| 42 | + [CallerLineNumber] int lineNumber = 0) |
| 43 | + { |
| 44 | + lock (_lock) |
| 45 | + { |
| 46 | + try |
| 47 | + { |
| 48 | + if (!_initialized) Init(); |
| 49 | + |
| 50 | + string fileName = Path.GetFileName(filePath); |
| 51 | + string logEntry = $"[{DateTime.Now:HH:mm:ss.fff}] [{fileName}:{lineNumber}] {memberName}: {message}\n"; |
| 52 | + |
| 53 | + File.AppendAllText(LogFile, logEntry); |
| 54 | + |
| 55 | + // Also write to console for immediate visibility |
| 56 | + Console.Write(logEntry); |
| 57 | + } |
| 58 | + catch |
| 59 | + { |
| 60 | + // Silently fail to avoid disrupting the app |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + /// <summary> |
| 66 | + /// Log an error with exception details. |
| 67 | + /// </summary> |
| 68 | + public static void Error(string message, Exception ex = null, |
| 69 | + [CallerMemberName] string memberName = "", |
| 70 | + [CallerFilePath] string filePath = "", |
| 71 | + [CallerLineNumber] int lineNumber = 0) |
| 72 | + { |
| 73 | + string errorMsg = ex != null |
| 74 | + ? $"ERROR: {message} - {ex.GetType().Name}: {ex.Message}\nStack: {ex.StackTrace}" |
| 75 | + : $"ERROR: {message}"; |
| 76 | + Log(errorMsg, memberName, filePath, lineNumber); |
| 77 | + } |
| 78 | + |
| 79 | + /// <summary> |
| 80 | + /// Log entry into a method/section. |
| 81 | + /// </summary> |
| 82 | + public static void Enter([CallerMemberName] string memberName = "") |
| 83 | + { |
| 84 | + Log($">>> Entering", memberName); |
| 85 | + } |
| 86 | + |
| 87 | + /// <summary> |
| 88 | + /// Log exit from a method/section. |
| 89 | + /// </summary> |
| 90 | + public static void Exit([CallerMemberName] string memberName = "") |
| 91 | + { |
| 92 | + Log($"<<< Exiting", memberName); |
| 93 | + } |
| 94 | + |
| 95 | + /// <summary> |
| 96 | + /// Flush the log file to ensure all content is written. |
| 97 | + /// </summary> |
| 98 | + public static void Flush() |
| 99 | + { |
| 100 | + // File.AppendAllText already flushes, but this is here for API completeness |
| 101 | + Log("Log flushed"); |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments