From 751857e09161eb9bbe393d131526c111903965ea Mon Sep 17 00:00:00 2001 From: GeoKureli-BlackbookPro Date: Fri, 8 May 2026 13:05:21 -0500 Subject: [PATCH 1/8] move funcs --- lib/debug/Logger.hx | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/debug/Logger.hx b/lib/debug/Logger.hx index 67d3f4f..7efd1d4 100644 --- a/lib/debug/Logger.hx +++ b/lib/debug/Logger.hx @@ -177,14 +177,6 @@ private class LoggerRaw verbose = new LoggerPriority(this, VERBOSE); } - #if logger.unit_test - public function resetFromCompilerFlags() - { - logLevels.resetFromCompilerFlags(LOG, id); - throwLevels.resetFromCompilerFlags(THROW, id); - } - #end - public function destroy() { error.destroy(); @@ -193,6 +185,14 @@ private class LoggerRaw verbose.destroy(); } + #if logger.unit_test + public function resetFromCompilerFlags() + { + logLevels.resetFromCompilerFlags(LOG, id); + throwLevels.resetFromCompilerFlags(THROW, id); + } + #end + public function log(msg:Any, ?pos:PosInfos) { if (logLevels.isEmpty() == false) From f42ea01cdfb9b0d492941887c615f56a61ab22b8 Mon Sep 17 00:00:00 2001 From: GeoKureli-BlackbookPro Date: Fri, 8 May 2026 16:30:00 -0500 Subject: [PATCH 2/8] add colors and ignore ANSI in ids --- lib/debug/Logger.hx | 90 +++++++++++++--- lib/debug/ansi/StyleTools.hx | 193 +++++++++++++++++++++++++++++++++++ tests/bare/src/Main.hx | 20 ++-- tests/bare/test.hxml | 2 +- tests/unit/src/Main.hx | 20 ++++ 5 files changed, 298 insertions(+), 27 deletions(-) create mode 100644 lib/debug/ansi/StyleTools.hx diff --git a/lib/debug/Logger.hx b/lib/debug/Logger.hx index 7efd1d4..5b4534e 100644 --- a/lib/debug/Logger.hx +++ b/lib/debug/Logger.hx @@ -3,6 +3,7 @@ package debug; import debug.Assert; import haxe.PosInfos; +using debug.ansi.StyleTools; /** * Tool used to simplify the categorization of logs, and easily customize which type of logs * are displayed, and which throw exceptions. @@ -55,9 +56,7 @@ abstract Logger(LoggerRaw) from LoggerRaw } /** - * Controls how each every Logger will actually log the message, this can also be set for each - * individual logger with: - * `myLogger.log = (msg, ?pos)->haxe.Log.trace('[${getTimestamp()}] $msg', pos);` + * Controls how each every Logger will actually log the message */ dynamic static public function globalLog(msg:String, ?pos:PosInfos) { @@ -69,14 +68,36 @@ abstract Logger(LoggerRaw) from LoggerRaw */ dynamic static public function globalFormatter(id:Null, priority:Priority, msg:Any, ?pos:PosInfos) { - return if (id != null && priority != NONE) - '$id[$priority]: $msg'; + final result = + if (id != null && priority != NONE) + '${id}[$priority]: $msg'; else if (priority != NONE) - '$priority: $msg'; + '$priority: $msg'; else if (id != null) - '$id: $msg'; + '$id: $msg'; else - '$msg'; + '$msg'; + + #if logger.no_color + return result; + #else + return switch priority + { + case ERROR: + // result.style([COLOR_FG(RED), BOLD]); + result.style(RED); + case WARN: + result.style(YELLOW); + case INFO: + result; + case NONE: + result.style(WHITE); + case VERBOSE: + result.style(DIM); + } + #end + + return msg; } static final list = new Map(); @@ -96,15 +117,19 @@ abstract Logger(LoggerRaw) from LoggerRaw { this = (cast Logger.log: LoggerRaw); } - else if (list.exists(id)) - { - // Note: do not set priority again - this = list[id]; - } else { - this = LoggerRaw.fromLevels(id, priority, throwPriority); - list[id] = this; + final sanitizedID = LoggerTools.sanitizeID(id); + if (list.exists(sanitizedID)) + { + // Note: do not set priority again + this = list[sanitizedID]; + } + else + { + this = LoggerRaw.fromLevels(id, priority, throwPriority); + list[sanitizedID] = this; + } } } @@ -126,7 +151,7 @@ abstract Logger(LoggerRaw) from LoggerRaw if (this.id == null) return new Logger(subID); - final fullID = '${this.id}.$subID'; + final fullID = LoggerTools.sanitizeID('${this.id}.$subID'); if (list.exists(fullID)) return list[fullID]; @@ -195,6 +220,7 @@ private class LoggerRaw public function log(msg:Any, ?pos:PosInfos) { + final isEmpty = logLevels.isEmpty(); if (logLevels.isEmpty() == false) logFinal(NONE, msg, pos); } @@ -393,7 +419,7 @@ abstract PriorityList(Array) from Array if (id == null) return fromGlobalCompilerFlag(type, backup); - final id = contextFinder.replace(id.toLowerCase(), ""); + final id = LoggerTools.removeContext(LoggerTools.sanitizeID(id)); // check if flags are cached for this id final key = '$id.$type'; @@ -544,3 +570,33 @@ class LoggerDefines } #end } + + +private class LoggerTools +{ + static final contextFinder = ~/\[(.*?)\]/g; + + /** + * Removes style + */ + static public function sanitizeID(id:String) + { + return id.removeStyle().toLowerCase(); + } + + /** + * Changes "Main[Foo].Sub[Context]" to "Main.Foo.Sub.Context" + */ + static public function subContext(id:String) + { + return contextFinder.replace(id, ".$1"); + } + + /** + * Removes context, ex: "Main[Foo].Sub[Context]" to "Main.Sub" + */ + static public function removeContext(id:String) + { + return contextFinder.replace(id, ""); + } +} \ No newline at end of file diff --git a/lib/debug/ansi/StyleTools.hx b/lib/debug/ansi/StyleTools.hx new file mode 100644 index 0000000..8974512 --- /dev/null +++ b/lib/debug/ansi/StyleTools.hx @@ -0,0 +1,193 @@ +package debug.ansi; + +class StyleTools +{ + overload static public inline extern function removeStyle(string:String):String + { + // #if interp + final split = string.split("\u001b["); + for (i in 1...split.length)// skip first + { + final str = split[i]; + split[i] = str.substr(str.indexOf("m") + 1); + } + + return split.join(""); + // #else // TODO: Test on various targets + // try + // { + // static final remover = ~/\u001b\[[0-9;]+m/g; + // return remover.split(string).join(""); + // } + // catch(e) + // { + // throw "Error creating EReg, create an issue, here: https://github.com/Geokureli/Logger/issues/new" + // } + // #end + } + + overload static public inline extern function style(string:String, style:Style):String + { + return apply(string, style); + } + + overload static public inline extern function style(string:String, styles:Array