0
0
Fork 0
mirror of https://github.com/Sanae6/SmoOnlineServer.git synced 2024-11-25 20:55:18 +00:00
SmoOnlineServer/Shared/Logger.cs

42 lines
1.3 KiB
C#
Raw Normal View History

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;
}
public string Name { get; set; }
2022-02-10 01:44:50 +00:00
public void Info(string text) => Handler?.Invoke(Name, "Info", text, ConsoleColor.White);
2021-11-29 04:04:34 +00:00
public void Warn(string text) => Handler?.Invoke(Name, "Warn", text, ConsoleColor.Yellow);
2021-11-29 04:04:34 +00:00
public void Error(string text) => Handler?.Invoke(Name, "Error", text, ConsoleColor.Red);
2022-04-28 03:32:52 +00:00
public void Error(Exception error) => Error(error.ToString());
2022-04-28 03:32:52 +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
}
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) => {
2022-06-20 11:04:30 +00:00
DateTime logtime = DateTime.Now;
Console.ForegroundColor = color;
2022-06-20 11:04:30 +00:00
Console.Write(PrefixNewLines(text, $"{{{logtime}}} {level} [{source}]"));
});
}
2021-11-29 04:04:34 +00:00
}