From c252c75d3f44eac7ea7e88a062e41201cb2997e1 Mon Sep 17 00:00:00 2001 From: Sergey Matveev Date: Mon, 16 Oct 2023 16:07:47 +0300 Subject: [PATCH] Prefix target's output with the name --- doc/news.texi | 7 +++++++ log.go | 41 +++++++++++++++++++++++++++++++++-------- run.go | 17 +++++++++++------ usage.go | 2 +- 4 files changed, 52 insertions(+), 15 deletions(-) diff --git a/doc/news.texi b/doc/news.texi index 1ab170a..68442f0 100644 --- a/doc/news.texi +++ b/doc/news.texi @@ -2,6 +2,13 @@ @cindex news @unnumbered News +@anchor{Release 2_2_0} +@section Release 2.2.0 +@itemize +@item + Prefix target's output lines with the name of the target. +@end itemize + @anchor{Release 2_1_0} @section Release 2.1.0 @itemize diff --git a/log.go b/log.go index 6b91c2d..e671876 100644 --- a/log.go +++ b/log.go @@ -68,8 +68,9 @@ var ( flagLogPid *bool flagLogJS *bool - LogMutex sync.Mutex - KeyEraseLine string + LogMutex sync.Mutex + KeyEraseLine string + LastLoggedTgt string ) func init() { @@ -108,6 +109,34 @@ func erasedStatus(s, end string) string { 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 { @@ -116,9 +145,7 @@ func tracef(level, format string, args ...interface{}) { 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 { diff --git a/run.go b/run.go index 816f962..3fd4168 100644 --- a/run.go +++ b/run.go @@ -628,9 +628,11 @@ func runScript(tgt *Tgt, errs chan error, forced, traced bool) error { line = scanner.Text() if strings.HasPrefix(line, childStderrPrefix) { line = line[len(childStderrPrefix):] - LogMutex.Lock() - os.Stderr.WriteString(StderrPrefix + line + "\n") - LogMutex.Unlock() + if StderrPrefix == "" { + withPrependedTgt(line) + } else { + stderrWrite(StderrPrefix + line + "\n") + } continue } if fdStderr != nil { @@ -640,10 +642,13 @@ func runScript(tgt *Tgt, errs chan error, forced, traced bool) error { if StderrSilent { continue } - if MyPID == 0 { - tracef(CNone, "%s", line) + if MyPID != 0 { + line = pid + " " + line + } + if StderrPrefix == "" { + withPrependedTgt("[" + tgt.rel + "]" + line) } else { - tracef(CNone, "%s %s", pid, line) + stderrWrite(StderrPrefix + "[" + tgt.rel + "]" + line + "\n") } } close(stderrTerm) diff --git a/usage.go b/usage.go index ca592b0..2803628 100644 --- a/usage.go +++ b/usage.go @@ -24,7 +24,7 @@ import ( ) const ( - Version = "2.1.0" + Version = "2.2.0" Warranty = `Copyright (C) 2020-2023 Sergey Matveev This program is free software: you can redistribute it and/or modify -- 2.44.0