]> Cypherpunks.ru repositories - goredo.git/blobdiff - log.go
Raise copyright years in advance
[goredo.git] / log.go
diff --git a/log.go b/log.go
index bb286edc02ad4c1b4ecff886fb861e9510f2ac9c..08d885aa25423060a41ed111c927e14b309cd7ac 100644 (file)
--- a/log.go
+++ b/log.go
@@ -1,6 +1,6 @@
 /*
 goredo -- djb's redo implementation on pure Go
-Copyright (C) 2020-2022 Sergey Matveev <stargrave@stargrave.org>
+Copyright (C) 2020-2024 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
@@ -49,7 +49,7 @@ var (
        LogWait    bool
        LogLock    bool
        LogJS      bool
-       MyPid      int
+       MyPID      int
 
        CDebug string
        CRedo  string
@@ -68,8 +68,9 @@ var (
        flagLogPid     *bool
        flagLogJS      *bool
 
-       LogMutex     sync.Mutex
-       KeyEraseLine string
+       LogMutex      sync.Mutex
+       KeyEraseLine  string
+       LastLoggedTgt string
 )
 
 func init() {
@@ -102,23 +103,49 @@ func init() {
 }
 
 func erasedStatus(s, end string) string {
-       if NoProgress {
+       if NoProgress || NoColor {
                return s + end
        }
        return s + KeyEraseLine + end
 }
 
+func withPrependedTgt(s string) {
+       if s[0] != '[' {
+               stderrWrite(erasedStatus(s, "\n"))
+               return
+       }
+       i := strings.IndexByte(s, ']')
+       if i == -1 {
+               stderrWrite(s)
+               return
+       }
+       tgt, s := s[1:i], s[i+1:]
+       if tgt != LastLoggedTgt {
+               LastLoggedTgt = tgt
+               tgt = "redo " + tgt + " ..."
+               if MyPID != 0 {
+                       tgt = fmt.Sprintf("[%d] %s", MyPID, tgt)
+               }
+               stderrWrite(erasedStatus(colourize(CDebug, tgt), "\n"))
+       }
+       stderrWrite(erasedStatus(s, "\n"))
+}
+
+func stderrWrite(s string) {
+       LogMutex.Lock()
+       os.Stderr.WriteString(s)
+       LogMutex.Unlock()
+}
+
 func tracef(level, format string, args ...interface{}) {
        var p string
-       if MyPid != 0 {
-               p = fmt.Sprintf("[%d] ", MyPid)
+       if MyPID != 0 {
+               p = fmt.Sprintf("[%d] ", MyPID)
        }
        switch level {
        case CNone:
                p = erasedStatus(StderrPrefix+p+fmt.Sprintf(format, args...), "\n")
-               LogMutex.Lock()
-               os.Stderr.WriteString(p)
-               LogMutex.Unlock()
+               stderrWrite(p)
                return
        case CDebug:
                if !Debug {
@@ -153,9 +180,7 @@ func tracef(level, format string, args ...interface{}) {
        msg := fmt.Sprintf(format, args...)
        msg = StderrPrefix + colourize(level, p+strings.Repeat(". ", Level)+msg)
        msg = erasedStatus(msg, "\n")
-       LogMutex.Lock()
-       os.Stderr.WriteString(msg)
-       LogMutex.Unlock()
+       stderrWrite(msg)
 }
 
 func colourize(colour, s string) string {