code sources
This commit is contained in:
@@ -0,0 +1,93 @@
|
||||
using System;
|
||||
using System.Drawing;
|
||||
using System.IO;
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
using Pastel;
|
||||
|
||||
namespace ComicCompressor
|
||||
{
|
||||
public class Logger
|
||||
{
|
||||
public LogLevel LoggingLevel { get; set; } = LogLevel.Warning;
|
||||
public bool Debug = false;
|
||||
|
||||
// Set to null for stdout
|
||||
public string LogFile { get; set; } = null;
|
||||
|
||||
public void Log(object message, LogLevel logLevel, Color? col = null)
|
||||
{
|
||||
Log(message.ToString(), logLevel, col:col);
|
||||
}
|
||||
|
||||
public void Log(string message, LogLevel logLevel,bool newLine = true, Color? col = null)
|
||||
{
|
||||
if (logLevel > LoggingLevel) return;
|
||||
|
||||
if (LogFile == null)
|
||||
{
|
||||
if(newLine)
|
||||
Console.WriteLine(message.Pastel(col ?? Color.FromArgb(0,204,204,204)));
|
||||
else
|
||||
Console.Write(message.Pastel(col ?? Color.FromArgb(0, 204, 204, 204)));
|
||||
return;
|
||||
}
|
||||
|
||||
WriteToFile(message);
|
||||
}
|
||||
|
||||
public void LogError(string message, Color? col = null)
|
||||
{
|
||||
Log(message, LogLevel.Error, col:col);
|
||||
}
|
||||
|
||||
public void LogError(object message, Color? col = null)
|
||||
{
|
||||
Log(message, LogLevel.Error, col:col);
|
||||
}
|
||||
|
||||
public void LogDebug(string message, LogLevel logLevel, Color? col = null)
|
||||
{
|
||||
if (!Debug) return;
|
||||
Log(message, logLevel,col:col);
|
||||
}
|
||||
|
||||
public void LogDebug(object message, LogLevel logLevel, Color? col = null)
|
||||
{
|
||||
LogDebug(message.ToString(), logLevel, col:col);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.Synchronized)]
|
||||
private void WriteToFile(string message)
|
||||
{
|
||||
if (LogFile == null) return;
|
||||
|
||||
if (!Path.IsPathRooted(LogFile) && !LogFile.StartsWith("."))
|
||||
{
|
||||
LogFile = Path.Join(".", LogFile);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
Directory.CreateDirectory(Path.GetDirectoryName(LogFile));
|
||||
File.AppendAllText(Path.GetFullPath(LogFile), message + "\n");
|
||||
}
|
||||
catch (UnauthorizedAccessException)
|
||||
{
|
||||
Console.WriteLine("Error: User does not have permissions for log file: " + LogFile);
|
||||
return;
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
Console.WriteLine("Error: could not write to log file: " + LogFile);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public enum LogLevel
|
||||
{
|
||||
Error = 2, Warning = 3, Info = 4, Verbose = 5
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user