Skip to content
Merged
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
27 changes: 27 additions & 0 deletions lib/debug/Logger.hx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package debug;

import debug.Assert;
import haxe.PosInfos;
import haxe.macro.Expr;

/**
* Tool used to simplify the categorization of logs, and easily customize which type of logs
Expand Down Expand Up @@ -113,6 +114,12 @@ abstract Logger(LoggerRaw) from LoggerRaw
{
this.log(msg, pos);
}

/** Variadic log, allows any number of arguments but cannot specify the pos */
macro public function v(expr:Expr, args:Array<Expr>):Expr
{
return Logger.logV(expr, args);
}

/**
* Creates a sub-category of this category, capable of having its own priorities.
Expand All @@ -132,6 +139,26 @@ abstract Logger(LoggerRaw) from LoggerRaw

return list[fullID] = LoggerRaw.fromParent(subID, this);
}

/**
* Easy way to combine various args into a single string
*/
static public function vFormat(args:Array<Any>, delim = ", "):String
{
return args.map(Std.string).join(delim);
}

#if macro
/** Used by variadic log.v methods to make a single arg log with posInfos */
static public function logV(instance:Expr, args:Array<Expr>):Expr
{
return macro
{
@:pos(instance.pos)
$instance(debug.Logger.vFormat([$a{args}]));
};
}
#end
}

@:allow(debug.Logger)
Expand Down
7 changes: 7 additions & 0 deletions lib/debug/LoggerPriority.hx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package debug;

import debug.Logger;
import haxe.PosInfos;
import haxe.macro.Expr;

@:forward(enabled, throws, assert, level)
abstract LoggerPriority(LoggerPriorityRaw)
Expand All @@ -16,6 +17,12 @@ abstract LoggerPriority(LoggerPriorityRaw)
{
this.log(msg, pos);
}

/** Variadic log, allows any number of arguments but cannot specify the pos */
macro public function v(expr:ExprOf<Bool>, args:Array<Expr>):Expr
{
return Logger.logV(expr, args);
}

@:allow(debug.LoggerRaw)
inline function destroy()
Expand Down
3 changes: 2 additions & 1 deletion tests/bare/src/Main.hx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Main
log.sub("sub[context]").verbose("test sub log"); // ignored
log.verbose.enabled = true;
log.verbose("test log"); // Output: Main: VERBOSE:test log
log.verbose.v(1, "a", true); // Output: Main: VERBOSE: 1, a, true
try
{
log.error("test log"); // throws exception
Expand Down Expand Up @@ -82,4 +83,4 @@ class Main
gLog('exception caught: ${e.message}'); // Output: exception caught: Main[ERROR]:test log
}
}
}
}
Loading