Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,9 @@ jobs:
- name: Build bare test
run: |
echo "Building bare test"
cd tests/bare/
haxe test.hxml
cd ../../
haxe tests/bare/test.hxml

- name: Build unit tests
run: |
echo "Building unit tests"
cd tests/unit/
haxe test.hxml
cd ../../
haxe tests/unit/test.hxml
13 changes: 2 additions & 11 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
{ "haxe.configurations":
[ [ "-cp", "test/src"
, "-cp", "lib"
, "-D", "analyzer-optimize"
// , "-D", "log=verbose"
// , "-D", "special.log=[info,ERROR]"
// , "-D", "special.throw=[VERBOSE]"
// , "-D", "special.log=NONE"
// , "-D", "special.throw=NONE"
, "-main", "Main"
, "--interp"
]
[ [ "tests/bare/test.hxml" ]
, [ "tests/unit/test.hxml", "-D logger.unit_test.show_styles" ]
]
//, "haxe.displayServer": { // configuration for starting haxe completion server itself
// "arguments": ["-v"], // arguments before --wait (-v is useful for debugging)
Expand Down
113 changes: 86 additions & 27 deletions lib/debug/Logger.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -55,28 +56,51 @@ 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)
dynamic static public function globalLog(msg:Any, ?pos:PosInfos)
{
haxe.Log.trace(msg, pos);
}

/**
* Set this to make a custom log formatter, and log will use this to format it's information
*/
dynamic static public function globalFormatter(id:Null<String>, priority:Priority, msg:Any, ?pos:PosInfos)
dynamic static public function globalFormatter(id:Null<String>, priority:Priority, msg:Any, ?pos:PosInfos):Any
{
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 colorizeMessage(priority, result);
#end
}

static public function colorizeMessage(priority:Priority, msg:String):String
{
return switch priority
{
case ERROR:
// msg.style([COLOR_FG(RED), BOLD]);
msg.color(RED);
case WARN:
msg.color(YELLOW);
case INFO:
msg;
case NONE:
msg.color(WHITE);
case VERBOSE:
msg.style(DIM);
}
}

static final list = new Map<String, LoggerRaw>();
Expand All @@ -96,15 +120,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;
}
}
}

Expand All @@ -126,7 +154,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];

Expand Down Expand Up @@ -177,14 +205,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();
Expand All @@ -193,8 +213,17 @@ 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)
{
final isEmpty = logLevels.isEmpty();
if (logLevels.isEmpty() == false)
logFinal(NONE, msg, pos);
}
Expand Down Expand Up @@ -393,7 +422,7 @@ abstract PriorityList(Array<Priority>) from Array<Priority>
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';
Expand Down Expand Up @@ -544,3 +573,33 @@ class LoggerDefines
}
#end
}


private class LoggerTools
{
static final contextFinder = ~/\[(.*?)\]/g;

/**
* Removes style
*/
static public function sanitizeID(id:String)
{
return id.unstyle().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, "");
}
}
90 changes: 90 additions & 0 deletions lib/debug/ansi/Color.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package debug.ansi;

import haxe.xml.Access;

/**
* Class representing a color, based on Int
*/
enum abstract Color(Int) from Int from UInt to Int to UInt
{
var BLACK = 0x000000; // 30
var RED = 0xFF0000; // 31
var GREEN = 0x00FF00; // 32
var YELLOW = 0xFFFF00; // 33
var BLUE = 0x0000FF; // 34
var MAGENTA = 0xFF00FF; // 35
var CYAN = 0x00FFFF; // 36
var WHITE = 0xFFFFFF; // 37
var GRAY = 0x808080;
var ORANGE = 0xFFA500;
var PURPLE = 0x800080;
var PINK = 0xFFC0CB;
var BROWN = 0x8B4513;

public var red (get, never):UInt;
public var blue (get, never):UInt;
public var green(get, never):UInt;

inline function get_red ():UInt return (this >> 16) & 0xFF;
inline function get_green():UInt return (this >> 8) & 0xFF;
inline function get_blue ():UInt return (this >> 0) & 0xFF;

/**
* Set RGB values as integers (0 to 255)
*
* @param red The red value of the color from 0 to 255
* @param green The green value of the color from 0 to 255
* @param blue The green value of the color from 0 to 255
* @return This color
*/
inline public function set(red:UInt, green:UInt, blue:UInt):Color
{
return this = concat(red, green, blue);
}

/**
* Creates a color from an RGB int
*
* @param value Usually a hex integer, I.E.: 0xFF00FF
* @param clampBits If true, discards extra bits, I.E.: 0xFF00FF00 becomes 0x00FF00
*/
overload public inline extern function new(value:UInt, clampBits = false)
{
this = clampBits ? value & 0xFFFFFF : value;
}

overload public inline extern function new(red:UInt, green:UInt, blue:UInt)
{
this = concat(red, green, blue);
}

/**
* Returns a new color with extra bits discarded
*/
inline public function to24Bit()
{
return new Color(this, true);
}

/**
* Creates a hex string representing this color, in the format "0xRRGGBB"
*/
public function toString()
{
return '0x' + StringTools.lpad(StringTools.hex(this), "0", 6);
}

/**
* Internal helper for concatenating three color bytes into 1 24bit integer
*
* @param red The red value of the color from 0 to 255
* @param green The green value of the color from 0 to 255
* @param blue The green value of the color from 0 to 255
*/
static public inline function concat(red:UInt, green:UInt, blue:UInt)
{
return ((red & 0xFF) << 16)
| ((green & 0xFF) << 8)
| ((blue & 0xFF) << 0);
}
}
Loading
Loading