69 lines
1.7 KiB
C#
Raw Normal View History

2024-06-19 21:11:42 -04:00
using ReCodeItLib.Utils;
namespace ReCodeIt.Utils;
2024-06-11 19:18:48 -04:00
2024-06-13 18:15:52 -04:00
public static class Logger
2024-06-11 19:18:48 -04:00
{
static Logger()
{
if (File.Exists(_logPath))
{
File.Delete(_logPath);
File.Create(_logPath).Close();
}
}
2024-06-19 21:11:42 -04:00
private static string _logPath => RegistryHelper.GetRegistryValue<string>("LogPath");
2024-06-11 19:18:48 -04:00
2024-06-13 07:45:40 -04:00
public static void ClearLog()
{
if (File.Exists(_logPath))
{
File.Delete(_logPath);
File.Create(_logPath).Close();
}
}
public static void Log(object message, ConsoleColor color = ConsoleColor.Gray, bool silent = false)
2024-06-11 19:18:48 -04:00
{
2024-06-13 20:25:11 -04:00
if (!silent)
2024-06-12 18:59:08 -04:00
{
2024-06-13 20:25:11 -04:00
Console.ForegroundColor = color;
Console.WriteLine(message);
2024-06-12 18:59:08 -04:00
}
WriteToDisk(message);
}
2024-06-11 19:18:48 -04:00
2024-06-13 07:45:40 -04:00
public static void LogDebug(object message, ConsoleColor color = ConsoleColor.Gray, bool silent = false)
2024-06-12 18:59:08 -04:00
{
2024-06-13 17:55:06 -04:00
if (DataProvider.Settings.AppSettings.Debug)
2024-06-12 18:59:08 -04:00
{
Console.ForegroundColor = color;
Console.WriteLine(message);
Console.ResetColor();
WriteToDisk(message);
}
2024-06-13 20:25:11 -04:00
WriteToDisk(message);
2024-06-12 18:59:08 -04:00
}
2024-06-13 07:45:40 -04:00
private static void WriteToDisk(object message)
2024-06-12 18:59:08 -04:00
{
2024-06-20 14:23:11 -04:00
if (DataProvider.IsCli) { return; }
2024-06-20 13:58:39 -04:00
2024-06-11 19:18:48 -04:00
try
{
using (StreamWriter sw = File.AppendText(_logPath))
{
sw.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss} - {message}");
2024-06-20 13:58:39 -04:00
sw.Close();
2024-06-11 19:18:48 -04:00
}
}
catch (IOException ex)
{
// Handle potential file writing errors gracefully
Console.WriteLine($"Error logging: {ex.Message}");
}
}
}