Use AnsiUtil for AnsiTrace, and highlight traces with headers
This commit is contained in:
@@ -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/
|
||||
|
||||
Reference in New Issue
Block a user