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 h, t := path.Split(tgt.a)
79 fd, err := os.Open(path.Join(h, RedoDir, t+LogRecSuffix))
81 return nil, ErrLine(err)
83 r := recfile.NewReader(bufio.NewReader(fd))
84 rec, err := r.NextMapWithSlice()
86 return rec, ErrLine(err)
89 func depthPrefix(depth int) string {
93 return " " + colourize(CDebug, strings.Repeat("> ", depth))
96 func showBuildLogSub(sub *BuildLogJob, depth int) error {
97 if _, ok := buildLogSeen[sub.tgt.rel]; ok {
100 buildLogSeen[sub.tgt.rel] = struct{}{}
101 dp := depthPrefix(depth)
104 sub.rec["Started"][0], dp,
105 colourize(CRedo, "redo "+sub.tgt.rel),
107 if err := showBuildLog(sub.tgt, sub.rec, depth+1); err != nil {
110 durationSec, durationNsec, err := durationToInts(sub.rec["Duration"][0])
114 if sub.exitCode > 0 {
116 "%s%s%s (code: %d) (%d.%ds)\n\n",
117 sub.rec["Finished"][0], dp,
118 colourize(CErr, "err "+sub.tgt.rel),
119 sub.exitCode, durationSec, durationNsec,
123 "%s%s%s (%d.%ds)\n\n",
124 sub.rec["Finished"][0], dp,
125 colourize(CRedo, "done "+sub.tgt.rel),
126 durationSec, durationNsec,
132 func durationToInts(d string) (int64, int64, error) {
133 duration, err := strconv.ParseInt(d, 10, 64)
137 return duration / 1e9, (duration % 1e9) / 1000, nil
140 func showBuildLogCmd(m map[string][]string, depth int) error {
141 started, err := tai64n.Decode(m["Started"][0])
145 dp := depthPrefix(depth)
148 m["Started"][0], dp, m["Cwd"][0], strings.Join(m["Cmd"], " "),
150 if len(m["ExitCode"]) > 0 {
151 fmt.Printf("%s%sExit code: %s\n", m["Started"][0], dp, m["ExitCode"][0])
153 finished, err := tai64n.Decode(m["Finished"][0])
157 durationSec, durationNsec, err := durationToInts(m["Duration"][0])
162 "%s%sStarted:\t%s\n%s%sFinished:\t%s\n%s%sDuration:\t%d.%ds\n\n",
163 m["Started"][0], dp, started.Format(HumanTimeFmt),
164 m["Started"][0], dp, finished.Format(HumanTimeFmt),
165 m["Started"][0], dp, durationSec, durationNsec,
170 func showBuildLog(tgt *Tgt, buildLogRec map[string][]string, depth int) error {
172 if *flagBuildLogCommands || *flagBuildLogRecursive {
173 buildLogRec, err = parseBuildLogRec(tgt)
178 if *flagBuildLogCommands {
179 if err = showBuildLogCmd(buildLogRec, depth); err != nil {
183 tgtH, tgtT := path.Split(tgt.a)
184 fd, err := os.Open(path.Join(tgtH, RedoDir, tgtT+LogSuffix))
188 if !*flagBuildLogRecursive {
189 w := bufio.NewWriter(os.Stdout)
190 _, err = io.Copy(w, bufio.NewReader(fd))
196 return ErrLine(w.Flush())
199 subs := make([]*BuildLogJob, 0, len(buildLogRec["Ifchange"]))
200 for _, depPath := range buildLogRec["Ifchange"] {
201 dep := NewTgt(path.Join(tgtH, depPath))
202 if dep.rel == tgt.rel {
205 rec, err := parseBuildLogRec(dep)
207 if errors.Is(err, fs.ErrNotExist) {
212 if rec["Build"][0] != buildLogRec["Build"][0] {
215 started, err := tai64n.Decode(rec["Started"][0])
220 if len(rec["ExitCode"]) > 0 {
221 exitCode, err = strconv.Atoi(rec["ExitCode"][0])
226 subs = append(subs, &BuildLogJob{
233 sort.Sort(ByStarted(subs))
234 scanner := bufio.NewScanner(fd)
240 sub = subs[len(subs)-1]
242 dp := depthPrefix(depth)
245 if err = scanner.Err(); err != nil {
250 text = scanner.Text()
252 return ErrLine(errors.New("unexpected non-TAI64Ned string"))
254 sep = strings.IndexByte(text, byte(' '))
258 t, err = tai64n.Decode(text[1:sep])
262 for sub != nil && t.After(sub.started) {
263 if err = showBuildLogSub(sub, depth); err != nil {
266 subs = subs[:len(subs)-1]
268 sub = subs[len(subs)-1]
276 fmt.Printf("%s%s%s\n", text[:sep], dp, text[sep+1:])
279 for i := len(subs); i > 0; i-- {
281 if err = showBuildLogSub(sub, depth); err != nil {