2023-09-12 08:31:55 +00:00
|
|
|
|
using Utils;
|
2023-04-25 09:31:15 +00:00
|
|
|
|
|
|
|
|
|
namespace Logging
|
2023-04-14 12:53:39 +00:00
|
|
|
|
{
|
2023-09-11 14:57:57 +00:00
|
|
|
|
public interface ILog
|
|
|
|
|
{
|
|
|
|
|
void Log(string message);
|
|
|
|
|
void Debug(string message = "", int skipFrames = 0);
|
|
|
|
|
void Error(string message);
|
2023-09-20 11:33:58 +00:00
|
|
|
|
void AddStringReplace(string from, string to);
|
2023-09-11 14:57:57 +00:00
|
|
|
|
LogFile CreateSubfile(string ext = "log");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public abstract class BaseLog : ILog
|
2023-04-14 12:53:39 +00:00
|
|
|
|
{
|
2023-11-08 08:24:39 +00:00
|
|
|
|
public static bool EnableDebugLogging { get; set; } = false;
|
|
|
|
|
|
2023-06-28 13:11:20 +00:00
|
|
|
|
private readonly NumberSource subfileNumberSource = new NumberSource(0);
|
2023-04-30 08:56:19 +00:00
|
|
|
|
private readonly List<BaseLogStringReplacement> replacements = new List<BaseLogStringReplacement>();
|
2023-04-14 12:53:39 +00:00
|
|
|
|
private LogFile? logFile;
|
2023-04-25 09:31:15 +00:00
|
|
|
|
|
2023-11-08 08:24:39 +00:00
|
|
|
|
public BaseLog()
|
2023-04-25 09:31:15 +00:00
|
|
|
|
{
|
2023-11-08 08:24:39 +00:00
|
|
|
|
IsDebug =
|
|
|
|
|
EnableDebugLogging ||
|
|
|
|
|
!string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("LOGDEBUG")) ||
|
|
|
|
|
!string.IsNullOrWhiteSpace(Environment.GetEnvironmentVariable("DEBUGLOG"));
|
2023-04-25 09:31:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-11-08 08:24:39 +00:00
|
|
|
|
protected bool IsDebug { get; private set; }
|
2023-06-28 13:11:20 +00:00
|
|
|
|
protected abstract string GetFullName();
|
2023-04-14 12:53:39 +00:00
|
|
|
|
|
2023-06-28 13:11:20 +00:00
|
|
|
|
public LogFile LogFile
|
2023-04-14 12:53:39 +00:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2023-07-18 12:26:21 +00:00
|
|
|
|
if (logFile == null) logFile = new LogFile(GetFullName(), "log");
|
2023-04-14 12:53:39 +00:00
|
|
|
|
return logFile;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-22 08:17:12 +00:00
|
|
|
|
public virtual void Log(string message)
|
2023-04-14 12:53:39 +00:00
|
|
|
|
{
|
2023-04-30 08:56:19 +00:00
|
|
|
|
LogFile.Write(ApplyReplacements(message));
|
2023-04-14 12:53:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-10-23 14:04:50 +00:00
|
|
|
|
public void Debug(string message = "", int skipFrames = 0)
|
2023-04-25 09:31:15 +00:00
|
|
|
|
{
|
2023-11-08 08:24:39 +00:00
|
|
|
|
if (IsDebug)
|
2023-04-25 09:31:15 +00:00
|
|
|
|
{
|
|
|
|
|
var callerName = DebugStack.GetCallerName(skipFrames);
|
2023-10-23 14:04:50 +00:00
|
|
|
|
Log($"(debug)({callerName}) {message}");
|
2023-04-25 09:31:15 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-22 08:17:12 +00:00
|
|
|
|
public virtual void Error(string message)
|
2023-04-14 12:53:39 +00:00
|
|
|
|
{
|
|
|
|
|
Log($"[ERROR] {message}");
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-22 08:17:12 +00:00
|
|
|
|
public virtual void AddStringReplace(string from, string to)
|
2023-04-30 08:56:19 +00:00
|
|
|
|
{
|
2023-05-31 12:49:06 +00:00
|
|
|
|
if (string.IsNullOrWhiteSpace(from)) return;
|
2023-08-30 08:57:20 +00:00
|
|
|
|
if (replacements.Any(r => r.From == from)) return;
|
2023-04-30 08:56:19 +00:00
|
|
|
|
replacements.Add(new BaseLogStringReplacement(from, to));
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-22 08:17:12 +00:00
|
|
|
|
public virtual void Delete()
|
2023-06-21 09:01:48 +00:00
|
|
|
|
{
|
|
|
|
|
File.Delete(LogFile.FullFilename);
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-28 13:11:20 +00:00
|
|
|
|
public LogFile CreateSubfile(string ext = "log")
|
|
|
|
|
{
|
|
|
|
|
return new LogFile($"{GetFullName()}_{GetSubfileNumber()}", ext);
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-30 08:56:19 +00:00
|
|
|
|
private string ApplyReplacements(string str)
|
|
|
|
|
{
|
2023-11-08 08:24:39 +00:00
|
|
|
|
if (IsDebug) return str;
|
2023-04-30 08:56:19 +00:00
|
|
|
|
foreach (var replacement in replacements)
|
|
|
|
|
{
|
|
|
|
|
str = replacement.Apply(str);
|
|
|
|
|
}
|
|
|
|
|
return str;
|
|
|
|
|
}
|
2023-06-28 13:11:20 +00:00
|
|
|
|
|
|
|
|
|
private string GetSubfileNumber()
|
|
|
|
|
{
|
|
|
|
|
return subfileNumberSource.GetNextNumber().ToString().PadLeft(6, '0');
|
|
|
|
|
}
|
2023-04-30 08:56:19 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class BaseLogStringReplacement
|
|
|
|
|
{
|
|
|
|
|
public BaseLogStringReplacement(string from, string to)
|
|
|
|
|
{
|
2023-08-30 08:57:20 +00:00
|
|
|
|
From = from;
|
|
|
|
|
To = to;
|
2023-04-30 08:56:19 +00:00
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(from) || string.IsNullOrEmpty(to) || from == to) throw new ArgumentException();
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-30 08:57:20 +00:00
|
|
|
|
public string From { get; }
|
|
|
|
|
public string To { get; }
|
|
|
|
|
|
2023-04-30 08:56:19 +00:00
|
|
|
|
public string Apply(string msg)
|
|
|
|
|
{
|
2023-08-30 08:57:20 +00:00
|
|
|
|
return msg.Replace(From, To);
|
2023-04-30 08:56:19 +00:00
|
|
|
|
}
|
2023-04-14 12:53:39 +00:00
|
|
|
|
}
|
|
|
|
|
}
|