2014-07-12 22:45:33 +00:00
|
|
|
// Copyright (C) 2014 Jakob Borg and Contributors (see the CONTRIBUTORS file).
|
|
|
|
// All rights reserved. Use of this source code is governed by an MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
2014-06-01 20:50:14 +00:00
|
|
|
|
2014-05-15 03:40:17 +00:00
|
|
|
// Package logger implements a standardized logger with callback functionality
|
2014-05-15 00:08:56 +00:00
|
|
|
package logger
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
2014-07-31 12:14:19 +00:00
|
|
|
"strings"
|
2014-05-15 00:08:56 +00:00
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
type LogLevel int
|
|
|
|
|
|
|
|
const (
|
|
|
|
LevelDebug LogLevel = iota
|
|
|
|
LevelInfo
|
|
|
|
LevelOK
|
|
|
|
LevelWarn
|
|
|
|
LevelFatal
|
|
|
|
NumLevels
|
|
|
|
)
|
|
|
|
|
|
|
|
type MessageHandler func(l LogLevel, msg string)
|
|
|
|
|
|
|
|
type Logger struct {
|
|
|
|
logger *log.Logger
|
|
|
|
handlers [NumLevels][]MessageHandler
|
|
|
|
mut sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
var DefaultLogger = New()
|
|
|
|
|
|
|
|
func New() *Logger {
|
|
|
|
return &Logger{
|
2014-06-23 19:57:22 +00:00
|
|
|
logger: log.New(os.Stdout, "", log.Ltime),
|
2014-05-15 00:08:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Logger) AddHandler(level LogLevel, h MessageHandler) {
|
|
|
|
l.mut.Lock()
|
|
|
|
defer l.mut.Unlock()
|
|
|
|
l.handlers[level] = append(l.handlers[level], h)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Logger) SetFlags(flag int) {
|
|
|
|
l.logger.SetFlags(flag)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Logger) SetPrefix(prefix string) {
|
|
|
|
l.logger.SetPrefix(prefix)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Logger) callHandlers(level LogLevel, s string) {
|
|
|
|
for _, h := range l.handlers[level] {
|
2014-07-31 12:14:19 +00:00
|
|
|
h(level, strings.TrimSpace(s))
|
2014-05-15 00:08:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Logger) Debugln(vals ...interface{}) {
|
|
|
|
l.mut.Lock()
|
|
|
|
defer l.mut.Unlock()
|
|
|
|
s := fmt.Sprintln(vals...)
|
|
|
|
l.logger.Output(2, "DEBUG: "+s)
|
|
|
|
l.callHandlers(LevelDebug, s)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Logger) Debugf(format string, vals ...interface{}) {
|
|
|
|
l.mut.Lock()
|
|
|
|
defer l.mut.Unlock()
|
|
|
|
s := fmt.Sprintf(format, vals...)
|
|
|
|
l.logger.Output(2, "DEBUG: "+s)
|
|
|
|
l.callHandlers(LevelDebug, s)
|
|
|
|
}
|
|
|
|
func (l *Logger) Infoln(vals ...interface{}) {
|
|
|
|
l.mut.Lock()
|
|
|
|
defer l.mut.Unlock()
|
|
|
|
s := fmt.Sprintln(vals...)
|
|
|
|
l.logger.Output(2, "INFO: "+s)
|
|
|
|
l.callHandlers(LevelInfo, s)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Logger) Infof(format string, vals ...interface{}) {
|
|
|
|
l.mut.Lock()
|
|
|
|
defer l.mut.Unlock()
|
|
|
|
s := fmt.Sprintf(format, vals...)
|
|
|
|
l.logger.Output(2, "INFO: "+s)
|
|
|
|
l.callHandlers(LevelInfo, s)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Logger) Okln(vals ...interface{}) {
|
|
|
|
l.mut.Lock()
|
|
|
|
defer l.mut.Unlock()
|
|
|
|
s := fmt.Sprintln(vals...)
|
|
|
|
l.logger.Output(2, "OK: "+s)
|
|
|
|
l.callHandlers(LevelOK, s)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Logger) Okf(format string, vals ...interface{}) {
|
|
|
|
l.mut.Lock()
|
|
|
|
defer l.mut.Unlock()
|
|
|
|
s := fmt.Sprintf(format, vals...)
|
|
|
|
l.logger.Output(2, "OK: "+s)
|
|
|
|
l.callHandlers(LevelOK, s)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Logger) Warnln(vals ...interface{}) {
|
|
|
|
l.mut.Lock()
|
|
|
|
defer l.mut.Unlock()
|
|
|
|
s := fmt.Sprintln(vals...)
|
|
|
|
l.logger.Output(2, "WARNING: "+s)
|
|
|
|
l.callHandlers(LevelWarn, s)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Logger) Warnf(format string, vals ...interface{}) {
|
|
|
|
l.mut.Lock()
|
|
|
|
defer l.mut.Unlock()
|
|
|
|
s := fmt.Sprintf(format, vals...)
|
|
|
|
l.logger.Output(2, "WARNING: "+s)
|
|
|
|
l.callHandlers(LevelWarn, s)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Logger) Fatalln(vals ...interface{}) {
|
|
|
|
l.mut.Lock()
|
|
|
|
defer l.mut.Unlock()
|
|
|
|
s := fmt.Sprintln(vals...)
|
|
|
|
l.logger.Output(2, "FATAL: "+s)
|
|
|
|
l.callHandlers(LevelFatal, s)
|
2014-07-31 08:26:45 +00:00
|
|
|
os.Exit(1)
|
2014-05-15 00:08:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Logger) Fatalf(format string, vals ...interface{}) {
|
|
|
|
l.mut.Lock()
|
|
|
|
defer l.mut.Unlock()
|
|
|
|
s := fmt.Sprintf(format, vals...)
|
|
|
|
l.logger.Output(2, "FATAL: "+s)
|
|
|
|
l.callHandlers(LevelFatal, s)
|
2014-07-31 08:26:45 +00:00
|
|
|
os.Exit(1)
|
2014-05-15 00:08:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *Logger) FatalErr(err error) {
|
|
|
|
if err != nil {
|
2014-07-06 12:46:48 +00:00
|
|
|
l.mut.Lock()
|
|
|
|
defer l.mut.Unlock()
|
|
|
|
l.logger.SetFlags(l.logger.Flags() | log.Lshortfile)
|
|
|
|
l.logger.Output(2, "FATAL: "+err.Error())
|
|
|
|
l.callHandlers(LevelFatal, err.Error())
|
2014-07-31 08:26:45 +00:00
|
|
|
os.Exit(1)
|
2014-05-15 00:08:56 +00:00
|
|
|
}
|
|
|
|
}
|