-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogger.cs
More file actions
186 lines (161 loc) · 6.04 KB
/
Logger.cs
File metadata and controls
186 lines (161 loc) · 6.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
@file Logger.cs
@description Shared PaxDrops logging helper that writes MelonLoader output and file logs with level-aware exception reporting.
@editCount 1
*/
using System;
using System.IO;
using MelonLoader;
using UnityEngine;
namespace PaxDrops
{
public enum LogLevel
{
Debug = 0,
Msg = 1,
Info = 2,
Warning = 3,
Error = 4
}
public static class Logger
{
private const string Prefix = "[PaxDrops]";
private const string LogDir = "Mods/PaxDrops/Logs";
private const string LatestLog = "latest.log";
private const int MaxLogs = 5;
public static LogLevel MinLogLevel { get; set; } = GetDefaultLogLevel();
private static StreamWriter? _writer;
public static string GetBuildConfiguration()
{
#if STAGING
return "STAGING";
#elif RELEASE
return "RELEASE";
#else
return "DEBUG";
#endif
}
private static LogLevel GetDefaultLogLevel()
{
#if STAGING
return LogLevel.Msg;
#elif RELEASE
return LogLevel.Info;
#else
return LogLevel.Debug;
#endif
}
public static void SetProductionMode() => MinLogLevel = LogLevel.Info;
public static void SetStagingMode() => MinLogLevel = LogLevel.Msg;
public static void SetDebugMode() => MinLogLevel = LogLevel.Debug;
private static string GetLogPrefix(LogLevel level, string category)
{
var levelStr = level switch
{
LogLevel.Debug => "DEBUG",
LogLevel.Msg => "MSG",
LogLevel.Info => "INFO",
LogLevel.Warning => "WARN",
LogLevel.Error => "ERROR",
_ => "LOG"
};
return $"{Prefix} [{category}] [{levelStr}]";
}
public static void Init()
{
try
{
InitializeLogLevel();
Directory.CreateDirectory(LogDir);
RotateLogs();
string fullPath = Path.Combine(LogDir, LatestLog);
_writer = new StreamWriter(fullPath, append: false) { AutoFlush = true };
MelonLogger.Msg($"{GetLogPrefix(LogLevel.Msg, "Logger")} Logger initialized. Writing to: " + fullPath);
}
catch (Exception ex)
{
MelonLogger.Warning($"{GetLogPrefix(LogLevel.Error, "Logger")} Failed to initialize file logger: {ex.Message}");
}
}
private static void InitializeLogLevel()
{
MinLogLevel = GetDefaultLogLevel();
Info("Logging System initializing...");
Info($"Build configuration: {GetBuildConfiguration()}");
Info($"Log level: {MinLogLevel}");
}
public static void Msg(string message, string category = "GENERAL LOG")
{
if (MinLogLevel > LogLevel.Msg) return;
MelonLogger.Msg($"{GetLogPrefix(LogLevel.Msg, category)} {message}");
WriteToFile($"[MSG] {message}");
}
public static void Info(string message, string category = "GENERAL LOG")
{
if (MinLogLevel > LogLevel.Info) return;
MelonLogger.Msg($"{GetLogPrefix(LogLevel.Info, category)} {message}");
WriteToFile($"[INFO] {message}");
}
public static void Warn(string message, string category = "GENERAL LOG")
{
if (MinLogLevel > LogLevel.Warning) return;
MelonLogger.Warning($"{GetLogPrefix(LogLevel.Warning, category)} {message}");
WriteToFile($"[WARN] {message}");
}
public static void Error(string message, string category = "GENERAL LOG")
{
if (MinLogLevel > LogLevel.Error) return;
MelonLogger.Error($"{GetLogPrefix(LogLevel.Error, category)} {message}");
WriteToFile($"[ERROR] {message}");
}
public static void Exception(Exception ex, string category = "GENERAL LOG")
{
if (MinLogLevel > LogLevel.Error) return;
string exceptionType = ex.GetType().Name;
string message = $"{exceptionType}: {ex.Message}";
MelonLogger.Error($"{GetLogPrefix(LogLevel.Error, category)} {message}");
WriteToFile($"[ERROR] {message}");
if (!string.IsNullOrWhiteSpace(ex.StackTrace))
{
WriteToFile($"[ERROR] {ex.StackTrace}");
}
if (ex.InnerException != null)
{
string innerType = ex.InnerException.GetType().Name;
string innerMessage = $"Inner {innerType}: {ex.InnerException.Message}";
MelonLogger.Error($"{GetLogPrefix(LogLevel.Error, category)} {innerMessage}");
WriteToFile($"[ERROR] {innerMessage}");
if (!string.IsNullOrWhiteSpace(ex.InnerException.StackTrace))
{
WriteToFile($"[ERROR] {ex.InnerException.StackTrace}");
}
}
}
public static void Debug(string message, string category = "GENERAL LOG")
{
if (MinLogLevel > LogLevel.Debug) return;
MelonLogger.Msg($"{GetLogPrefix(LogLevel.Debug, category)} {message}");
WriteToFile($"[DEBUG] {message}");
}
private static void WriteToFile(string line)
{
_writer?.WriteLine($"{DateTime.Now:HH:mm:ss} {line}");
}
private static void RotateLogs()
{
for (int i = MaxLogs - 1; i > 0; i--)
{
string src = Path.Combine(LogDir, i == 1 ? LatestLog : $"log_{i - 1}.txt");
string dst = Path.Combine(LogDir, $"log_{i}.txt");
if (File.Exists(src))
File.Copy(src, dst, overwrite: true);
}
}
public static void Shutdown()
{
_writer?.Close();
_writer?.Dispose();
_writer = null;
}
}
}