Maintain constant call depth in logging to ensure correct line numbers. More tests needed, too lazy.

This commit is contained in:
Alex Bramley 2011-09-29 22:45:26 +01:00
parent 26a482d8ea
commit 9773b47969
1 changed files with 20 additions and 15 deletions

View File

@ -66,7 +66,7 @@ type logger struct {
*sync.Mutex // to ensure changing levels/flags is atomic *sync.Mutex // to ensure changing levels/flags is atomic
} }
var internal Logger var internal *logger
func init() { func init() {
/// Hopefully this won't cause pain and suffering /// Hopefully this won't cause pain and suffering
@ -106,12 +106,13 @@ func init() {
internal = New(out, lv, *only) internal = New(out, lv, *only)
} }
func New(out io.Writer, level int, only bool) Logger { func New(out io.Writer, level int, only bool) *logger {
l := log.New(out, "", log.LstdFlags) l := log.New(out, "", log.LstdFlags)
return &logger{l, level, only, &sync.Mutex{}} return &logger{l, level, only, &sync.Mutex{}}
} }
func (l *logger) Log(lv int, fm string, v ...interface{}) { // Internal function all others call to ensure identical call depth
func (l *logger) write(lv int, fm string, v ...interface{}) {
if lv > l.level { if lv > l.level {
// Your logs are not important to us, goodnight // Your logs are not important to us, goodnight
return return
@ -127,56 +128,60 @@ func (l *logger) Log(lv int, fm string, v ...interface{}) {
l.log.SetFlags(log.LstdFlags) l.log.SetFlags(log.LstdFlags)
} }
// Writing the log is deceptively simple // Writing the log is deceptively simple
l.log.Output(2, fmt.Sprintf(fm, v...)) l.log.Output(3, fmt.Sprintf(fm, v...))
if lv == LogFatal { if lv == LogFatal {
// Always fatal to stderr too. // Always fatal to stderr too.
log.Fatalf(fm, v...) log.Fatalf(fm, v...)
} }
} }
func (l *logger) Log(lv int, fm string, v ...interface{}) {
l.write(lv, fm, v...)
}
func Log(lv int, fm string, v ...interface{}) { func Log(lv int, fm string, v ...interface{}) {
internal.Log(lv, fm, v...) internal.write(lv, fm, v...)
} }
// Helper functions for specific levels // Helper functions for specific levels
func (l *logger) Debug(fm string, v ...interface{}) { func (l *logger) Debug(fm string, v ...interface{}) {
l.Log(LogDebug, fm, v...) l.write(LogDebug, fm, v...)
} }
func Debug(fm string, v ...interface{}) { func Debug(fm string, v ...interface{}) {
internal.Debug(fm, v...) internal.write(LogDebug, fm, v...)
} }
func (l *logger) Info(fm string, v ...interface{}) { func (l *logger) Info(fm string, v ...interface{}) {
l.Log(LogInfo, fm, v...) l.write(LogInfo, fm, v...)
} }
func Info(fm string, v ...interface{}) { func Info(fm string, v ...interface{}) {
internal.Info(fm, v...) internal.write(LogInfo, fm, v...)
} }
func (l *logger) Warn(fm string, v ...interface{}) { func (l *logger) Warn(fm string, v ...interface{}) {
l.Log(LogWarn, fm, v...) l.write(LogWarn, fm, v...)
} }
func Warn(fm string, v ...interface{}) { func Warn(fm string, v ...interface{}) {
internal.Warn(fm, v...) internal.write(LogWarn, fm, v...)
} }
func (l *logger) Error(fm string, v ...interface{}) { func (l *logger) Error(fm string, v ...interface{}) {
l.Log(LogError, fm, v...) l.write(LogError, fm, v...)
} }
func Error(fm string, v ...interface{}) { func Error(fm string, v ...interface{}) {
internal.Error(fm, v...) internal.write(LogError, fm, v...)
} }
func (l *logger) Fatal(fm string, v ...interface{}) { func (l *logger) Fatal(fm string, v ...interface{}) {
l.Log(LogFatal, fm, v...) l.write(LogFatal, fm, v...)
} }
func Fatal(fm string, v ...interface{}) { func Fatal(fm string, v ...interface{}) {
internal.Fatal(fm, v...) internal.write(LogFatal, fm, v...)
} }
func (l *logger) SetLogLevel(lv int) { func (l *logger) SetLogLevel(lv int) {