Skip to content
Snippets Groups Projects
Select Git revision
  • 4e83be61aca522249ee2354580059cc257fd6a01
  • release default protected
  • master protected
  • NationalTreasure/NotificationUpgrade
  • XX-4441
  • xx-4417/gw-poll-earliest-client-round
  • tls-websockets
  • hotfix/drain
  • hotfix/matcher
  • projects/crust_RELEASE
  • XX-4055/ChannelIdentityTracking
  • XX-4066/CrustUpgrade_MASTER
  • Ace/Huawei
  • hotfix/accumulate-notifs
  • XX-3564/TlsCipherSuite
  • hotfix/groupNotification
  • Anne/License-Update
  • hotfix/trustoldgatewaysonly
  • hotfix/notifications
  • notls
  • url-repo-rename
  • v0.0.4
  • v0.0.3
  • v0.0.2
  • v0.0.1
25 results

utils.go

Blame
  • logLevel.go 2.34 KiB
    ////////////////////////////////////////////////////////////////////////////////
    // Copyright © 2022 xx foundation                                             //
    //                                                                            //
    // Use of this source code is governed by a license that can be found in the  //
    // LICENSE file.                                                              //
    ////////////////////////////////////////////////////////////////////////////////
    
    //go:build js && wasm
    
    package logging
    
    import (
    	"fmt"
    	"github.com/pkg/errors"
    	jww "github.com/spf13/jwalterweatherman"
    	"gitlab.com/elixxir/xxdk-wasm/utils"
    	"log"
    	"syscall/js"
    )
    
    // LogLevel sets level of logging. All logs at the set level and below will be
    // displayed (e.g., when log level is ERROR, only ERROR, CRITICAL, and FATAL
    // messages will be printed).
    //
    // The default log level without updates is INFO.
    func LogLevel(threshold jww.Threshold) error {
    	if threshold < jww.LevelTrace || threshold > jww.LevelFatal {
    		return errors.Errorf("log level is not valid: log level: %d", threshold)
    	}
    
    	jww.SetLogThreshold(threshold)
    	jww.SetFlags(log.LstdFlags | log.Lmicroseconds)
    
    	ll := NewJsConsoleLogListener(threshold)
    	AddLogListener(ll.Listen)
    	jww.SetStdoutThreshold(jww.LevelFatal + 1)
    
    	msg := fmt.Sprintf("Log level set to: %s", threshold)
    	switch threshold {
    	case jww.LevelTrace:
    		fallthrough
    	case jww.LevelDebug:
    		fallthrough
    	case jww.LevelInfo:
    		jww.INFO.Print(msg)
    	case jww.LevelWarn:
    		jww.WARN.Print(msg)
    	case jww.LevelError:
    		jww.ERROR.Print(msg)
    	case jww.LevelCritical:
    		jww.CRITICAL.Print(msg)
    	case jww.LevelFatal:
    		jww.FATAL.Print(msg)
    	}
    
    	return nil
    }
    
    // LogLevelJS sets level of logging. All logs at the set level and below will be
    // displayed (e.g., when log level is ERROR, only ERROR, CRITICAL, and FATAL
    // messages will be printed).
    //
    // Log level options:
    //
    //	TRACE    - 0
    //	DEBUG    - 1
    //	INFO     - 2
    //	WARN     - 3
    //	ERROR    - 4
    //	CRITICAL - 5
    //	FATAL    - 6
    //
    // The default log level without updates is INFO.
    //
    // Parameters:
    //   - args[0] - Log level (int).
    //
    // Returns:
    //   - Throws TypeError if the log level is invalid.
    func LogLevelJS(_ js.Value, args []js.Value) any {
    	threshold := jww.Threshold(args[0].Int())
    	err := LogLevel(threshold)
    	if err != nil {
    		utils.Throw(utils.TypeError, err)
    		return nil
    	}
    
    	return nil
    }