mirror of
https://github.com/gogrlx/nats-server.git
synced 2026-04-14 02:07:59 -07:00
Govet doesn't like functions that look like format handlers not ending in `f`, such as Debug() vs Debugf(). This is changing some of the new log handling to use 'f' function names to appease govet. Updated the implicit handling of including the client as an arg without being used in a format string. Now the client object simply has a Errorf function for logging errors and it adds itself onto the format string.
85 lines
1.7 KiB
Go
85 lines
1.7 KiB
Go
// Copyright 2012-2014 Apcera Inc. All rights reserved.
|
|
|
|
package server
|
|
|
|
import (
|
|
"sync"
|
|
"sync/atomic"
|
|
)
|
|
|
|
var trace int32
|
|
var debug int32
|
|
var log = struct {
|
|
logger Logger
|
|
sync.Mutex
|
|
}{}
|
|
|
|
type Logger interface {
|
|
Noticef(format string, v ...interface{})
|
|
Fatalf(format string, v ...interface{})
|
|
Errorf(format string, v ...interface{})
|
|
Debugf(format string, v ...interface{})
|
|
Tracef(format string, v ...interface{})
|
|
}
|
|
|
|
func (s *Server) SetLogger(logger Logger, d, t bool) {
|
|
if d {
|
|
atomic.StoreInt32(&debug, 1)
|
|
}
|
|
|
|
if t {
|
|
atomic.StoreInt32(&trace, 1)
|
|
}
|
|
|
|
log.Lock()
|
|
defer log.Unlock()
|
|
log.logger = logger
|
|
}
|
|
|
|
func Noticef(format string, v ...interface{}) {
|
|
executeLogCall(func(logger Logger, format string, v ...interface{}) {
|
|
logger.Noticef(format, v...)
|
|
}, format, v...)
|
|
}
|
|
|
|
func Errorf(format string, v ...interface{}) {
|
|
executeLogCall(func(logger Logger, format string, v ...interface{}) {
|
|
logger.Errorf(format, v...)
|
|
}, format, v...)
|
|
}
|
|
|
|
func Fatalf(format string, v ...interface{}) {
|
|
executeLogCall(func(logger Logger, format string, v ...interface{}) {
|
|
logger.Fatalf(format, v...)
|
|
}, format, v...)
|
|
}
|
|
|
|
func Debugf(format string, v ...interface{}) {
|
|
if debug == 0 {
|
|
return
|
|
}
|
|
|
|
executeLogCall(func(logger Logger, format string, v ...interface{}) {
|
|
logger.Debugf(format, v...)
|
|
}, format, v...)
|
|
}
|
|
|
|
func Tracef(format string, v ...interface{}) {
|
|
if trace == 0 {
|
|
return
|
|
}
|
|
|
|
executeLogCall(func(logger Logger, format string, v ...interface{}) {
|
|
logger.Tracef(format, v...)
|
|
}, format, v...)
|
|
}
|
|
|
|
func executeLogCall(f func(logger Logger, format string, v ...interface{}), format string, args ...interface{}) {
|
|
log.Lock()
|
|
defer log.Unlock()
|
|
if log.logger == nil {
|
|
return
|
|
}
|
|
f(log.logger, format, args...)
|
|
}
|