unipdf/common/logging.go

86 lines
2.0 KiB
Go
Raw Normal View History

/*
* This file is subject to the terms and conditions defined in
2016-07-29 17:23:39 +00:00
* file 'LICENSE.md', which is part of this source code package.
*/
package common
2016-07-17 19:59:17 +00:00
import (
"fmt"
2017-01-21 14:47:55 +11:00
"os"
2017-01-12 17:52:29 +11:00
"path/filepath"
"runtime"
2016-07-17 19:59:17 +00:00
)
type Logger interface {
Error(format string, args ...interface{})
Warning(format string, args ...interface{})
Notice(format string, args ...interface{})
Info(format string, args ...interface{})
Debug(format string, args ...interface{})
}
// Dummy Logger does nothing.
type DummyLogger struct{}
func (this DummyLogger) Error(format string, args ...interface{}) {
}
func (this DummyLogger) Warning(format string, args ...interface{}) {
}
func (this DummyLogger) Notice(format string, args ...interface{}) {
}
func (this DummyLogger) Info(format string, args ...interface{}) {
}
func (this DummyLogger) Debug(format string, args ...interface{}) {
}
2016-07-17 19:59:17 +00:00
// Simple Console Logger that the tests use.
type ConsoleLogger struct{}
2017-01-26 20:20:13 +11:00
const DebugOutput = false
2016-07-17 19:59:17 +00:00
func (this ConsoleLogger) Error(format string, args ...interface{}) {
2017-01-21 14:47:55 +11:00
this.output(os.Stderr, "[ERROR] ", format, args...)
2016-07-17 19:59:17 +00:00
}
func (this ConsoleLogger) Warning(format string, args ...interface{}) {
2017-01-21 14:47:55 +11:00
this.output(os.Stdout, "[WARNING] ", format, args...)
2016-07-17 19:59:17 +00:00
}
func (this ConsoleLogger) Notice(format string, args ...interface{}) {
2017-01-21 14:47:55 +11:00
this.output(os.Stdout, "[NOTICE] ", format, args...)
}
2016-07-17 19:59:17 +00:00
func (this ConsoleLogger) Info(format string, args ...interface{}) {
2017-01-21 14:47:55 +11:00
this.output(os.Stdout, "[INFO] ", format, args...)
2016-07-17 19:59:17 +00:00
}
func (this ConsoleLogger) Debug(format string, args ...interface{}) {
2017-01-26 20:20:13 +11:00
if DebugOutput {
this.output(os.Stdout, "[DEBUG] ", format, args...)
}
2017-01-12 17:52:29 +11:00
}
2017-01-21 14:47:55 +11:00
func (this ConsoleLogger) output(f *os.File, prefix, format string, args ...interface{}) {
2017-01-12 17:52:29 +11:00
_, file, line, ok := runtime.Caller(3)
if !ok {
file = "???"
line = 0
} else {
file = filepath.Base(file)
}
2017-01-21 14:47:55 +11:00
src := fmt.Sprintf("%s%s:%d ", prefix, file, line) + format + "\n"
fmt.Fprintf(f, src, args...)
2016-07-17 19:59:17 +00:00
}
var Log Logger = DummyLogger{}
func SetLogger(logger Logger) {
Log = logger
2017-01-12 17:52:29 +11:00
fmt.Printf("SetLogger: logger=%+v\n", logger)
}