mirror of
https://github.com/octoleo/syncthing.git
synced 2024-11-14 01:04:14 +00:00
154 lines
3.5 KiB
Go
154 lines
3.5 KiB
Go
// Copyright (C) 2014 Jakob Borg and Contributors (see the CONTRIBUTORS file).
|
|
//
|
|
// This program is free software: you can redistribute it and/or modify it
|
|
// under the terms of the GNU General Public License as published by the Free
|
|
// Software Foundation, either version 3 of the License, or (at your option)
|
|
// any later version.
|
|
//
|
|
// This program is distributed in the hope that it will be useful, but WITHOUT
|
|
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
// more details.
|
|
//
|
|
// You should have received a copy of the GNU General Public License along
|
|
// with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
// Package logger implements a standardized logger with callback functionality
|
|
package logger
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"strings"
|
|
"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{
|
|
logger: log.New(os.Stdout, "", log.Ltime),
|
|
}
|
|
}
|
|
|
|
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] {
|
|
h(level, strings.TrimSpace(s))
|
|
}
|
|
}
|
|
|
|
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)
|
|
os.Exit(1)
|
|
}
|
|
|
|
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)
|
|
os.Exit(1)
|
|
}
|