- Add mutex to prevent interleaved log output from concurrent goroutines - Make logMsg delegate to logPrintf for consistent synchronization - Buffer events channel to size 1 with select/drop when full - Close events channel on ListenForTrainer exit so process terminates
24 lines
342 B
Go
24 lines
342 B
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"sync"
|
|
"time"
|
|
)
|
|
|
|
var logMu sync.Mutex
|
|
|
|
func logMsg(msg string) {
|
|
logPrintf("%s\n", msg)
|
|
}
|
|
|
|
func logPrintf(format string, a ...any) {
|
|
logMu.Lock()
|
|
fmt.Printf(time.Now().Format("2006-01-02 15:04:05")+" "+format, a...)
|
|
logMu.Unlock()
|
|
}
|
|
|
|
func logMsgf(format string, a ...any) {
|
|
logPrintf(format+"\n", a...)
|
|
}
|