Created
January 18, 2018 08:41
-
-
Save mastef/d217e6a2fdd6b4e6ddc5fe11f55d048d to your computer and use it in GitHub Desktop.
haxe trace to console.log with objects instead of strings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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