Tracing to the “haxe:trace” div in Haxe 2.10 & 3.01 JS

Up until Haxe 2.10 using trace in the JS target wrote the contents of your trace to the <div> in the html file with the id “haxe:trace” something like this:

Main.hx:34: The contents of the trace

That’s changed with 2.10 with the content of the traces often being written to the browsers console (ctrl+shift+K in Windows or option+apple+K on a Mac).

Some kind folks on the mailing list gave me some pointers on how to switch back to writing to the <div id=”haxe:trace”> by setting the trace function to a method you’ve written yourself.

To change the trace method used by haxe you need to use ( and change divTrace to the name of your new trace method):

haxe.Log.trace = divTrace;

Here’s my method which traces out all the info in the PosInfos object which you can read a bit about here:

Haxe 2.10

public function divTrace(v:Dynamic, ?i :PosInfos)
{
	Lib.document.getElementById("haxe:trace").innerHTML +="<br>[" + i.fileName + " : " + i.lineNumber +"] " + i.className + "." + i.methodName + "() - " + v;
}

Haxe 3.01

public function divTrace(v:Dynamic, ?i :PosInfos)
{
	Browser.document.getElementById("haxe:trace").innerHTML +="<br>[" + i.fileName + " : " + i.lineNumber +"] " + i.className + "." + i.methodName + "() - " + v;
}

It writes something like this to the <div id=”haxe:trace”> in the html:
[Main.hx : 34] Main.new() - The contents of the trace

 

Advertisement