Skip to content

Commit

Permalink
Option to set logging verbosity and override handler
Browse files Browse the repository at this point in the history
  • Loading branch information
tonimelisma committed Nov 8, 2020
1 parent c5a00ea commit 494b219
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
3 changes: 3 additions & 0 deletions vips/govips.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ func Startup(config *Config) {
cName := C.CString("govips")
defer freeCString(cName)

// Initialize govips logging handler and verbosity filter to historical default
LoggingSettings(nil, logLevelInfo)

// Override default glib logging handler to intercept logging messages
C.vips_set_logging_handler()

Expand Down
46 changes: 44 additions & 2 deletions vips/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,46 @@ const (
logLevelDebug logLevel = C.G_LOG_LEVEL_DEBUG
)

// Two global variables which keep state of the current logging handler
// function and desired verbosity for logging. Set by LoggingSettings()
var (
currentLoggingHandlerFunction LoggingHandlerFunction
currentLoggingVerbosity logLevel
)

// govipsLoggingHandler is the private bridge function exported to the C library
// and called 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))
}

func govipsLog(messageDomain string, messageLevel logLevel, message string) {
// 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).
// Define nil to use standard logging handler.
// Verbosity is the minimum logLevel you want to pass forward. Default is logLevelInfo
// due to backwards compatibility but it's very verbose for a library.
// Suggest setting it to at least logLevelWarning. Use logLevelDebug for debugging.
func LoggingSettings(handler LoggingHandlerFunction, verbosity logLevel) {
if handler == nil {
currentLoggingHandlerFunction = defaultLoggingHandlerFunction
} else {
currentLoggingHandlerFunction = handler
}

currentLoggingVerbosity = verbosity
}

func defaultLoggingHandlerFunction(messageDomain string, messageLevel logLevel, message string) {
var messageLevelDescription string
switch logLevel(messageLevel) {
case logLevelError:
Expand All @@ -38,5 +72,13 @@ func govipsLog(messageDomain string, messageLevel logLevel, message string) {
messageLevelDescription = "debug"
}

log.Println(messageDomain, "[", messageLevelDescription, "]", message)
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)
}
}

0 comments on commit 494b219

Please sign in to comment.