X-Git-Url: http://www.git.cypherpunks.ru/?a=blobdiff_plain;f=log.go;h=ebfb39493043b630f6b839ff75d6cecd54c0a91c;hb=534c8fdbe4a61d920fd6aa7ba2a8003adb672c7e;hp=335ca53c3d0a146079286a4c4b04caed91c6f450;hpb=bc7701e7a4f95cee680e0736ec3e68a8b0b5c09f;p=goredo.git diff --git a/log.go b/log.go index 335ca53..ebfb394 100644 --- a/log.go +++ b/log.go @@ -20,12 +20,22 @@ along with this program. If not, see . package main import ( + "flag" "fmt" "os" "strings" + "sync" ) const ( + EnvLevel = "REDO_LEVEL" + EnvDebug = "REDO_DEBUG" + EnvLogWait = "REDO_LOG_WAIT" + EnvLogLock = "REDO_LOG_LOCK" + EnvLogPid = "REDO_LOG_PID" + EnvLogJS = "REDO_LOG_JS" + EnvNoColor = "NO_COLOR" + CReset = "\033[0m" CBold = "\033[1m" CBlack = "\033[30;1m" @@ -48,12 +58,21 @@ const ( ) var ( + Level = 0 NoColor bool Debug bool LogWait bool LogLock bool LogJS bool MyPid int + + 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{}) { @@ -63,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 { @@ -94,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 {