Serge Bazanski | 075465c | 2021-11-16 15:38:49 +0100 | [diff] [blame] | 1 | package cluster |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "io" |
| 6 | "strings" |
| 7 | |
| 8 | "source.monogon.dev/metropolis/pkg/logbuffer" |
| 9 | ) |
| 10 | |
| 11 | // prefixedStdio is a io.ReadWriter which splits written bytes into lines, |
| 12 | // prefixes them with some known prefix, and spits them to os.Stdout. |
| 13 | // |
| 14 | // io.Reader is implemented for compatibility with code which expects an |
| 15 | // io.ReadWriter, but always returns EOF. |
| 16 | type prefixedStdio struct { |
| 17 | *logbuffer.LineBuffer |
| 18 | } |
| 19 | |
| 20 | // newPrefixedStdio returns a prefixedStdio that prefixes all lines with <num>|, |
| 21 | // used to distinguish different VMs used within the launch codebase. |
| 22 | func newPrefixedStdio(num int) prefixedStdio { |
| 23 | return prefixedStdio{ |
| 24 | logbuffer.NewLineBuffer(1024, func(l *logbuffer.Line) { |
| 25 | s := strings.TrimSpace(l.String()) |
| 26 | // TODO(q3k): don't just skip lines containing escape sequences, strip the |
| 27 | // sequences out. Or stop parsing qemu logs and instead dial log endpoint in |
| 28 | // spawned nodes. |
| 29 | if strings.Contains(s, "\u001b") { |
| 30 | return |
| 31 | } |
| 32 | fmt.Printf("%02d| %s\n", num, s) |
| 33 | }), |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | func (p prefixedStdio) Read(_ []byte) (int, error) { |
| 38 | return 0, io.EOF |
| 39 | } |