Skip to content

Instantly share code, notes, and snippets.

@mastef
Created January 18, 2018 08:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mastef/d217e6a2fdd6b4e6ddc5fe11f55d048d to your computer and use it in GitHub Desktop.
Save mastef/d217e6a2fdd6b4e6ddc5fe11f55d048d to your computer and use it in GitHub Desktop.
haxe trace to console.log with objects instead of strings
// check browser console in https://try.haxe.org/#cFBDb
class Trace {
static function main() {
var a = {};
Reflect.setProperty(a, "b", "c");
trace("before");
trace("a");
trace(a);
trace("a", a);
trace(123);
haxe.Log.trace = consoleTrace;
trace("after");
trace("a");
trace(a);
trace("a", a);
trace(123);
}
static function consoleTrace(v:Dynamic, ?infos:haxe.PosInfos) {
var args = formatOutput(v,infos);
Reflect.callMethod(js.Browser.console, js.Browser.console.log, args);
}
public static function formatOutput( v : Dynamic, infos : haxe.PosInfos ) : Array<Dynamic> {
var arr = [v];
if( infos == null )
return arr;
var pstr = infos.fileName + ":" + infos.lineNumber;
if( infos != null && infos.customParams != null ) for( v in infos.customParams ) arr.push(v);
arr.unshift(pstr);
return arr;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment