2023-04-30 08:56:19 +00:00
|
|
|
|
using Utils;
|
2023-04-25 09:31:15 +00:00
|
|
|
|
|
|
|
|
|
namespace Logging
|
2023-04-14 12:53:39 +00:00
|
|
|
|
{
|
|
|
|
|
public abstract class BaseLog
|
|
|
|
|
{
|
2023-04-30 08:56:19 +00:00
|
|
|
|
private readonly bool debug;
|
|
|
|
|
private readonly List<BaseLogStringReplacement> replacements = new List<BaseLogStringReplacement>();
|
2023-04-14 12:53:39 +00:00
|
|
|
|
private bool hasFailed;
|
|
|
|
|
private LogFile? logFile;
|
2023-04-25 09:31:15 +00:00
|
|
|
|
|
|
|
|
|
protected BaseLog(bool debug)
|
|
|
|
|
{
|
|
|
|
|
this.debug = debug;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-14 12:53:39 +00:00
|
|
|
|
protected abstract LogFile CreateLogFile();
|
|
|
|
|
|
|
|
|
|
protected LogFile LogFile
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (logFile == null) logFile = CreateLogFile();
|
|
|
|
|
return logFile;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Log(string message)
|
|
|
|
|
{
|
2023-04-30 08:56:19 +00:00
|
|
|
|
LogFile.Write(ApplyReplacements(message));
|
2023-04-14 12:53:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-25 09:31:15 +00:00
|
|
|
|
public void Debug(string message = "", int skipFrames = 0)
|
|
|
|
|
{
|
|
|
|
|
if (debug)
|
|
|
|
|
{
|
|
|
|
|
var callerName = DebugStack.GetCallerName(skipFrames);
|
2023-04-30 08:56:19 +00:00
|
|
|
|
// We don't use Log because in the debug output we should not have any replacements.
|
|
|
|
|
LogFile.Write($"(debug)({callerName}) {message}");
|
2023-04-25 09:31:15 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-14 12:53:39 +00:00
|
|
|
|
public void Error(string message)
|
|
|
|
|
{
|
|
|
|
|
Log($"[ERROR] {message}");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void MarkAsFailed()
|
|
|
|
|
{
|
|
|
|
|
if (hasFailed) return;
|
|
|
|
|
hasFailed = true;
|
|
|
|
|
LogFile.ConcatToFilename("_FAILED");
|
|
|
|
|
}
|
2023-04-30 08:56:19 +00:00
|
|
|
|
|
|
|
|
|
public void AddStringReplace(string from, string to)
|
|
|
|
|
{
|
2023-05-31 12:49:06 +00:00
|
|
|
|
if (string.IsNullOrWhiteSpace(from)) return;
|
2023-04-30 08:56:19 +00:00
|
|
|
|
replacements.Add(new BaseLogStringReplacement(from, to));
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-21 09:01:48 +00:00
|
|
|
|
public void Delete()
|
|
|
|
|
{
|
|
|
|
|
File.Delete(LogFile.FullFilename);
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-30 08:56:19 +00:00
|
|
|
|
private string ApplyReplacements(string str)
|
|
|
|
|
{
|
|
|
|
|
foreach (var replacement in replacements)
|
|
|
|
|
{
|
|
|
|
|
str = replacement.Apply(str);
|
|
|
|
|
}
|
|
|
|
|
return str;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class BaseLogStringReplacement
|
|
|
|
|
{
|
|
|
|
|
private readonly string from;
|
|
|
|
|
private readonly string to;
|
|
|
|
|
|
|
|
|
|
public BaseLogStringReplacement(string from, string to)
|
|
|
|
|
{
|
|
|
|
|
this.from = from;
|
|
|
|
|
this.to = to;
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(from) || string.IsNullOrEmpty(to) || from == to) throw new ArgumentException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public string Apply(string msg)
|
|
|
|
|
{
|
|
|
|
|
return msg.Replace(from, to);
|
|
|
|
|
}
|
2023-04-14 12:53:39 +00:00
|
|
|
|
}
|
|
|
|
|
}
|