| 1 | // Copyright 2020 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package export |
| 6 | |
| 7 | import ( |
| 8 | "io" |
| 9 | |
| 10 | "golang.org/x/tools/internal/event/core" |
| 11 | "golang.org/x/tools/internal/event/keys" |
| 12 | "golang.org/x/tools/internal/event/label" |
| 13 | ) |
| 14 | |
| 15 | type Printer struct { |
| 16 | buffer [128]byte |
| 17 | } |
| 18 | |
| 19 | // WriteEvent writes the formatted event to w. It is not concurrency-safe. |
| 20 | func (p *Printer) WriteEvent(w io.Writer, ev core.Event, lm label.Map) { |
| 21 | // TODO(adonovan): any function that accepts an arbitrary |
| 22 | // io.Writer should really deal with errors. Use bytes.Buffer? |
| 23 | buf := p.buffer[:0] |
| 24 | if !ev.At().IsZero() { |
| 25 | w.Write(ev.At().AppendFormat(buf, "2006/01/02 15:04:05 ")) // ignore error |
| 26 | } |
| 27 | msg := keys.Msg.Get(lm) |
| 28 | io.WriteString(w, msg) |
| 29 | if err := keys.Err.Get(lm); err != nil { |
| 30 | if msg != "" { |
| 31 | io.WriteString(w, ": ") |
| 32 | } |
| 33 | io.WriteString(w, err.Error()) |
| 34 | } |
| 35 | for l := range ev.Labels() { |
| 36 | if l.Key() == keys.Msg || l.Key() == keys.Err { |
| 37 | continue |
| 38 | } |
| 39 | io.WriteString(w, "\n\t") |
| 40 | io.WriteString(w, l.Key().Name()) |
| 41 | io.WriteString(w, "=") |
| 42 | w.Write(l.Key().Append(buf, l)) // ignore error |
| 43 | } |
| 44 | io.WriteString(w, "\n") |
| 45 | } |