summaryrefslogtreecommitdiffstats
path: root/vendor/go.mau.fi/whatsmeow/util/log
diff options
context:
space:
mode:
authorWim <wim@42.be>2022-01-31 00:27:37 +0100
committerWim <wim@42.be>2022-03-20 14:57:48 +0100
commite3cafeaf9292f67459ff1d186f68283bfaedf2ae (patch)
treeb69c39620aa91dba695b3b935c6651c0fb37ce75 /vendor/go.mau.fi/whatsmeow/util/log
parente7b193788a56ee7cdb02a87a9db0ad6724ef66d5 (diff)
downloadmatterbridge-msglm-e3cafeaf9292f67459ff1d186f68283bfaedf2ae.tar.gz
matterbridge-msglm-e3cafeaf9292f67459ff1d186f68283bfaedf2ae.tar.bz2
matterbridge-msglm-e3cafeaf9292f67459ff1d186f68283bfaedf2ae.zip
Add dependencies/vendor (whatsapp)
Diffstat (limited to 'vendor/go.mau.fi/whatsmeow/util/log')
-rw-r--r--vendor/go.mau.fi/whatsmeow/util/log/log.go83
1 files changed, 83 insertions, 0 deletions
diff --git a/vendor/go.mau.fi/whatsmeow/util/log/log.go b/vendor/go.mau.fi/whatsmeow/util/log/log.go
new file mode 100644
index 00000000..2662cdea
--- /dev/null
+++ b/vendor/go.mau.fi/whatsmeow/util/log/log.go
@@ -0,0 +1,83 @@
+// Copyright (c) 2021 Tulir Asokan
+//
+// This Source Code Form is subject to the terms of the Mozilla Public
+// License, v. 2.0. If a copy of the MPL was not distributed with this
+// file, You can obtain one at http://mozilla.org/MPL/2.0/.
+
+// Package waLog contains a simple logger interface used by the other whatsmeow packages.
+package waLog
+
+import (
+ "fmt"
+ "strings"
+ "time"
+)
+
+// Logger is a simple logger interface that can have subloggers for specific areas.
+type Logger interface {
+ Warnf(msg string, args ...interface{})
+ Errorf(msg string, args ...interface{})
+ Infof(msg string, args ...interface{})
+ Debugf(msg string, args ...interface{})
+ Sub(module string) Logger
+}
+
+type noopLogger struct{}
+
+func (n *noopLogger) Errorf(_ string, _ ...interface{}) {}
+func (n *noopLogger) Warnf(_ string, _ ...interface{}) {}
+func (n *noopLogger) Infof(_ string, _ ...interface{}) {}
+func (n *noopLogger) Debugf(_ string, _ ...interface{}) {}
+func (n *noopLogger) Sub(_ string) Logger { return n }
+
+// Noop is a no-op Logger implementation that silently drops everything.
+var Noop Logger = &noopLogger{}
+
+type stdoutLogger struct {
+ mod string
+ color bool
+ min int
+}
+
+var colors = map[string]string{
+ "INFO": "\033[36m",
+ "WARN": "\033[33m",
+ "ERROR": "\033[31m",
+}
+
+var levelToInt = map[string]int{
+ "": -1,
+ "DEBUG": 0,
+ "INFO": 1,
+ "WARN": 2,
+ "ERROR": 3,
+}
+
+func (s *stdoutLogger) outputf(level, msg string, args ...interface{}) {
+ if levelToInt[level] < s.min {
+ return
+ }
+ var colorStart, colorReset string
+ if s.color {
+ colorStart = colors[level]
+ colorReset = "\033[0m"
+ }
+ fmt.Printf("%s%s [%s %s] %s%s\n", time.Now().Format("15:04:05.000"), colorStart, s.mod, level, fmt.Sprintf(msg, args...), colorReset)
+}
+
+func (s *stdoutLogger) Errorf(msg string, args ...interface{}) { s.outputf("ERROR", msg, args...) }
+func (s *stdoutLogger) Warnf(msg string, args ...interface{}) { s.outputf("WARN", msg, args...) }
+func (s *stdoutLogger) Infof(msg string, args ...interface{}) { s.outputf("INFO", msg, args...) }
+func (s *stdoutLogger) Debugf(msg string, args ...interface{}) { s.outputf("DEBUG", msg, args...) }
+func (s *stdoutLogger) Sub(mod string) Logger {
+ return &stdoutLogger{mod: fmt.Sprintf("%s/%s", s.mod, mod), color: s.color, min: s.min}
+}
+
+// Stdout is a simple Logger implementation that outputs to stdout. The module name given is included in log lines.
+//
+// minLevel specifies the minimum log level to output. An empty string will output all logs.
+//
+// If color is true, then info, warn and error logs will be colored cyan, yellow and red respectively using ANSI color escape codes.
+func Stdout(module string, minLevel string, color bool) Logger {
+ return &stdoutLogger{mod: module, color: color, min: levelToInt[strings.ToUpper(minLevel)]}
+}