2022-04-28 03:32:52 +00:00
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
|
|
namespace Shared;
|
2021-11-29 04:04:34 +00:00
|
|
|
|
|
|
|
|
|
public class Logger {
|
|
|
|
|
public Logger(string name) {
|
|
|
|
|
Name = name;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-14 19:45:58 +00:00
|
|
|
|
public string Name { get; set; }
|
2022-02-10 01:44:50 +00:00
|
|
|
|
|
2022-06-13 00:48:24 +00:00
|
|
|
|
public void Info(string text) => Handler?.Invoke(Name, "Info", text, ConsoleColor.White);
|
2021-11-29 04:04:34 +00:00
|
|
|
|
|
2022-06-13 00:48:24 +00:00
|
|
|
|
public void Warn(string text) => Handler?.Invoke(Name, "Warn", text, ConsoleColor.Yellow);
|
2021-11-29 04:04:34 +00:00
|
|
|
|
|
2022-06-13 00:48:24 +00:00
|
|
|
|
public void Error(string text) => Handler?.Invoke(Name, "Error", text, ConsoleColor.Red);
|
2022-04-28 03:32:52 +00:00
|
|
|
|
|
2022-06-13 00:48:24 +00:00
|
|
|
|
public void Error(Exception error) => Error(error.ToString());
|
2022-04-28 03:32:52 +00:00
|
|
|
|
|
2022-06-13 00:48:24 +00:00
|
|
|
|
public static string PrefixNewLines(string text, string prefix) {
|
2022-04-28 03:32:52 +00:00
|
|
|
|
StringBuilder builder = new StringBuilder();
|
|
|
|
|
foreach (string str in text.Split('\n'))
|
|
|
|
|
builder
|
|
|
|
|
.Append(prefix)
|
|
|
|
|
.Append(' ')
|
|
|
|
|
.AppendLine(str);
|
|
|
|
|
return builder.ToString();
|
2021-11-29 04:04:34 +00:00
|
|
|
|
}
|
2022-06-13 00:48:24 +00:00
|
|
|
|
|
|
|
|
|
public delegate void LogHandler(string source, string level, string text, ConsoleColor color);
|
|
|
|
|
|
|
|
|
|
private static LogHandler? Handler;
|
|
|
|
|
public static void AddLogHandler(LogHandler handler) => Handler += handler;
|
|
|
|
|
|
|
|
|
|
static Logger() {
|
|
|
|
|
AddLogHandler((source, level, text, color) => {
|
|
|
|
|
Console.ForegroundColor = color;
|
|
|
|
|
Console.Write(PrefixNewLines(text, $"{level} [{source}]"));
|
|
|
|
|
});
|
|
|
|
|
}
|
2021-11-29 04:04:34 +00:00
|
|
|
|
}
|