| 1 | package plugin |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "sync" |
| 6 | |
| 7 | fileencoding "reasonix/internal/fileutil/encoding" |
| 8 | ) |
| 9 | |
| 10 | // tailBuffer keeps the last limit bytes written, for truncating a process's |
| 11 | // captured output to what teardown diagnostics actually need. |
| 12 | type tailBuffer struct { |
| 13 | mu sync.Mutex |
| 14 | limit int |
| 15 | buf []byte |
| 16 | cut bool // bytes before buf were dropped to hold the limit |
| 17 | } |
| 18 | |
| 19 | func (b *tailBuffer) Write(p []byte) (int, error) { |
| 20 | b.mu.Lock() |
| 21 | defer b.mu.Unlock() |
| 22 | b.buf = append(b.buf, p...) |
| 23 | if b.limit > 0 && len(b.buf) > b.limit { |
| 24 | b.buf = append([]byte(nil), b.buf[len(b.buf)-b.limit:]...) |
| 25 | b.cut = true |
| 26 | } |
| 27 | return len(p), nil |
| 28 | } |
| 29 | |
| 30 | // String reads the tail in the server's own encoding: cmd.exe on a Chinese |
| 31 | // Windows reports a missing command in the console code page, not UTF-8. |
| 32 | func (b *tailBuffer) String() string { |
| 33 | b.mu.Lock() |
| 34 | defer b.mu.Unlock() |
| 35 | return strings.TrimSpace(string(fileencoding.DecodeOutput(b.buf, fileencoding.Cut{Head: b.cut}))) |
| 36 | } |
| 37 |