/* goredo -- redo implementation on pure Go Copyright (C) 2020 Sergey Matveev This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 3 of the License. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ // Logging facilities package main import ( "bytes" "flag" "fmt" "os" "strings" "sync" "golang.org/x/term" ) 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" ) var ( Level = 0 NoColor bool Debug bool LogWait bool LogLock bool LogJS bool MyPid int CDebug string CRedo string CWait string CLock string CErr string CWarn string CJS string CReset string CNone string = "NONE" 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 LogLenPrev int ) func init() { var b bytes.Buffer t := term.NewTerminal(&b, "") CDebug = string(t.Escape.Yellow) CRedo = string(t.Escape.Green) CWait = string(t.Escape.Blue) CLock = string(t.Escape.Cyan) CErr = string(t.Escape.Red) CWarn = string(t.Escape.Magenta) CJS = string(t.Escape.White) CReset = string(t.Escape.Reset) } func fillUpToTermSize(s, end string) string { sLen := len(s) if sLen < LogLenPrev { s += strings.Repeat(" ", LogLenPrev-sLen) LogLenPrev = sLen } else { LogLenPrev = sLen } return s + end } func trace(level, format string, args ...interface{}) { var p string if MyPid != 0 { p = fmt.Sprintf("[%d] ", MyPid) } switch level { case CNone: LogMutex.Lock() os.Stderr.WriteString(fillUpToTermSize( StderrPrefix+p+fmt.Sprintf(format, args...), "\n", )) LogMutex.Unlock() return case CDebug: if !Debug { return } p += "dbg " case CWait: if !(LogWait || Debug) { return } p += "wait " case CRedo: p += "redo " case CLock: if !(LogLock || Debug) { return } p += "lock " case CJS: if !(LogJS || Debug) { return } p += "js " case CErr: p += "err " case CWarn: p += "warn " } msg := fmt.Sprintf(format, args...) msg = StderrPrefix + colourize(level, p+strings.Repeat(". ", Level)+msg) LogMutex.Lock() os.Stderr.WriteString(fillUpToTermSize(msg, "\n")) LogMutex.Unlock() } func colourize(colour, s string) string { if NoColor { return s } return colour + s + CReset }