forked from RedBigz/Citruslib-FixedUp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogger.cs
More file actions
159 lines (132 loc) · 5.31 KB
/
Logger.cs
File metadata and controls
159 lines (132 loc) · 5.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
using System;
using UnityEngine;
namespace WobbleBridge
{
// Logging changes and stuff
public static partial class Wobble
{
// Internal logger for WobbleBridge debugging — don't use this, make your own Logger!
internal static Logger log = new Logger("WobbleBridge", ConsoleColor.Cyan);
internal static bool landLogSupressed = false;
// Takes over Landfall's logger to match our format
internal static Logger landLog = new Logger("LandLog", ConsoleColor.Gray);
}
/// <summary>
/// A class for prettier debug logging.
/// </summary>
public class Logger
{
string modName;
ConsoleColor modColor;
static string lastMessage = "";
static int combo = 1;
/// <summary>
/// Creates a new Logger for your mod.
/// </summary>
/// <param name="n">The name of your mod, or a sub-name if your mod has multiple separate parts.</param>
/// <param name="c">The color of the name prefix.</param>
public Logger(string n, ConsoleColor c = ConsoleColor.White)
{
modName = n;
modColor = c;
}
/// <summary>
/// Logs a standard message to the console.
/// </summary>
public void Log(string text) => Write(text, ConsoleColor.Gray);
/// <summary>
/// Logs a warning to the console (yellow).
/// </summary>
public void LogWarning(string text) => Write(text, ConsoleColor.Yellow, warning: true);
/// <summary>
/// Logs an error to the console (red).
/// </summary>
public void LogError(string text) => Write(text, ConsoleColor.Red, error: true);
// ─── Internal Write ───────────────────────────────────────────────────────
private void Write(string text, ConsoleColor textColor, bool error = false, bool warning = false)
{
if (this == Wobble.landLog && Wobble.landLogSupressed)
return;
if (Compare(text, lastMessage) / Mathf.Max(1f, Mathf.Max(text.Length, lastMessage.Length)) < 0.15f)
{
ClearLastLine();
combo++;
}
else
{
combo = 1;
}
// Prefix
Console.ForegroundColor = ConsoleColor.White;
Console.Write("[");
Console.ForegroundColor = modColor;
Console.Write(modName);
Console.ForegroundColor = ConsoleColor.White;
Console.Write("] ");
// Severity tag
if (error)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.Write("[ERROR] ");
}
else if (warning)
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write("[WARN] ");
}
Console.Write("- ");
// Message
Console.ForegroundColor = textColor;
Console.Write(text);
// Combo counter
if (combo > 1)
{
Console.ForegroundColor = combo switch
{
< 10 => ConsoleColor.White,
< 25 => ConsoleColor.Yellow,
< 50 => ConsoleColor.DarkYellow,
< 100 => ConsoleColor.Red,
< 500 => ConsoleColor.DarkRed,
_ => combo % 2 == 0 ? ConsoleColor.DarkRed : ConsoleColor.Yellow
};
Console.Write($" (COMBO: {combo})");
}
// Unity error hook (shows in Unity console / log file)
if (error)
Debug.LogError($"[{modName}] {text}");
else if (warning)
Debug.LogWarning($"[{modName}] {text}");
else
Console.WriteLine("");
lastMessage = text;
}
// ─── Helpers ──────────────────────────────────────────────────────────────
static void ClearLastLine()
{
Console.SetCursorPosition(0, Console.CursorTop - 1);
Console.Write(new string(' ', Console.BufferWidth));
Console.SetCursorPosition(0, Console.CursorTop - 1);
}
// Levenshtein distance — used to detect repeated/similar messages for the combo system
static int Compare(string s, string t)
{
if (string.IsNullOrEmpty(s)) return string.IsNullOrEmpty(t) ? 0 : t.Length;
if (string.IsNullOrEmpty(t)) return s.Length;
int n = s.Length;
int m = t.Length;
int[,] d = new int[n + 1, m + 1];
for (int i = 0; i <= n; d[i, 0] = i++) ;
for (int j = 1; j <= m; d[0, j] = j++) ;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
int cost = t[j - 1] == s[i - 1] ? 0 : 1;
d[i, j] = Math.Min(Math.Min(d[i - 1, j] + 1, d[i, j - 1] + 1), d[i - 1, j - 1] + cost);
}
}
return d[n, m];
}
}
}