blob: 3ea3e18b7acdefb0f637d8406d291126e86db555 [file] [log] [blame]
Serge Bazanski075465c2021-11-16 15:38:49 +01001package cluster
2
3import (
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.
16type 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.
22func newPrefixedStdio(num int) prefixedStdio {
23 return prefixedStdio{
24 logbuffer.NewLineBuffer(1024, func(l *logbuffer.Line) {
Serge Bazanski05f813b2023-03-16 17:58:39 +010025 s := strings.TrimRight(l.String(), " \t\n\r")
26 s = strings.TrimLeft(s, "\n\r\t")
Serge Bazanski075465c2021-11-16 15:38:49 +010027 // 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
38func (p prefixedStdio) Read(_ []byte) (int, error) {
39 return 0, io.EOF
40}