Use AnsiUtil for AnsiTrace, and highlight traces with headers

This commit is contained in:
MoonDroid
2025-10-23 17:29:55 -04:00
committed by Hundrec
parent 0d8fd214a1
commit eb1d54e750
+18 -20
View File
@@ -10,17 +10,22 @@ import sys.io.FileOutput;
import sys.io.File;
#end
using StringTools;
/**
* Class that helps with some Ansi related logging functionality like some terminal color checking
*/
@:nullSafety
class AnsiTrace
{
#if sys
private static final HEADER_REGEX = ~/^\s*\[(.*?)\]\s*(.*)$/;
#if (sys && FEATURE_DEBUG_FILE_LOGGING)
private static final logFilePath:String = 'logs/log-${DateUtil.generateTimestamp()}.txt';
private static var logFile:Null<FileOutput> = null;
private static var logFileClosed:Bool = false;
#end
#end
/**
* Output a message to the log.
@@ -77,14 +82,6 @@ class AnsiTrace
public static var colorSupported:Bool = #if sys (Sys.getEnv("TERM")?.startsWith('xterm')
|| Sys.getEnv("ANSICON") != null) #else false #end;
// ansi stuff
static inline var RED = "\x1b[31m";
static inline var YELLOW = "\x1b[33m";
static inline var WHITE = "\x1b[37m";
static inline var NORMAL = "\x1b[0m";
static inline var BOLD = "\x1b[1m";
static inline var ITALIC = "\x1b[3m";
/**
* Format the output to use ANSI colors.
* Edited from the standard `trace()` implementation.
@@ -97,15 +94,26 @@ class AnsiTrace
if (colorSupported)
{
var dirs:Array<String> = infos.fileName.split("/");
dirs[dirs.length - 1] = ansiWrap(dirs[dirs.length - 1], BOLD);
dirs[dirs.length - 1] = AnsiUtil.apply(dirs[dirs.length - 1], [BOLD]);
// rejoin the dirs
infos.fileName = dirs.join("/");
}
var pstr:String = infos.fileName + ":" + ansiWrap(infos.lineNumber, BOLD);
var pstr:String = infos.fileName + ":" + AnsiUtil.apply(infos.lineNumber, [BOLD]);
if (infos.customParams != null) for (v in infos.customParams)
str += ", " + Std.string(v);
var header:String = "";
var body:String = str;
if (HEADER_REGEX.match(str))
{
header = ' ${HEADER_REGEX.matched(1)} ';
body = HEADER_REGEX.matched(2);
}
if (header.ltrim() != '') str = '${AnsiUtil.apply(header, [BG_WHITE, BOLD])} ${body}';
return pstr + ": " + str;
}
@@ -124,16 +132,6 @@ class AnsiTrace
#end
}
static function ansiWrap(str:Dynamic, ansiCol:String)
{
return ansify(ansiCol) + str + ansify(NORMAL);
}
static function ansify(ansiCol:String)
{
return (colorSupported ? ansiCol : "");
}
/**
* Color pixel art of BF in ANSI format.
* Generated using https://dom111.github.io/image-to-ansi/