blob: 2e4bbd27373a204e2080247d4842d8b66fd3bbcc [file] [log] [blame]
Tim Windelschmidt6d33a432025-02-04 14:34:25 +01001// Copyright The Monogon Project Authors.
2// SPDX-License-Identifier: Apache-2.0
3
Tim Windelschmidt9f21f532024-05-07 15:14:20 +02004package launch
Serge Bazanski075465c2021-11-16 15:38:49 +01005
6import (
7 "fmt"
8 "io"
9 "strings"
10
Tim Windelschmidt9f21f532024-05-07 15:14:20 +020011 "source.monogon.dev/osbase/logbuffer"
Serge Bazanski075465c2021-11-16 15:38:49 +010012)
13
14// prefixedStdio is a io.ReadWriter which splits written bytes into lines,
15// prefixes them with some known prefix, and spits them to os.Stdout.
16//
17// io.Reader is implemented for compatibility with code which expects an
18// io.ReadWriter, but always returns EOF.
19type prefixedStdio struct {
20 *logbuffer.LineBuffer
21}
22
23// newPrefixedStdio returns a prefixedStdio that prefixes all lines with <num>|,
24// used to distinguish different VMs used within the launch codebase.
25func newPrefixedStdio(num int) prefixedStdio {
26 return prefixedStdio{
27 logbuffer.NewLineBuffer(1024, func(l *logbuffer.Line) {
Serge Bazanski05f813b2023-03-16 17:58:39 +010028 s := strings.TrimRight(l.String(), " \t\n\r")
29 s = strings.TrimLeft(s, "\n\r\t")
Serge Bazanski075465c2021-11-16 15:38:49 +010030 // TODO(q3k): don't just skip lines containing escape sequences, strip the
31 // sequences out. Or stop parsing qemu logs and instead dial log endpoint in
32 // spawned nodes.
33 if strings.Contains(s, "\u001b") {
34 return
35 }
36 fmt.Printf("%02d| %s\n", num, s)
37 }),
38 }
39}
40
41func (p prefixedStdio) Read(_ []byte) (int, error) {
42 return 0, io.EOF
43}