namespace Supercell.Laser.Server { using Spectre.Console; using Supercell.Laser.Server.Networking.Session; using Supercell.Laser.Titan.Debug; public static class Logger { private static readonly object SyncRoot = new object(); public static void Init() { Debugger.SetListener(new DebuggerListener()); } private static readonly string[] AsciiArt = [ @" ___ _ _ ___ _ ", @" / __|| |_ (_) _ _ _ _ | _ ) _ _ __ _ __ | | ", @" \__ \| ' \| || ' \| || || _ \| '_|/ _` |\ V / | ", @" |___/|_||_|_||_||_|\_, ||___/|_| \__,_| \_/|_| ", @" |__/ ", ]; public static void Banner() { lock (SyncRoot) { AnsiConsole.Clear(); AnsiConsole.WriteLine(); int width = AnsiConsole.Profile.Width; foreach (string line in AsciiArt) { string padded = line.PadLeft((width + line.Length) / 2); AnsiConsole.Write(new Text(padded + "\n", new Style(Color.HotPink))); } AnsiConsole.WriteLine(); } } public static void Print(string log) => Info(log); public static void Info(string log) => Write("›", "INFO", log, Color.Cyan1); public static void Network(string log) => Write("◈", "NET", log, Color.MediumPurple1); public static void Packet(string log) => Write("◆", "PKT", log, Color.Turquoise2); public static void Command(string log) => Write("❯", "CMD", log, Color.Yellow1); public static void Success(string log) => Write("+", "OK", log, Color.Chartreuse2); public static void Warning(string log) => Write("!", "WARN", log, Color.Yellow1); public static void Error(string log) => Write("x", "ERR", log, Color.Red1); public static void Connection(string ip) => Write("→", "CONN", $"{ip} connected", Color.MediumPurple1); public static void Disconnection(string ip) => Write("←", "DISC", $"{ip} disconnected", Color.Red1); public static void Exception(string context, Exception exception) { lock (SyncRoot) { Error($"{context}: {exception.GetType().Name}: {exception.Message}"); AnsiConsole.WriteException(exception, ExceptionFormats.ShortenPaths | ExceptionFormats.ShowLinks); } } private static void Write(string icon, string level, string log, Color color) { lock (SyncRoot) { string time = DateTime.Now.ToString("HH:mm:ss"); int online = 0; try { online = Sessions.Count; } catch { } int width = AnsiConsole.Profile.Width; string prefix = $"{time} [{online}] {icon} {level} "; string full = prefix + log; int pad = Math.Max(0, (width - full.Length) / 2); string spaces = new string(' ', pad); AnsiConsole.Write(new Text(spaces + $"{time} [{online}] ", new Style(Color.Grey))); AnsiConsole.Write(new Text($"{icon} {level} ", new Style(color, decoration: Decoration.Bold))); AnsiConsole.Write(new Text(log + "\n", new Style(Color.White))); } } } public class DebuggerListener : IDebuggerListener { public void Error(string message) => Logger.Error($"[LOGIC] {message}"); public void Print(string message) => Logger.Info($"[LOGIC] {message}"); public void Warning(string message) => Logger.Warning($"[LOGIC] {message}"); } }