git » sdk » commit 41249a4

Use debug tracing in browser

author Stephen Paul Weber
2024-10-21 15:39:46 UTC
committer Stephen Paul Weber
2024-10-21 15:39:46 UTC
parent 9fa244477822fe9e8a85796240dddce9eada50e7

Use debug tracing in browser

So we can hide traces and see app logs if needed.

Also make customParams show up nicer using browser formatting instead of
the Haxe formatter.

snikket/Client.hx +1 -0
snikket/Util.hx +16 -0

diff --git a/snikket/Client.hx b/snikket/Client.hx
index 605cf9c..ffbd081 100644
--- a/snikket/Client.hx
+++ b/snikket/Client.hx
@@ -83,6 +83,7 @@ class Client extends EventEmitter {
 		@param persistence the persistence layer to use for storage
 	**/
 	public function new(address: String, persistence: Persistence) {
+		Util.setupTrace();
 		super();
 		this.jid = JID.parse(address);
 		this._displayName = this.jid.node;
diff --git a/snikket/Util.hx b/snikket/Util.hx
index 9f18718..da43ecf 100644
--- a/snikket/Util.hx
+++ b/snikket/Util.hx
@@ -1,5 +1,21 @@
 package snikket;
 
+function setupTrace() {
+#if js
+	haxe.Log.trace = (v, ?infos) -> {
+		if (js.Syntax.typeof(untyped console) != "undefined" && (untyped console).debug != null) {
+			final params = infos.customParams ?? [];
+			infos.customParams = [];
+			final str: Dynamic = haxe.Log.formatOutput(v, infos);
+			(untyped console).debug.apply(null, [str].concat(params));
+		} else if (js.Syntax.typeof(untyped console) != "undefined" && (untyped console).log != null) {
+			final str = haxe.Log.formatOutput(v, infos);
+			(untyped console).log(str);
+		}
+	}
+#end
+}
+
 // Std.downcast doesn't play well with null safety
 function downcast<T, S>(value: T, c: Class<S>): Null<S> {
 	return cast Std.downcast(cast value, cast c);