Skip to content

Commit dd452f5

Browse files
committed
Logs with color
1 parent e703db7 commit dd452f5

3 files changed

Lines changed: 127 additions & 0 deletions

File tree

Core/LocalAdmin.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,7 @@ private void StartSession()
611611
Menu();
612612
SetTerminalTitle();
613613
Logger.Initialize();
614+
LoggerWithColors.Initialize();
614615

615616
ConsoleUtil.WriteLine($"Started new session on port {GamePort}.", ConsoleColor.DarkGreen);
616617
ConsoleUtil.WriteLine("Trying to start server...", ConsoleColor.Gray);
@@ -1186,6 +1187,7 @@ public void Exit(int code = -1, bool waitForKey = false, bool restart = false)
11861187
_processClosing = true;
11871188
LogCleaner.Abort();
11881189
Logger.EndLogging();
1190+
LoggerWithColors.EndLogging();
11891191
TerminateGame(); // Forcefully terminating the process
11901192
_gameProcess?.Dispose();
11911193

IO/ConsoleUtil.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,9 @@ public static void Write(string content, ConsoleColor color = ConsoleColor.White
100100

101101
if (log)
102102
Logger.Log($"{GetLogsTimestamp()} {(multiline ? content.Replace("\n", GetLogsPadding(), StringComparison.Ordinal) : content)}");
103+
104+
if (display)
105+
LoggerWithColors.Log($"{GetLogsTimestamp()} {(multiline ? content.Replace("\n", GetLogsPadding(), StringComparison.Ordinal) : content)}", color);
103106
}
104107
}
105108

@@ -131,6 +134,9 @@ public static void WriteLine(string? content, ConsoleColor color = ConsoleColor.
131134

132135
if (log)
133136
Logger.Log($"{GetLogsTimestamp()} {(multiline ? content.Replace("\n", GetLogsPadding(), StringComparison.Ordinal) : content)}");
137+
138+
if (display)
139+
LoggerWithColors.Log($"{GetLogsTimestamp()} {(multiline ? content.Replace("\n", GetLogsPadding(), StringComparison.Ordinal) : content)}", color);
134140
}
135141
}
136142
}

IO/Logging/LoggerWithColors.cs

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
using System;
2+
using System.IO;
3+
using System.Text;
4+
5+
namespace LocalAdmin.V2.IO.Logging;
6+
7+
public static class LoggerWithColors
8+
{
9+
private static StringBuilder? _sb;
10+
private static bool _logging;
11+
private static string? _logPath;
12+
private static ulong _totalLength, _totalEntries;
13+
internal const string LogFolderName = "LocalAdminLogsWithColors";
14+
15+
public static void Initialize()
16+
{
17+
if (!Core.LocalAdmin.EnableLogging)
18+
return;
19+
if (_logging)
20+
EndLogging();
21+
22+
string dir = Core.LocalAdmin.LaLogsPath ?? PathManager.GameUserDataRoot + LogFolderName + Path.DirectorySeparatorChar + Core.LocalAdmin.GamePort + Path.DirectorySeparatorChar;
23+
if (!Directory.Exists(dir))
24+
Directory.CreateDirectory(dir);
25+
26+
_totalLength = 0;
27+
_totalEntries = 0;
28+
_logPath = dir + $"LocalAdmin Log {DateTime.Now:yyyy-MM-dd HH.mm.ss}.txt";
29+
_logging = true;
30+
31+
Log($"{ConsoleUtil.GetLogsTimestamp()} Logging started.", ConsoleColor.White);
32+
Log($"{ConsoleUtil.GetLogsTimestamp()} Timezone offset: {DateTimeOffset.Now:zzz}", ConsoleColor.White);
33+
}
34+
35+
public static void EndLogging(bool bypass = false)
36+
{
37+
if (!_logging && !bypass) return;
38+
AppendLog($"{ConsoleUtil.GetLogsTimestamp()} --- END OF LOG ---", ConsoleColor.White, true, true);
39+
40+
_logging = false;
41+
_sb = null;
42+
}
43+
44+
private static string GetAnsiCode(ConsoleColor color)
45+
{
46+
return color switch
47+
{
48+
ConsoleColor.Black => "\x1b[30m",
49+
ConsoleColor.DarkBlue => "\x1b[34m",
50+
ConsoleColor.DarkGreen => "\x1b[32m",
51+
ConsoleColor.DarkCyan => "\x1b[36m",
52+
ConsoleColor.DarkRed => "\x1b[31m",
53+
ConsoleColor.DarkMagenta => "\x1b[35m",
54+
ConsoleColor.DarkYellow => "\x1b[33m",
55+
ConsoleColor.Gray => "\x1b[37m",
56+
ConsoleColor.DarkGray => "\x1b[90m",
57+
ConsoleColor.Blue => "\x1b[94m",
58+
ConsoleColor.Green => "\x1b[92m",
59+
ConsoleColor.Cyan => "\x1b[96m",
60+
ConsoleColor.Red => "\x1b[91m",
61+
ConsoleColor.Magenta => "\x1b[95m",
62+
ConsoleColor.Yellow => "\x1b[93m",
63+
ConsoleColor.White => "\x1b[97m",
64+
_ => "\x1b[0m"
65+
};
66+
}
67+
68+
private static void AppendLog(string text, ConsoleColor color, bool flush = false, bool bypass = false)
69+
{
70+
if (!_logging && !bypass) return;
71+
72+
try
73+
{
74+
string ansiPrefix = GetAnsiCode(color);
75+
string ansiSuffix = "\x1b[0m";
76+
string coloredText = ansiPrefix + text + ansiSuffix;
77+
78+
if (Core.LocalAdmin.AutoFlush)
79+
File.AppendAllText(_logPath!, coloredText + Environment.NewLine);
80+
else
81+
{
82+
_sb ??= new StringBuilder();
83+
_sb.AppendLine(coloredText);
84+
85+
if (_sb.Length > 1000 || flush)
86+
{
87+
File.AppendAllText(_logPath!, _sb.ToString());
88+
_sb.Clear();
89+
}
90+
}
91+
92+
if (bypass)
93+
return;
94+
95+
_totalEntries++;
96+
_totalLength += (uint)coloredText.Length;
97+
98+
if (_totalEntries > Core.LocalAdmin.LogEntriesLimit && Core.LocalAdmin.LogEntriesLimit > 0)
99+
{
100+
_logging = false;
101+
AppendLog($"{ConsoleUtil.GetLogsTimestamp()} Log entries limit exceeded. Logging stopped.", ConsoleColor.White, flush: true, bypass: true);
102+
EndLogging(true);
103+
}
104+
105+
if (_totalLength > Core.LocalAdmin.LogLengthLimit && Core.LocalAdmin.LogLengthLimit > 0)
106+
{
107+
_logging = false;
108+
AppendLog($"{ConsoleUtil.GetLogsTimestamp()} Log length limit exceeded. Logging stopped.", ConsoleColor.White, flush: true, bypass: true);
109+
EndLogging(true);
110+
}
111+
}
112+
catch (Exception e)
113+
{
114+
Console.Write("Failed to write log: " + e.Message);
115+
}
116+
}
117+
118+
public static void Log(string text, ConsoleColor color, bool flush = false) => AppendLog(text, color, flush);
119+
}

0 commit comments

Comments
 (0)