0
0
mirror of https://github.com/sp-tarkov/launcher.git synced 2025-02-13 06:10:45 -05:00

69 lines
1.8 KiB
C#
Raw Permalink Normal View History

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;
2024-07-12 17:29:43 -04:00
public 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");
2024-07-12 17:29:43 -04:00
LogFile = Path.Combine(_filePath, "launcher.log");
2024-02-05 16:06:28 -05:00
2024-07-12 17:29:43 -04:00
if (File.Exists(LogFile))
2024-02-05 16:06:28 -05:00
{
2024-07-12 17:29:43 -04:00
File.Delete(LogFile);
2024-02-05 16:06:28 -05:00
}
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
2024-07-12 17:29:43 -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
}
}