]> Cypherpunks.ru repositories - goredo.git/blobdiff - log.go
Repetitive OOD optimization
[goredo.git] / log.go
diff --git a/log.go b/log.go
index 2745fd4ee71a05f90837f80f1fd2e2137f4e304b..703f198368d7b9886c91029962c58a4f3b40dead 100644 (file)
--- a/log.go
+++ b/log.go
@@ -1,6 +1,6 @@
 /*
-goredo -- redo implementation on pure Go
-Copyright (C) 2020 Sergey Matveev <stargrave@stargrave.org>
+goredo -- djb's redo implementation on pure Go
+Copyright (C) 2020-2021 Sergey Matveev <stargrave@stargrave.org>
 
 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
@@ -20,62 +20,71 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 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"
-
-       CReset   = "\033[0m"
-       CBold    = "\033[1m"
-       CBlack   = "\033[30;1m"
-       CRed     = "\033[31;1m"
-       CGreen   = "\033[32;1m"
-       CYellow  = "\033[33;1m"
-       CBlue    = "\033[34;1m"
-       CMagenta = "\033[35;1m"
-       CCyan    = "\033[36;1m"
-       CWhite   = "\033[37;1m"
-
-       CDebug = CYellow
-       CRedo  = CGreen
-       CWait  = CBlue
-       CLock  = CCyan
-       CErr   = CRed
-       CWarn  = CMagenta
-       CJS    = CWhite
-       CNone  = "NONE"
+       EnvLevel      = "REDO_LEVEL"
+       EnvNoProgress = "REDO_NO_PROGRESS"
+       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
-
-       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))
+       Level      = 0
+       NoColor    bool
+       NoProgress 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"
+
+       flagNoProgress = flag.Bool("no-progress", false, fmt.Sprintf("no progress printing (%s=1), also implies -no-status", EnvNoProgress))
+       flagDebug      = flag.Bool("d", 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 {
@@ -111,6 +120,9 @@ func trace(level, format string, args ...interface{}) {
                }
                p += "wait "
        case CRedo:
+               if NoProgress {
+                       return
+               }
                p += "redo "
        case CLock:
                if !(LogLock || Debug) {