]> Cypherpunks.ru repositories - goredo.git/blobdiff - log.go
Simple real-time status messages
[goredo.git] / log.go
diff --git a/log.go b/log.go
index f4fa2ca9c65f647b8aa1d48d71232984b76bed90..ebfb39493043b630f6b839ff75d6cecd54c0a91c 100644 (file)
--- a/log.go
+++ b/log.go
@@ -24,6 +24,7 @@ import (
        "fmt"
        "os"
        "strings"
+       "sync"
 )
 
 const (
@@ -65,11 +66,13 @@ var (
        LogJS   bool
        MyPid   int
 
-       flagDebug   = flag.Bool("debug", false, "enable debug logging (REDO_DEBUG=1)")
-       flagLogWait = flag.Bool("log-wait", false, "enable wait messages logging (REDO_LOG_WAIT=1)")
-       flagLogLock = flag.Bool("log-lock", false, "enable lock messages logging (REDO_LOG_LOCK=1)")
-       flagLogPid  = flag.Bool("log-pid", false, "append PIDs (REDO_LOG_PID=1)")
-       flagLogJS   = flag.Bool("log-js", false, "enable jobserver messages logging (REDO_LOG_JS=1)")
+       flagDebug   = flag.Bool("debug", false, fmt.Sprintf("enable debug logging (%s=1)", EnvDebug))
+       flagLogWait = flag.Bool("log-wait", false, fmt.Sprintf("enable wait messages logging (%s=1)", EnvLogWait))
+       flagLogLock = flag.Bool("log-lock", false, fmt.Sprintf("enable lock messages logging (%s=1)", EnvLogLock))
+       flagLogPid  = flag.Bool("log-pid", false, fmt.Sprintf("append PIDs (%s=1)", EnvLogPid))
+       flagLogJS   = flag.Bool("log-js", false, fmt.Sprintf("enable jobserver messages logging (%s=1)", EnvLogJS))
+
+       LogMutex sync.Mutex
 )
 
 func trace(level, format string, args ...interface{}) {
@@ -79,7 +82,9 @@ func trace(level, format string, args ...interface{}) {
        }
        switch level {
        case CNone:
+               LogMutex.Lock()
                os.Stderr.WriteString(StderrPrefix + p + fmt.Sprintf(format+"\n", args...))
+               LogMutex.Unlock()
                return
        case CDebug:
                if !Debug {
@@ -110,7 +115,9 @@ func trace(level, format string, args ...interface{}) {
        }
        msg := fmt.Sprintf(format, args...)
        msg = StderrPrefix + colourize(level, p+strings.Repeat(". ", Level)+msg)
+       LogMutex.Lock()
        os.Stderr.WriteString(msg + "\n")
+       LogMutex.Unlock()
 }
 
 func colourize(colour, s string) string {