2023-03-03 19:25:33 +00:00
|
|
|
/* LogManager.cs
|
|
|
|
* License: NCSA Open Source License
|
|
|
|
*
|
2024-05-21 20:15:19 +01:00
|
|
|
* Copyright: SPT
|
2023-03-03 19:25:33 +00:00
|
|
|
* AUTHORS:
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.IO;
|
2024-06-02 14:46:53 -04:00
|
|
|
using SPT.Launcher.Helpers;
|
2023-03-03 19:25:33 +00:00
|
|
|
|
2024-05-21 20:15:19 +01:00
|
|
|
namespace SPT.Launcher.Controllers
|
2023-03-03 19:25:33 +00:00
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// LogManager
|
|
|
|
/// </summary>
|
|
|
|
public class LogManager
|
|
|
|
{
|
|
|
|
private static LogManager _instance;
|
2024-02-05 16:06:28 -05:00
|
|
|
public static LogManager Instance => _instance ??= new LogManager();
|
|
|
|
private readonly string _filePath;
|
|
|
|
private readonly string _logFile;
|
2023-03-03 19:25:33 +00:00
|
|
|
|
2024-02-05 16:06:28 -05:00
|
|
|
private LogManager()
|
2023-03-03 19:25:33 +00:00
|
|
|
{
|
2024-02-05 16:06:28 -05:00
|
|
|
_filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "user", "logs");
|
|
|
|
_logFile = Path.Combine(_filePath, "launcher.log");
|
|
|
|
|
|
|
|
if (File.Exists(_logFile))
|
|
|
|
{
|
|
|
|
File.Delete(_logFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
Write($" ==== Launcher Started ====");
|
2023-03-03 19:25:33 +00:00
|
|
|
}
|
|
|
|
|
2024-06-02 14:46:53 -04:00
|
|
|
private string GetDevModeTag()
|
|
|
|
{
|
|
|
|
if (LauncherSettingsProvider.Instance != null && LauncherSettingsProvider.Instance.IsDevMode)
|
|
|
|
{
|
|
|
|
return "[DEV_MODE]";
|
|
|
|
}
|
|
|
|
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2024-02-05 16:06:28 -05:00
|
|
|
private void Write(string text)
|
2023-03-03 19:25:33 +00:00
|
|
|
{
|
2024-02-05 16:06:28 -05:00
|
|
|
if (!Directory.Exists(_filePath))
|
2023-03-03 19:25:33 +00:00
|
|
|
{
|
2024-02-05 16:06:28 -05:00
|
|
|
Directory.CreateDirectory(_filePath);
|
2023-03-03 19:25:33 +00:00
|
|
|
}
|
2024-06-02 14:46:53 -04:00
|
|
|
|
|
|
|
File.AppendAllLines(_logFile, new[] { $"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}]{GetDevModeTag()}{text}" });
|
2023-03-03 19:25:33 +00:00
|
|
|
}
|
|
|
|
|
2024-02-05 16:06:28 -05:00
|
|
|
public void Debug(string text) => Write($"[Debug] {text}");
|
2023-03-03 19:25:33 +00:00
|
|
|
|
2024-02-05 16:06:28 -05:00
|
|
|
public void Info(string text) => Write($"[Info] {text}");
|
2023-03-03 19:25:33 +00:00
|
|
|
|
2024-02-05 16:06:28 -05:00
|
|
|
public void Warning(string text) => Write($"[Warning] {text}");
|
2023-03-03 19:25:33 +00:00
|
|
|
|
2024-02-05 16:06:28 -05:00
|
|
|
public void Error(string text) => Write($"[Error] {text}");
|
2023-03-03 19:25:33 +00:00
|
|
|
|
2024-02-05 16:06:28 -05:00
|
|
|
public void Exception(Exception ex) => Write($"[Exception] {ex.Message}\nStacktrace:\n{ex.StackTrace}");
|
2023-03-03 19:25:33 +00:00
|
|
|
}
|
|
|
|
}
|