summaryrefslogtreecommitdiffstats
path: root/vendor/go.mau.fi/libsignal/logger/Logger.go
blob: 653320bf7333dafffb7d2db87dd9e0362f81bac5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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)
}