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

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

  1. Nick Holder says:

    Updated the post as there was a mistake in the divTrace method meaning it would only display the last traced value.

    …innerHTML = “[” …

    should have been

    …innerHTML += “<br>[” …

  2. Nick Holder says:

    Updated to include haxe 3.01 snippet

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: