forked from davidbyttow/govips
/
logging.go
97 lines (85 loc) · 3.87 KB
/
logging.go
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
90
91
92
93
94
95
96
97
package vips
// #include <glib.h>
import "C"
import (
"log"
)
// LogLevel is the enum controlling logging message verbosity.
type LogLevel int
// The logging verbosity levels classify and filter logging messages.
// From most to least verbose, they are debug, info, message, warning, critical and error.
const (
LogLevelError LogLevel = C.G_LOG_LEVEL_ERROR
LogLevelCritical LogLevel = C.G_LOG_LEVEL_CRITICAL
LogLevelWarning LogLevel = C.G_LOG_LEVEL_WARNING
LogLevelMessage LogLevel = C.G_LOG_LEVEL_MESSAGE
LogLevelInfo LogLevel = C.G_LOG_LEVEL_INFO
LogLevelDebug LogLevel = C.G_LOG_LEVEL_DEBUG
)
// Three global variables which keep state of the current logging handler
// function, desired verbosity for logging and whether defaults have been
// overridden. Set by LoggingSettings()
var (
currentLoggingHandlerFunction LoggingHandlerFunction
currentLoggingVerbosity LogLevel
currentLoggingOverridden bool
)
// govipsLoggingHandler is the private bridge function exported to the C library
// and called by glib and libvips for each logging message. It will call govipsLog
// which in turn will filter based on verbosity and direct the messages to the
// currently chosen LoggingHandlerFunction.
//export govipsLoggingHandler
func govipsLoggingHandler(messageDomain *C.char, messageLevel C.int, message *C.char) {
govipsLog(C.GoString(messageDomain), LogLevel(messageLevel), C.GoString(message))
}
// LoggingHandlerFunction is a function which will be called for each log message.
// By default, govips sends logging messages to os.Stderr. If you want to log elsewhere
// such as to a file or to a state variable which you inspect yourself, define a new
// logging handler function and set it via LoggingSettings().
type LoggingHandlerFunction func(messageDomain string, messageLevel LogLevel, message string)
// LoggingSettings sets the logging handler and logging verbosity for govips.
// The handler function is the function which will be called for each log message.
// You can define one yourself to log somewhere else besides the default (stderr).
// Use nil as handler to use standard logging handler.
// Verbosity is the minimum logLevel you want to log. Default is logLevelInfo
// due to backwards compatibility but it's quite verbose for a library.
// Suggest setting it to at least logLevelWarning. Use logLevelDebug for debugging.
func LoggingSettings(handler LoggingHandlerFunction, verbosity LogLevel) {
currentLoggingOverridden = true
govipsLoggingSettings(handler, verbosity)
}
func govipsLoggingSettings(handler LoggingHandlerFunction, verbosity LogLevel) {
if handler == nil {
currentLoggingHandlerFunction = defaultLoggingHandlerFunction
} else {
currentLoggingHandlerFunction = handler
}
currentLoggingVerbosity = verbosity
// TODO turn on debugging in libvips and redirect to handler when setting verbosity to debug
// This way debugging information would go to the same channel as all other logging
}
func defaultLoggingHandlerFunction(messageDomain string, messageLevel LogLevel, message string) {
var messageLevelDescription string
switch messageLevel {
case LogLevelError:
messageLevelDescription = "error"
case LogLevelCritical:
messageLevelDescription = "critical"
case LogLevelWarning:
messageLevelDescription = "warning"
case LogLevelMessage:
messageLevelDescription = "message"
case LogLevelInfo:
messageLevelDescription = "info"
case LogLevelDebug:
messageLevelDescription = "debug"
}
log.Printf("[%v.%v] %v", messageDomain, messageLevelDescription, message)
}
// govipsLog is the default function used to log debug or error messages internally in govips.
// It's used by all govips functionality directly, as well as by glib and libvips via the C bridge.
func govipsLog(messageDomain string, messageLevel LogLevel, message string) {
if messageLevel <= currentLoggingVerbosity {
currentLoggingHandlerFunction(messageDomain, messageLevel, message)
}
}