-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDebugLogger.cs
More file actions
287 lines (261 loc) · 10.3 KB
/
DebugLogger.cs
File metadata and controls
287 lines (261 loc) · 10.3 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// This file is part of BOINC.
// https://boinc.berkeley.edu
// Copyright (C) 2026 University of California
//
// BOINC is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation,
// either version 3 of the License, or (at your option) any later version.
//
// BOINC is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with BOINC. If not, see <http://www.gnu.org/licenses/>.
using System;
using System.IO;
using System.Text;
namespace boinc_buda_runner_wsl_installer
{
/// <summary>
/// Debug logging utility that writes verbose information to a log file and optionally to console
/// </summary>
internal static class DebugLogger
{
private static string _logFilePath;
private static bool _isEnabled = true; // Always enabled
private static readonly object _lockObject = new object();
private static bool _initialized = false;
// Console logging configuration
private static bool _consoleInfoEnabled = false;
private static bool _consoleDebugEnabled = false;
private static bool _consoleErrorEnabled = false;
/// <summary>
/// Configure console logging behavior (stdout/stderr). INFO and WARN go to stdout if infoEnabled;
/// DEBUG goes to stdout if debugEnabled; ERROR goes to stderr if errorEnabled.
/// </summary>
public static void ConfigureConsoleLogging(bool infoEnabled, bool debugEnabled, bool errorEnabled)
{
_consoleInfoEnabled = infoEnabled;
_consoleDebugEnabled = debugEnabled;
_consoleErrorEnabled = errorEnabled;
}
/// <summary>
/// Gets or sets whether debug logging is enabled
/// </summary>
public static bool IsEnabled
{
get => _isEnabled;
set
{
_isEnabled = value;
if (_isEnabled && string.IsNullOrEmpty(_logFilePath))
{
InitializeLogFile();
}
}
}
/// <summary>
/// Gets the current log file path
/// </summary>
public static string LogFilePath => _logFilePath;
/// <summary>
/// Initializes the log file with a timestamped filename in the temp directory
/// </summary>
private static void InitializeLogFile()
{
if (_initialized) return;
_initialized = true;
try
{
var timestamp = DateTime.Now.ToString("yyyyMMdd_HHmmss");
var fileName = $"boinc_buda_installer_debug_{timestamp}.log";
var tempPath = Path.GetTempPath();
_logFilePath = Path.Combine(tempPath, fileName);
// Create the initial log entry
LogInfo($"Debug logging started at {DateTime.Now:yyyy-MM-dd HH:mm:ss}", "DebugLogger");
LogInfo($"Log file: {_logFilePath}", "DebugLogger");
LogInfo($"Application: BOINC WSL Distro Installer", "DebugLogger");
LogInfo($"Application Version: 2.2.2", "DebugLogger");
LogInfo($"Windows Version: {Environment.OSVersion}", "DebugLogger");
LogInfo($"Is 64-bit OS: {Environment.Is64BitOperatingSystem}", "DebugLogger");
LogInfo($"Is 64-bit Process: {Environment.Is64BitProcess}", "DebugLogger");
LogInfo($"CLR Version: {Environment.Version}", "DebugLogger");
LogInfo($"Working Directory: {Environment.CurrentDirectory}", "DebugLogger");
LogInfo($"User Domain: {Environment.UserDomainName}", "DebugLogger");
LogInfo($"User Name: {Environment.UserName}", "DebugLogger");
LogInfo("=" + new string('=', 70), "DebugLogger");
}
catch
{
// If we can't create the log file, disable logging silently
_isEnabled = false;
}
}
/// <summary>
/// Logs an informational message
/// </summary>
public static void LogInfo(string message, string component = null)
{
Log("INFO", message, component);
}
/// <summary>
/// Logs a warning message
/// </summary>
public static void LogWarning(string message, string component = null)
{
Log("WARN", message, component);
}
/// <summary>
/// Logs an error message
/// </summary>
public static void LogError(string message, string component = null)
{
Log("ERROR", message, component);
}
/// <summary>
/// Logs an exception with full details
/// </summary>
public static void LogException(Exception ex, string context = null, string component = null)
{
var message = context != null ? $"{context}: {ex}" : ex.ToString();
Log("ERROR", message, component);
}
/// <summary>
/// Logs a debug message (most verbose level)
/// </summary>
public static void LogDebug(string message, string component = null)
{
Log("DEBUG", message, component);
}
/// <summary>
/// Logs the start of a method or operation
/// </summary>
public static void LogMethodStart(string methodName, string parameters = null, string component = null)
{
var message = parameters != null ? $"Starting {methodName}({parameters})" : $"Starting {methodName}()";
Log("DEBUG", message, component);
}
/// <summary>
/// Logs the end of a method or operation with result
/// </summary>
public static void LogMethodEnd(string methodName, string result = null, string component = null)
{
var message = result != null ? $"Completed {methodName} -> {result}" : $"Completed {methodName}";
Log("DEBUG", message, component);
}
/// <summary>
/// Logs process execution details
/// </summary>
public static void LogProcessExecution(string fileName, string arguments, int exitCode, string output = null, string error = null, string component = null)
{
LogDebug($"Process: {fileName} {arguments}", component);
LogDebug($"Exit Code: {exitCode}", component);
if (!string.IsNullOrEmpty(output))
{
LogDebug($"Output: {output.Trim()}", component);
}
if (!string.IsNullOrEmpty(error))
{
LogWarning($"Error Output: {error.Trim()}", component);
}
}
/// <summary>
/// Logs UI status changes
/// </summary>
public static void LogUIStatusChange(string elementId, string newIcon, string newStatus, string component = "UI")
{
LogDebug($"UI Update: {elementId} -> Icon: {newIcon}, Status: {newStatus}", component);
}
/// <summary>
/// Logs configuration or state information
/// </summary>
public static void LogConfiguration(string name, object value, string component = null)
{
LogInfo($"Config: {name} = {value}", component);
}
/// <summary>
/// Core logging method that writes to the file
/// </summary>
private static void Log(string level, string message, string component)
{
// Write to file
if (_isEnabled)
{
if (string.IsNullOrEmpty(_logFilePath))
{
InitializeLogFile();
}
try
{
if (!string.IsNullOrEmpty(_logFilePath))
{
lock (_lockObject)
{
var timestamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");
var threadId = System.Threading.Thread.CurrentThread.ManagedThreadId;
var componentPart = !string.IsNullOrEmpty(component) ? $"[{component}] " : "";
var logEntry = $"{timestamp} [{level}] [T{threadId:D2}] {componentPart}{message}";
File.AppendAllText(_logFilePath, logEntry + Environment.NewLine, Encoding.UTF8);
}
}
}
catch { }
}
// Write to console according to configuration
try
{
if (level == "ERROR")
{
if (_consoleErrorEnabled)
{
Console.Error.WriteLine(message);
}
}
else if (level == "DEBUG")
{
if (_consoleDebugEnabled)
{
Console.Out.WriteLine(message);
}
}
else // INFO or WARN
{
if (_consoleInfoEnabled)
{
Console.Out.WriteLine(message);
}
}
}
catch { }
}
/// <summary>
/// Logs a section separator for better readability
/// </summary>
public static void LogSeparator(string title = null)
{
if (!_isEnabled) return;
var separator = new string('-', 50);
if (!string.IsNullOrEmpty(title))
{
LogInfo($"{separator} {title} {separator}");
}
else
{
LogInfo(separator);
}
}
/// <summary>
/// Flushes any pending log entries (useful before app shutdown)
/// </summary>
public static void Flush()
{
if (_isEnabled && !string.IsNullOrEmpty(_logFilePath))
{
LogInfo("Debug logging session ended.", "DebugLogger");
}
}
}
}