-/*
-goredo -- djb's redo implementation on pure Go
-Copyright (C) 2020-2023 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
-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 <http://www.gnu.org/licenses/>.
-*/
+// goredo -- djb's redo implementation on pure Go
+// 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
+// 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 <http://www.gnu.org/licenses/>.
// Logging facilities
flagLogPid *bool
flagLogJS *bool
- LogMutex sync.Mutex
- KeyEraseLine string
+ LogMutex sync.Mutex
+ KeyEraseLine string
+ LastLoggedTgt string
)
func init() {
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 {
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 {
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 {