From 3eb4903f20183eeca0edfe404629f7b418cf54a6 Mon Sep 17 00:00:00 2001
From: Jono Wenger <jono@elixxir.io>
Date: Fri, 9 Sep 2022 22:09:21 -0700
Subject: [PATCH] Replace log init

---
 main.go         | 10 ++++++++++
 wasm/logging.go | 14 ++++++--------
 2 files changed, 16 insertions(+), 8 deletions(-)

diff --git a/main.go b/main.go
index 4d0b412d..46a170cf 100644
--- a/main.go
+++ b/main.go
@@ -11,6 +11,7 @@ package main
 
 import (
 	"fmt"
+	jww "github.com/spf13/jwalterweatherman"
 	"gitlab.com/elixxir/client/bindings"
 	"gitlab.com/elixxir/xxdk-wasm/utils"
 	"gitlab.com/elixxir/xxdk-wasm/wasm"
@@ -18,6 +19,15 @@ import (
 	"syscall/js"
 )
 
+func init() {
+	// Overwrites setting the log level to INFO done in bindings so that the
+	// Javascript console can be used
+	ll := &wasm.LogListener{
+		Threshold: jww.LevelInfo, Value: js.Global().Get("console")}
+	jww.SetLogListeners(ll.Listen)
+	jww.SetStdoutThreshold(jww.LevelFatal + 1)
+}
+
 func main() {
 	fmt.Println("Starting xxDK WebAssembly bindings.")
 	fmt.Printf("Client version %s\n", bindings.GetVersion())
diff --git a/wasm/logging.go b/wasm/logging.go
index 177a57e3..a39cf7fa 100644
--- a/wasm/logging.go
+++ b/wasm/logging.go
@@ -46,14 +46,13 @@ var logListeners []jww.LogListener
 // Returns:
 //  - Throws TypeError if the log level is invalid.
 func LogLevel(_ js.Value, args []js.Value) interface{} {
-	level := args[0].Int()
-	if level < 0 || level > 6 {
-		err := errors.Errorf("log level is not valid: log level: %d", level)
+	threshold := jww.Threshold(args[0].Int())
+	if threshold < jww.LevelTrace || threshold > jww.LevelFatal {
+		err := errors.Errorf("log level is not valid: log level: %d", threshold)
 		utils.Throw(utils.TypeError, err)
 		return nil
 	}
 
-	threshold := jww.Threshold(level)
 	jww.SetLogThreshold(threshold)
 	jww.SetFlags(log.LstdFlags | log.Lmicroseconds)
 
@@ -94,13 +93,12 @@ func LogLevel(_ js.Value, args []js.Value) interface{} {
 //  - A Javascript representation of the LogFile object, which allows accessing
 //    the contents of the log file and other metadata.
 func LogToFile(_ js.Value, args []js.Value) interface{} {
-	level := args[0].Int()
-	if level < 0 || level > 6 {
-		err := errors.Errorf("log level is not valid: log level: %d", level)
+	threshold := jww.Threshold(args[0].Int())
+	if threshold < jww.LevelTrace || threshold > jww.LevelFatal {
+		err := errors.Errorf("log level is not valid: log level: %d", threshold)
 		utils.Throw(utils.TypeError, err)
 		return nil
 	}
-	threshold := jww.Threshold(level)
 
 	// Create new buffer of the specified size
 	b, err := circbuf.NewBuffer(int64(args[2].Int()))
-- 
GitLab