| Tim Windelschmidt | 6d33a43 | 2025-02-04 14:34:25 +0100 | [diff] [blame] | 1 | // Copyright The Monogon Project Authors. |
| 2 | // SPDX-License-Identifier: Apache-2.0 |
| 3 | |
| Tim Windelschmidt | 9f21f53 | 2024-05-07 15:14:20 +0200 | [diff] [blame] | 4 | package launch |
| Serge Bazanski | 075465c | 2021-11-16 15:38:49 +0100 | [diff] [blame] | 5 | |
| 6 | import ( |
| 7 | "fmt" |
| 8 | "io" |
| 9 | "strings" |
| 10 | |
| Tim Windelschmidt | 9f21f53 | 2024-05-07 15:14:20 +0200 | [diff] [blame] | 11 | "source.monogon.dev/osbase/logbuffer" |
| Serge Bazanski | 075465c | 2021-11-16 15:38:49 +0100 | [diff] [blame] | 12 | ) |
| 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. |
| 19 | type 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. |
| 25 | func newPrefixedStdio(num int) prefixedStdio { |
| 26 | return prefixedStdio{ |
| 27 | logbuffer.NewLineBuffer(1024, func(l *logbuffer.Line) { |
| Serge Bazanski | 05f813b | 2023-03-16 17:58:39 +0100 | [diff] [blame] | 28 | s := strings.TrimRight(l.String(), " \t\n\r") |
| 29 | s = strings.TrimLeft(s, "\n\r\t") |
| Serge Bazanski | 075465c | 2021-11-16 15:38:49 +0100 | [diff] [blame] | 30 | // 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 | |
| 41 | func (p prefixedStdio) Read(_ []byte) (int, error) { |
| 42 | return 0, io.EOF |
| 43 | } |