logger fix
- introduce quit, drained, shutdown channels - mainLoop falls through reading message channel to drained state, and waits is blocked in default branch until any message is sent - Flush() waits for <-drained - Stop() pushes quit and nodges mainloop out of blocking drained state - package-global mutex - Reset() - clear tests
This commit is contained in:
parent
4fb2905b1e
commit
d4300c406c
@ -40,6 +40,9 @@ func (msg *logMessage) send(logger LogSystem) {
|
|||||||
var logMessages chan (*logMessage)
|
var logMessages chan (*logMessage)
|
||||||
var logSystems []LogSystem
|
var logSystems []LogSystem
|
||||||
var quit chan bool
|
var quit chan bool
|
||||||
|
var drained chan bool
|
||||||
|
var shutdown chan bool
|
||||||
|
var mutex = sync.Mutex{}
|
||||||
|
|
||||||
type LogLevel uint8
|
type LogLevel uint8
|
||||||
|
|
||||||
@ -57,29 +60,41 @@ func start() {
|
|||||||
out:
|
out:
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
|
case <-quit:
|
||||||
|
break out
|
||||||
case msg := <-logMessages:
|
case msg := <-logMessages:
|
||||||
for _, logSystem := range logSystems {
|
for _, logSystem := range logSystems {
|
||||||
if logSystem.GetLogLevel() >= msg.LogLevel {
|
if logSystem.GetLogLevel() >= msg.LogLevel {
|
||||||
msg.send(logSystem)
|
msg.send(logSystem)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
case <-quit:
|
case drained <- true:
|
||||||
break out
|
default:
|
||||||
|
drained <- true // this blocks until a message is sent to the queu
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
close(shutdown)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Reset() {
|
||||||
|
mutex.Lock()
|
||||||
|
defer mutex.Unlock()
|
||||||
|
if logSystems != nil {
|
||||||
|
quit <- true
|
||||||
|
select {
|
||||||
|
case <-drained:
|
||||||
|
}
|
||||||
|
<-shutdown
|
||||||
|
}
|
||||||
|
logSystems = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// waits until log messages are drained (dispatched to log writers)
|
// waits until log messages are drained (dispatched to log writers)
|
||||||
func Flush() {
|
func Flush() {
|
||||||
quit <- true
|
mutex.Lock()
|
||||||
|
defer mutex.Unlock()
|
||||||
done:
|
if logSystems != nil {
|
||||||
for {
|
<-drained
|
||||||
select {
|
|
||||||
case <-logMessages:
|
|
||||||
default:
|
|
||||||
break done
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -92,28 +107,34 @@ func NewLogger(tag string) *Logger {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func AddLogSystem(logSystem LogSystem) {
|
func AddLogSystem(logSystem LogSystem) {
|
||||||
var mutex = &sync.Mutex{}
|
|
||||||
mutex.Lock()
|
mutex.Lock()
|
||||||
defer mutex.Unlock()
|
defer mutex.Unlock()
|
||||||
if logSystems == nil {
|
if logSystems == nil {
|
||||||
logMessages = make(chan *logMessage)
|
logMessages = make(chan *logMessage)
|
||||||
quit = make(chan bool)
|
quit = make(chan bool)
|
||||||
|
drained = make(chan bool, 1)
|
||||||
|
shutdown = make(chan bool, 1)
|
||||||
go start()
|
go start()
|
||||||
}
|
}
|
||||||
logSystems = append(logSystems, logSystem)
|
logSystems = append(logSystems, logSystem)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func send(msg *logMessage) {
|
||||||
|
select {
|
||||||
|
case <-drained:
|
||||||
|
}
|
||||||
|
logMessages <- msg
|
||||||
|
}
|
||||||
|
|
||||||
func (logger *Logger) sendln(level LogLevel, v ...interface{}) {
|
func (logger *Logger) sendln(level LogLevel, v ...interface{}) {
|
||||||
if logMessages != nil {
|
if logSystems != nil {
|
||||||
msg := newPrintlnLogMessage(level, logger.tag, v...)
|
send(newPrintlnLogMessage(level, logger.tag, v...))
|
||||||
logMessages <- msg
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (logger *Logger) sendf(level LogLevel, format string, v ...interface{}) {
|
func (logger *Logger) sendf(level LogLevel, format string, v ...interface{}) {
|
||||||
if logMessages != nil {
|
if logSystems != nil {
|
||||||
msg := newPrintfLogMessage(level, logger.tag, format, v...)
|
send(newPrintfLogMessage(level, logger.tag, format, v...))
|
||||||
logMessages <- msg
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -28,10 +28,6 @@ func (t *TestLogSystem) GetLogLevel() LogLevel {
|
|||||||
return t.level
|
return t.level
|
||||||
}
|
}
|
||||||
|
|
||||||
func quote(s string) string {
|
|
||||||
return fmt.Sprintf("'%s'", s)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestLoggerPrintln(t *testing.T) {
|
func TestLoggerPrintln(t *testing.T) {
|
||||||
logger := NewLogger("TEST")
|
logger := NewLogger("TEST")
|
||||||
testLogSystem := &TestLogSystem{level: WarnLevel}
|
testLogSystem := &TestLogSystem{level: WarnLevel}
|
||||||
@ -41,10 +37,10 @@ func TestLoggerPrintln(t *testing.T) {
|
|||||||
logger.Infoln("info")
|
logger.Infoln("info")
|
||||||
logger.Debugln("debug")
|
logger.Debugln("debug")
|
||||||
Flush()
|
Flush()
|
||||||
|
Reset()
|
||||||
output := testLogSystem.Output
|
output := testLogSystem.Output
|
||||||
fmt.Println(quote(output))
|
|
||||||
if output != "[TEST] error\n[TEST] warn\n" {
|
if output != "[TEST] error\n[TEST] warn\n" {
|
||||||
t.Error("Expected logger output '[TEST] error\\n[TEST] warn\\n', got ", quote(testLogSystem.Output))
|
t.Error("Expected logger output '[TEST] error\\n[TEST] warn\\n', got ", testLogSystem.Output)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -57,10 +53,10 @@ func TestLoggerPrintf(t *testing.T) {
|
|||||||
logger.Infof("info")
|
logger.Infof("info")
|
||||||
logger.Debugf("debug")
|
logger.Debugf("debug")
|
||||||
Flush()
|
Flush()
|
||||||
|
Reset()
|
||||||
output := testLogSystem.Output
|
output := testLogSystem.Output
|
||||||
fmt.Println(quote(output))
|
|
||||||
if output != "[TEST] error to { 2}\n[TEST] warn" {
|
if output != "[TEST] error to { 2}\n[TEST] warn" {
|
||||||
t.Error("Expected logger output '[TEST] error to { 2}\\n[TEST] warn', got ", quote(testLogSystem.Output))
|
t.Error("Expected logger output '[TEST] error to { 2}\\n[TEST] warn', got ", testLogSystem.Output)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -73,13 +69,14 @@ func TestMultipleLogSystems(t *testing.T) {
|
|||||||
logger.Errorln("error")
|
logger.Errorln("error")
|
||||||
logger.Warnln("warn")
|
logger.Warnln("warn")
|
||||||
Flush()
|
Flush()
|
||||||
|
Reset()
|
||||||
output0 := testLogSystem0.Output
|
output0 := testLogSystem0.Output
|
||||||
output1 := testLogSystem1.Output
|
output1 := testLogSystem1.Output
|
||||||
if output0 != "[TEST] error\n" {
|
if output0 != "[TEST] error\n" {
|
||||||
t.Error("Expected logger 0 output '[TEST] error\\n', got ", quote(testLogSystem0.Output))
|
t.Error("Expected logger 0 output '[TEST] error\\n', got ", testLogSystem0.Output)
|
||||||
}
|
}
|
||||||
if output1 != "[TEST] error\n[TEST] warn\n" {
|
if output1 != "[TEST] error\n[TEST] warn\n" {
|
||||||
t.Error("Expected logger 1 output '[TEST] error\\n[TEST] warn\\n', got ", quote(testLogSystem1.Output))
|
t.Error("Expected logger 1 output '[TEST] error\\n[TEST] warn\\n', got ", testLogSystem1.Output)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -92,11 +89,11 @@ func TestFileLogSystem(t *testing.T) {
|
|||||||
logger.Errorf("error to %s\n", filename)
|
logger.Errorf("error to %s\n", filename)
|
||||||
logger.Warnln("warn")
|
logger.Warnln("warn")
|
||||||
Flush()
|
Flush()
|
||||||
|
Reset()
|
||||||
contents, _ := ioutil.ReadFile(filename)
|
contents, _ := ioutil.ReadFile(filename)
|
||||||
output := string(contents)
|
output := string(contents)
|
||||||
fmt.Println(quote(output))
|
|
||||||
if output != "[TEST] error to test.log\n[TEST] warn\n" {
|
if output != "[TEST] error to test.log\n[TEST] warn\n" {
|
||||||
t.Error("Expected contents of file 'test.log': '[TEST] error to test.log\\n[TEST] warn\\n', got ", quote(output))
|
t.Error("Expected contents of file 'test.log': '[TEST] error to test.log\\n[TEST] warn\\n', got ", output)
|
||||||
} else {
|
} else {
|
||||||
os.Remove(filename)
|
os.Remove(filename)
|
||||||
}
|
}
|
||||||
@ -105,5 +102,7 @@ func TestFileLogSystem(t *testing.T) {
|
|||||||
func TestNoLogSystem(t *testing.T) {
|
func TestNoLogSystem(t *testing.T) {
|
||||||
logger := NewLogger("TEST")
|
logger := NewLogger("TEST")
|
||||||
logger.Warnln("warn")
|
logger.Warnln("warn")
|
||||||
|
fmt.Println("1")
|
||||||
Flush()
|
Flush()
|
||||||
|
Reset()
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user