github.phpd.cn/thought-machine/please@v12.2.0+incompatible/src/output/trace.go (about) 1 // For writing out JSON trace files which Chrome can interpret nicely for us. 2 // See https://docs.google.com/document/d/1CvAClvFfyA5R-PhYUmn5OOQtYMH4h6I0nSsKchNAySU 3 4 package output 5 6 import "encoding/json" 7 import "fmt" 8 import "os" 9 10 import "core" 11 12 var traces = make([]traceEntry, 0, 1000) 13 14 func addTrace(result *core.BuildResult, previous core.BuildLabel, active bool) { 15 // It's a bit fiddly to keep all the phases in line here. 16 if result.Label != previous { 17 traces = append(traces, translateEvent(result, "B")) 18 } else if !active { 19 traces = append(traces, translateEvent(result, "E")) 20 } else { 21 traces = append(traces, translateEvent(result, "E")) 22 traces = append(traces, translateEvent(result, "B")) 23 } 24 } 25 26 func writeTrace(traceFile string) { 27 file, err := os.Create(traceFile) 28 if err != nil { 29 log.Errorf("Couldn't create trace file: %s", err) 30 return 31 } 32 defer file.Close() 33 file.Write(formatTrace()) 34 } 35 36 func formatTrace() []byte { 37 var out traceObjectFormat 38 out.OtherData.Version = "Please v" + core.PleaseVersion.String() 39 out.TraceEvents = traces 40 data, err := json.Marshal(out) 41 if err != nil { 42 log.Errorf("Error serialising JSON trace data: %s", err) 43 } 44 return data 45 } 46 47 func translateEvent(result *core.BuildResult, phase string) traceEntry { 48 entry := traceEntry{ 49 Name: result.Label.String(), 50 Cat: result.Status.Category(), 51 Ph: phase, 52 Pid: 0, // This isn't really important, there's only one process. 53 Ts: result.Time.UnixNano() / 1000, 54 } 55 entry.Tid = fmt.Sprintf("Builder %d", result.ThreadID) 56 entry.Args.Description = result.Description 57 if result.Err != nil { 58 entry.Args.Err = fmt.Sprintf("%s", result.Err) 59 } 60 return entry 61 } 62 63 type traceObjectFormat struct { 64 TraceEvents []traceEntry `json:"traceEvents"` 65 OtherData struct { 66 Version string `json:"version"` 67 } `json:"otherData"` 68 // Ignoring other properties for now. 69 } 70 71 type traceEntry struct { 72 Name string `json:"name"` 73 Cat string `json:"cat"` 74 Ph string `json:"ph"` 75 Pid int32 `json:"pid"` 76 Tid string `json:"tid"` 77 Ts int64 `json:"ts"` 78 Args struct { 79 Description string `json:"description"` 80 Err string `json:"err"` 81 } `json:"args"` 82 }