From 1b919a0615be0749eb9b0b686fcea4e725e72fab Mon Sep 17 00:00:00 2001
From: Jono Wenger <jono@elixxir.io>
Date: Fri, 19 Aug 2022 15:42:50 -0700
Subject: [PATCH] Fix logging to work properly with the console

---
 go.mod                   |  6 ++--
 go.sum                   | 12 +++++++
 test/assets/wasm_exec.js |  2 +-
 wasm/logging.go          | 77 ++++++++++++++++++++++++++++++++++++++--
 4 files changed, 91 insertions(+), 6 deletions(-)

diff --git a/go.mod b/go.mod
index 9da4c801..2d922785 100644
--- a/go.mod
+++ b/go.mod
@@ -3,8 +3,9 @@ module gitlab.com/elixxir/xxdk-wasm
 go 1.17
 
 require (
+	github.com/pkg/errors v0.9.1
 	github.com/spf13/jwalterweatherman v1.1.0
-	gitlab.com/elixxir/client v1.5.1-0.20220819191805-0a3f0ec5e022
+	gitlab.com/elixxir/client v1.5.1-0.20220819220235-f5f805caa59a
 )
 
 require (
@@ -25,7 +26,6 @@ require (
 	github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 // indirect
 	github.com/pelletier/go-toml v1.9.5 // indirect
 	github.com/pelletier/go-toml/v2 v2.0.2 // indirect
-	github.com/pkg/errors v0.9.1 // indirect
 	github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e // indirect
 	github.com/spf13/afero v1.9.2 // indirect
 	github.com/spf13/cast v1.5.0 // indirect
@@ -41,7 +41,7 @@ require (
 	gitlab.com/elixxir/bloomfilter v0.0.0-20211222005329-7d931ceead6f // indirect
 	gitlab.com/elixxir/comms v0.0.4-0.20220603231314-e47e4af13326 // indirect
 	gitlab.com/elixxir/crypto v0.0.7-0.20220606201132-c370d5039cea // indirect
-	gitlab.com/elixxir/ekv v0.1.8-0.20220819191231-b09ccfc34bdd // indirect
+	gitlab.com/elixxir/ekv v0.1.8-0.20220819215938-4404f37e6808 // indirect
 	gitlab.com/elixxir/primitives v0.0.3-0.20220606195757-40f7a589347f // indirect
 	gitlab.com/xx_network/comms v0.0.4-0.20220630163702-f3d372ef6acd // indirect
 	gitlab.com/xx_network/crypto v0.0.5-0.20220729193517-1e5e96f39f6e // indirect
diff --git a/go.sum b/go.sum
index fffda699..dcc8d3e2 100644
--- a/go.sum
+++ b/go.sum
@@ -426,6 +426,12 @@ gitlab.com/elixxir/client v1.5.1-0.20220819183619-ee6ed55cc2c2 h1:V9t1+3TrvMhvqY
 gitlab.com/elixxir/client v1.5.1-0.20220819183619-ee6ed55cc2c2/go.mod h1:43aEEoQg6N0wViL80TMT6ULeNja6t+YhAcdxFvoSba8=
 gitlab.com/elixxir/client v1.5.1-0.20220819191805-0a3f0ec5e022 h1:bpJYaiwuM55p5FrRwdIExxDDJmOnEcFNmKNBj0wh3qc=
 gitlab.com/elixxir/client v1.5.1-0.20220819191805-0a3f0ec5e022/go.mod h1:N/9levnzqNI6iBBSZDejHQ4n3mhExyR/isLUFrfon/U=
+gitlab.com/elixxir/client v1.5.1-0.20220819202428-47f9ac28e827 h1:EFAgofnriF40mAE6/dUyNUzcqvRu+qWp76koBhzOCYY=
+gitlab.com/elixxir/client v1.5.1-0.20220819202428-47f9ac28e827/go.mod h1:EZcAQPgiKX6CBh4gj46d0rS4d9NVUbjQr9nT43PFFD8=
+gitlab.com/elixxir/client v1.5.1-0.20220819205327-d0010b4a26e2 h1:pLJkqV19DYzcSqhlIvNyi9h/9xGqheG8gdygOxj0RNM=
+gitlab.com/elixxir/client v1.5.1-0.20220819205327-d0010b4a26e2/go.mod h1:j/pt/fd2kXvNeLJ1pH0ldmJmeVlrvCjdtJrlkahdNmE=
+gitlab.com/elixxir/client v1.5.1-0.20220819220235-f5f805caa59a h1:RPl7eyCByJRP2jBLXn3su93HB2GMj6o2hqM0T3/id2g=
+gitlab.com/elixxir/client v1.5.1-0.20220819220235-f5f805caa59a/go.mod h1:94BaZDB6nGJ3DmSZiqMkDzjSb/eed4LYy1cJSUctsIA=
 gitlab.com/elixxir/comms v0.0.4-0.20220603231314-e47e4af13326 h1:Zid8oNHtbOqF6ebrcGIccvIMabFNGh9dzY1b7mgIcF0=
 gitlab.com/elixxir/comms v0.0.4-0.20220603231314-e47e4af13326/go.mod h1:tlHSrtSliKWUxsck8z/Ql/VJkMdSONV2BeWaUAAXzgk=
 gitlab.com/elixxir/crypto v0.0.0-20200804182833-984246dea2c4/go.mod h1:ucm9SFKJo+K0N2GwRRpaNr+tKXMIOVWzmyUD0SbOu2c=
@@ -439,6 +445,12 @@ gitlab.com/elixxir/ekv v0.1.8-0.20220819182720-9469c5593e46 h1:/1YYi6J8WnLP5zt9O
 gitlab.com/elixxir/ekv v0.1.8-0.20220819182720-9469c5593e46/go.mod h1:e6WPUt97taFZe5PFLPb1Dupk7tqmDCTQu1kkstqJvw4=
 gitlab.com/elixxir/ekv v0.1.8-0.20220819191231-b09ccfc34bdd h1:0TKp/keclGT063K3Q8oDKQb54tRGfcdBqeMfgNwReeI=
 gitlab.com/elixxir/ekv v0.1.8-0.20220819191231-b09ccfc34bdd/go.mod h1:e6WPUt97taFZe5PFLPb1Dupk7tqmDCTQu1kkstqJvw4=
+gitlab.com/elixxir/ekv v0.1.8-0.20220819202333-0f81cb310b1c h1:pD5NCcU61PPe5+x7JI0TIOPN6ZsScPqJ2PLABJSkCJg=
+gitlab.com/elixxir/ekv v0.1.8-0.20220819202333-0f81cb310b1c/go.mod h1:e6WPUt97taFZe5PFLPb1Dupk7tqmDCTQu1kkstqJvw4=
+gitlab.com/elixxir/ekv v0.1.8-0.20220819205151-41fba013dd11 h1:z/IUTdDxoXMRqH/psoywyOBhODtG4JFKhBG3ljawZJs=
+gitlab.com/elixxir/ekv v0.1.8-0.20220819205151-41fba013dd11/go.mod h1:e6WPUt97taFZe5PFLPb1Dupk7tqmDCTQu1kkstqJvw4=
+gitlab.com/elixxir/ekv v0.1.8-0.20220819215938-4404f37e6808 h1:KVKOJ7drSsvqeS3dBZlNj78+33oImZV/Sj/IzfV81gk=
+gitlab.com/elixxir/ekv v0.1.8-0.20220819215938-4404f37e6808/go.mod h1:e6WPUt97taFZe5PFLPb1Dupk7tqmDCTQu1kkstqJvw4=
 gitlab.com/elixxir/primitives v0.0.0-20200731184040-494269b53b4d/go.mod h1:OQgUZq7SjnE0b+8+iIAT2eqQF+2IFHn73tOo+aV11mg=
 gitlab.com/elixxir/primitives v0.0.0-20200804170709-a1896d262cd9/go.mod h1:p0VelQda72OzoUckr1O+vPW0AiFe0nyKQ6gYcmFSuF8=
 gitlab.com/elixxir/primitives v0.0.0-20200804182913-788f47bded40/go.mod h1:tzdFFvb1ESmuTCOl1z6+yf6oAICDxH2NPUemVgoNLxc=
diff --git a/test/assets/wasm_exec.js b/test/assets/wasm_exec.js
index 0fc07d61..528d7465 100644
--- a/test/assets/wasm_exec.js
+++ b/test/assets/wasm_exec.js
@@ -502,7 +502,7 @@
 						console.log(value);
 					},
 
-					// func Throw(exception string, message string)
+					// func throw(exception string, message string)
 					'gitlab.com/elixxir/xxdk-wasm/wasm.throw': (sp) => {
 						const exception = loadString(sp + 8)
 						const message = loadString(sp + 24)
diff --git a/wasm/logging.go b/wasm/logging.go
index 14092aa7..4c2f833b 100644
--- a/wasm/logging.go
+++ b/wasm/logging.go
@@ -10,7 +10,11 @@
 package wasm
 
 import (
+	"github.com/pkg/errors"
+	jww "github.com/spf13/jwalterweatherman"
 	"gitlab.com/elixxir/client/bindings"
+	"io"
+	"log"
 	"syscall/js"
 )
 
@@ -35,15 +39,84 @@ import (
 // Returns:
 //  - Throws TypeError if the log level is invalid.
 func LogLevel(_ js.Value, args []js.Value) interface{} {
-	err := bindings.LogLevel(args[0].Int())
-	if err != nil {
+	level := args[0].Int()
+	if level < 0 || level > 6 {
+		err := errors.Errorf("log level is not valid: log level: %d", level)
 		Throw(TypeError, err)
 		return nil
 	}
 
+	threshold := jww.Threshold(level)
+	jww.SetLogThreshold(threshold)
+	jww.SetFlags(log.LstdFlags | log.Lmicroseconds)
+
+	ll := &LogListener{threshold, js.Global().Get("console")}
+	jww.SetLogListeners(ll.Listen)
+	jww.SetStdoutThreshold(jww.LevelFatal + 1)
+
+	switch threshold {
+	case jww.LevelTrace:
+		fallthrough
+	case jww.LevelDebug:
+		fallthrough
+	case jww.LevelInfo:
+		jww.INFO.Printf("Log level set to: %s", threshold)
+	case jww.LevelWarn:
+		jww.WARN.Printf("Log level set to: %s", threshold)
+	case jww.LevelError:
+		jww.ERROR.Printf("Log level set to: %s", threshold)
+	case jww.LevelCritical:
+		jww.CRITICAL.Printf("Log level set to: %s", threshold)
+	case jww.LevelFatal:
+		jww.FATAL.Printf("Log level set to: %s", threshold)
+	}
+
 	return nil
 }
 
+// console contains the Javascript console object, which provides access to the
+// browser's debugging console. This structure detects logging types and prints
+// it using the correct logging method.
+type console struct {
+	call string
+	js.Value
+}
+
+func (c *console) Write(p []byte) (n int, err error) {
+	c.Call(c.call, string(p))
+	return len(p), nil
+}
+
+type LogListener struct {
+	jww.Threshold
+	js.Value
+}
+
+func (ll *LogListener) Listen(t jww.Threshold) io.Writer {
+	if t < ll.Threshold {
+		return nil
+	}
+
+	switch t {
+	case jww.LevelTrace:
+		return &console{"debug", ll.Value}
+	case jww.LevelDebug:
+		return &console{"log", ll.Value}
+	case jww.LevelInfo:
+		return &console{"info", ll.Value}
+	case jww.LevelWarn:
+		return &console{"warn", ll.Value}
+	case jww.LevelError:
+		return &console{"error", ll.Value}
+	case jww.LevelCritical:
+		return &console{"error", ll.Value}
+	case jww.LevelFatal:
+		return &console{"error", ll.Value}
+	default:
+		return &console{"log", ll.Value}
+	}
+}
+
 // logWriter wraps Javascript callbacks to adhere to the [bindings.LogWriter]
 // interface.
 type logWriter struct {
-- 
GitLab