2 goredo -- djb's redo implementation on pure Go
3 Copyright (C) 2020-2023 Sergey Matveev <stargrave@stargrave.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, version 3 of the License.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
34 "go.cypherpunks.ru/recfile"
35 "go.cypherpunks.ru/tai64n/v2"
38 const HumanTimeFmt = "2006-01-02 15:04:05.000000000 Z07:00"
40 type BuildLogJob struct {
44 rec map[string][]string
47 type ByStarted []*BuildLogJob
49 func (a ByStarted) Len() int { return len(a) }
51 func (a ByStarted) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
53 func (a ByStarted) Less(i, j int) bool {
54 // actually that code performs reverse order
55 if a[i].exitCode > a[j].exitCode {
56 // bad return code has higher priority
59 return a[i].started.After(a[j].started)
63 flagBuildLogRecursive *bool
64 flagBuildLogCommands *bool
65 buildLogSeen map[string]struct{}
69 if CmdName() != CmdNameRedoLog {
72 flagBuildLogRecursive = flag.Bool("r", false, "Show logs recursively")
73 flagBuildLogCommands = flag.Bool("c", false, "Show how target was invoked")
74 buildLogSeen = make(map[string]struct{})
77 func parseBuildLogRec(tgt *Tgt) (map[string][]string, error) {
78 fd, err := os.Open(path.Join(tgt.h, RedoDir, tgt.t+LogRecSuffix))
80 return nil, ErrLine(err)
82 r := recfile.NewReader(bufio.NewReader(fd))
83 rec, err := r.NextMapWithSlice()
85 return rec, ErrLine(err)
88 func depthPrefix(depth int) string {
92 return " " + colourize(CDebug, strings.Repeat("> ", depth))
95 func showBuildLogSub(sub *BuildLogJob, depth int) error {
96 if _, ok := buildLogSeen[sub.tgt.a]; ok {
99 buildLogSeen[sub.tgt.a] = struct{}{}
100 dp := depthPrefix(depth)
103 sub.rec["Started"][0], dp,
104 colourize(CRedo, "redo "+sub.tgt.String()),
106 if err := showBuildLog(sub.tgt, sub.rec, depth+1); err != nil {
109 durationSec, durationNsec, err := durationToInts(sub.rec["Duration"][0])
113 if sub.exitCode > 0 {
115 "%s%s%s (code: %d) (%d.%ds)\n\n",
116 sub.rec["Finished"][0], dp,
117 colourize(CErr, "err "+sub.tgt.String()),
118 sub.exitCode, durationSec, durationNsec,
122 "%s%s%s (%d.%ds)\n\n",
123 sub.rec["Finished"][0], dp,
124 colourize(CRedo, "done "+sub.tgt.String()),
125 durationSec, durationNsec,
131 func durationToInts(d string) (int64, int64, error) {
132 duration, err := strconv.ParseInt(d, 10, 64)
136 return duration / 1e9, (duration % 1e9) / 1000, nil
139 func showBuildLogCmd(m map[string][]string, depth int) error {
140 started, err := tai64n.Decode(m["Started"][0])
144 dp := depthPrefix(depth)
147 m["Started"][0], dp, m["Cwd"][0], strings.Join(m["Cmd"], " "),
149 if len(m["ExitCode"]) > 0 {
150 fmt.Printf("%s%sExit code: %s\n", m["Started"][0], dp, m["ExitCode"][0])
152 finished, err := tai64n.Decode(m["Finished"][0])
156 durationSec, durationNsec, err := durationToInts(m["Duration"][0])
161 "%s%sStarted:\t%s\n%s%sFinished:\t%s\n%s%sDuration:\t%d.%ds\n\n",
162 m["Started"][0], dp, started.Format(HumanTimeFmt),
163 m["Started"][0], dp, finished.Format(HumanTimeFmt),
164 m["Started"][0], dp, durationSec, durationNsec,
169 func showBuildLog(tgt *Tgt, buildLogRec map[string][]string, depth int) error {
171 if *flagBuildLogCommands || *flagBuildLogRecursive {
172 buildLogRec, err = parseBuildLogRec(tgt)
177 if *flagBuildLogCommands {
178 if err = showBuildLogCmd(buildLogRec, depth); err != nil {
182 fd, err := os.Open(path.Join(tgt.h, RedoDir, tgt.t+LogSuffix))
186 if !*flagBuildLogRecursive {
187 w := bufio.NewWriter(os.Stdout)
188 _, err = io.Copy(w, bufio.NewReader(fd))
194 return ErrLine(w.Flush())
197 subs := make([]*BuildLogJob, 0, len(buildLogRec["Ifchange"]))
198 for _, depPath := range buildLogRec["Ifchange"] {
199 dep := NewTgt(path.Join(tgt.h, depPath))
203 rec, err := parseBuildLogRec(dep)
205 if errors.Is(err, fs.ErrNotExist) {
210 if rec["Build"][0] != buildLogRec["Build"][0] {
213 started, err := tai64n.Decode(rec["Started"][0])
218 if len(rec["ExitCode"]) > 0 {
219 exitCode, err = strconv.Atoi(rec["ExitCode"][0])
224 subs = append(subs, &BuildLogJob{
231 sort.Sort(ByStarted(subs))
232 scanner := bufio.NewScanner(fd)
238 sub = subs[len(subs)-1]
240 dp := depthPrefix(depth)
243 if err = scanner.Err(); err != nil {
248 text = scanner.Text()
250 return ErrLine(errors.New("unexpected non-TAI64Ned string"))
252 sep = strings.IndexByte(text, byte(' '))
256 t, err = tai64n.Decode(text[1:sep])
260 for sub != nil && t.After(sub.started) {
261 if err = showBuildLogSub(sub, depth); err != nil {
264 subs = subs[:len(subs)-1]
266 sub = subs[len(subs)-1]
274 fmt.Printf("%s%s%s\n", text[:sep], dp, text[sep+1:])
277 for i := len(subs); i > 0; i-- {
279 if err = showBuildLogSub(sub, depth); err != nil {