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) { |
Serge Bazanski | 05f813b | 2023-03-16 17:58:39 +0100 | [diff] [blame] | 25 | s := strings.TrimRight(l.String(), " \t\n\r") |
| 26 | s = strings.TrimLeft(s, "\n\r\t") |
Serge Bazanski | 075465c | 2021-11-16 15:38:49 +0100 | [diff] [blame] | 27 | // TODO(q3k): don't just skip lines containing escape sequences, strip the |
| 28 | // sequences out. Or stop parsing qemu logs and instead dial log endpoint in |
| 29 | // spawned nodes. |
| 30 | if strings.Contains(s, "\u001b") { |
| 31 | return |
| 32 | } |
| 33 | fmt.Printf("%02d| %s\n", num, s) |
| 34 | }), |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | func (p prefixedStdio) Read(_ []byte) (int, error) { |
| 39 | return 0, io.EOF |
| 40 | } |