diff options
Diffstat (limited to 'vendor/go.mau.fi/libsignal/logger')
-rw-r--r-- | vendor/go.mau.fi/libsignal/logger/DefaultLogger.go | 85 | ||||
-rw-r--r-- | vendor/go.mau.fi/libsignal/logger/Logger.go | 89 |
2 files changed, 174 insertions, 0 deletions
diff --git a/vendor/go.mau.fi/libsignal/logger/DefaultLogger.go b/vendor/go.mau.fi/libsignal/logger/DefaultLogger.go new file mode 100644 index 00000000..62515bff --- /dev/null +++ b/vendor/go.mau.fi/libsignal/logger/DefaultLogger.go @@ -0,0 +1,85 @@ +package logger + +import ( + "fmt" + "strings" + "time" +) + +// DefaultLogger is used if no logger has been set up. +type defaultLogger struct { + namespaces []string +} + +// log simply logs the given message to stdout if the message +// caller is allowed to log. +func (d *defaultLogger) log(level, caller, msg string) { + if !d.shouldLog(caller) { + // return + } + t := time.Now() + fmt.Println( + "["+level+"]", + t.Format(time.RFC3339), + caller, + "▶ ", + msg, + ) +} + +// shouldLog determines whether or not the given caller should +// be allowed to log messages. +func (d *defaultLogger) shouldLog(caller string) bool { + shouldLog := false + d.ensureNamespaces() + for _, namespace := range d.namespaces { + if namespace == "all" { + shouldLog = true + } + if strings.Contains(caller, namespace) { + shouldLog = true + } + } + + return shouldLog +} + +// ensureNamespaces checks to see if our list of loggable namespaces +// has been initialized or not. If not, it defaults to log all. +func (d *defaultLogger) ensureNamespaces() { + if d.namespaces == nil { + d.namespaces = []string{"all"} + } +} + +// Debug is used to log debug messages. +func (d *defaultLogger) Debug(caller, msg string) { + //d.log("DEBUG", caller, msg) +} + +// Info is used to log info messages. +func (d *defaultLogger) Info(caller, msg string) { + d.log("INFO", caller, msg) +} + +// Warning is used to log warning messages. +func (d *defaultLogger) Warning(caller, msg string) { + d.log("WARNING", caller, msg) +} + +// Error is used to log error messages. +func (d *defaultLogger) Error(caller, msg string) { + d.log("ERROR", caller, msg) +} + +// Configure takes a configuration string separated by commas +// that contains all the callers that should be logged. This +// allows granular logging of different go files. +// +// Example: +// logger.Configure("RootKey.go,Curve.go") +// logger.Configure("all") +// +func (d *defaultLogger) Configure(settings string) { + d.namespaces = strings.Split(settings, ",") +} diff --git a/vendor/go.mau.fi/libsignal/logger/Logger.go b/vendor/go.mau.fi/libsignal/logger/Logger.go new file mode 100644 index 00000000..653320bf --- /dev/null +++ b/vendor/go.mau.fi/libsignal/logger/Logger.go @@ -0,0 +1,89 @@ +// Package logger provides optional debug logging of the Signal library. +package logger + +import ( + "fmt" + "runtime" + "strconv" + "strings" +) + +// Logger is a shared loggable interface that this library will use for all log messages. +var Logger Loggable + +// Loggable is an interface for logging. +type Loggable interface { + Debug(caller, message string) + Info(caller, message string) + Warning(caller, message string) + Error(caller, message string) + Configure(settings string) +} + +// Setup will configure the shared logger to use the provided logger. +func Setup(logger *Loggable) { + Logger = *logger +} + +// ToString converts an arbitrary number of objects to a string for use in a logger. +func toString(a ...interface{}) string { + return fmt.Sprint(a...) +} + +// EnsureLogger will use the default logger if one was not set up. +func ensureLogger() { + if Logger == nil { + // fmt.Println("Error: No logger was configured. Use `logger.Setup` to configure a logger.") + Logger = &defaultLogger{} + } +} + +// GetCaller gets the go file name and line number that the logger was called from. +func getCaller() string { + var file string + _, path, line, _ := runtime.Caller(2) + paths := strings.Split(path, "/") + if len(paths) > 0 { + file = paths[len(paths)-1] + } else { + file = "<unkn>" + } + + return file + ":" + strconv.Itoa(line) +} + +/* + * Go methods used by the library for logging. + */ + +// Debug prints debug level logs. +func Debug(msg ...interface{}) { + ensureLogger() + Logger.Debug(getCaller(), toString(msg...)) +} + +// Info prints info level logs. +func Info(msg ...interface{}) { + ensureLogger() + Logger.Info(getCaller(), toString(msg...)) +} + +// Warning prints warning level logs. +func Warning(msg ...interface{}) { + ensureLogger() + Logger.Warning(getCaller(), toString(msg...)) +} + +// Error prints error level logs. +func Error(msg ...interface{}) { + ensureLogger() + Logger.Error(getCaller(), toString(msg...)) +} + +// Configure allows arbitrary logger configuration settings. The +// default logger uses this method to configure what Go files +// are allowed to log. +func Configure(settings string) { + ensureLogger() + Logger.Configure(settings) +} |