| Tim Windelschmidt | 6d33a43 | 2025-02-04 14:34:25 +0100 | [diff] [blame] | 1 | // Copyright The Monogon Project Authors. |
| Lorenz Brun | 25b82a8 | 2020-03-23 20:27:51 +0100 | [diff] [blame] | 2 | // SPDX-License-Identifier: Apache-2.0 |
| Lorenz Brun | 25b82a8 | 2020-03-23 20:27:51 +0100 | [diff] [blame] | 3 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 4 | // Package logbuffer implements a fixed-size in-memory ring buffer for |
| 5 | // line-separated logs. It implements io.Writer and splits the data into lines. |
| 6 | // The lines are kept in a ring where the oldest are overwritten once it's |
| 7 | // full. It allows retrieval of the last n lines. There is a built-in line |
| 8 | // length limit to bound the memory usage at maxLineLength * size. |
| Lorenz Brun | 25b82a8 | 2020-03-23 20:27:51 +0100 | [diff] [blame] | 9 | package logbuffer |
| 10 | |
| 11 | import ( |
| Lorenz Brun | 25b82a8 | 2020-03-23 20:27:51 +0100 | [diff] [blame] | 12 | "sync" |
| 13 | ) |
| 14 | |
| 15 | // LogBuffer implements a fixed-size in-memory ring buffer for line-separated logs |
| 16 | type LogBuffer struct { |
| Serge Bazanski | 248b2ec | 2020-10-26 15:55:51 +0100 | [diff] [blame] | 17 | mu sync.RWMutex |
| 18 | content []Line |
| 19 | length int |
| 20 | *LineBuffer |
| Lorenz Brun | 25b82a8 | 2020-03-23 20:27:51 +0100 | [diff] [blame] | 21 | } |
| 22 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 23 | // New creates a new LogBuffer with a given ringbuffer size and maximum line |
| 24 | // length. |
| Lorenz Brun | 25b82a8 | 2020-03-23 20:27:51 +0100 | [diff] [blame] | 25 | func New(size, maxLineLength int) *LogBuffer { |
| Serge Bazanski | 248b2ec | 2020-10-26 15:55:51 +0100 | [diff] [blame] | 26 | lb := &LogBuffer{ |
| 27 | content: make([]Line, size), |
| Lorenz Brun | 25b82a8 | 2020-03-23 20:27:51 +0100 | [diff] [blame] | 28 | } |
| Serge Bazanski | 248b2ec | 2020-10-26 15:55:51 +0100 | [diff] [blame] | 29 | lb.LineBuffer = NewLineBuffer(maxLineLength, lb.lineCallback) |
| 30 | return lb |
| Lorenz Brun | 25b82a8 | 2020-03-23 20:27:51 +0100 | [diff] [blame] | 31 | } |
| 32 | |
| Serge Bazanski | 248b2ec | 2020-10-26 15:55:51 +0100 | [diff] [blame] | 33 | func (b *LogBuffer) lineCallback(line *Line) { |
| Lorenz Brun | 25b82a8 | 2020-03-23 20:27:51 +0100 | [diff] [blame] | 34 | b.mu.Lock() |
| 35 | defer b.mu.Unlock() |
| 36 | |
| Serge Bazanski | 248b2ec | 2020-10-26 15:55:51 +0100 | [diff] [blame] | 37 | b.content[b.length%len(b.content)] = *line |
| 38 | b.length++ |
| Lorenz Brun | 25b82a8 | 2020-03-23 20:27:51 +0100 | [diff] [blame] | 39 | } |
| 40 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 41 | // capToContentLength caps the number of requested lines to what is actually |
| 42 | // available |
| Lorenz Brun | 25b82a8 | 2020-03-23 20:27:51 +0100 | [diff] [blame] | 43 | func (b *LogBuffer) capToContentLength(n int) int { |
| 44 | // If there aren't enough lines to read, reduce the request size |
| 45 | if n > b.length { |
| 46 | n = b.length |
| 47 | } |
| 48 | // If there isn't enough ringbuffer space, reduce the request size |
| 49 | if n > len(b.content) { |
| 50 | n = len(b.content) |
| 51 | } |
| 52 | return n |
| 53 | } |
| 54 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 55 | // ReadLines reads the last n lines from the buffer in chronological order. If |
| 56 | // n is bigger than the ring buffer or the number of available lines only the |
| 57 | // number of stored lines are returned. |
| Lorenz Brun | 25b82a8 | 2020-03-23 20:27:51 +0100 | [diff] [blame] | 58 | func (b *LogBuffer) ReadLines(n int) []Line { |
| 59 | b.mu.RLock() |
| 60 | defer b.mu.RUnlock() |
| 61 | |
| 62 | n = b.capToContentLength(n) |
| 63 | |
| 64 | // Copy references out to keep them around |
| 65 | outArray := make([]Line, n) |
| 66 | for i := 1; i <= n; i++ { |
| 67 | outArray[n-i] = b.content[(b.length-i)%len(b.content)] |
| 68 | } |
| 69 | return outArray |
| 70 | } |
| 71 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 72 | // ReadLinesTruncated works exactly the same as ReadLines, but adds an ellipsis |
| 73 | // at the end of every line that was truncated because it was over |
| 74 | // MaxLineLength |
| Lorenz Brun | 25b82a8 | 2020-03-23 20:27:51 +0100 | [diff] [blame] | 75 | func (b *LogBuffer) ReadLinesTruncated(n int, ellipsis string) []string { |
| Serge Bazanski | 248b2ec | 2020-10-26 15:55:51 +0100 | [diff] [blame] | 76 | b.mu.RLock() |
| 77 | defer b.mu.RUnlock() |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 78 | // This does not use ReadLines() to prevent excessive reference copying and |
| 79 | // associated GC pressure since it could process a lot of lines. |
| Lorenz Brun | 25b82a8 | 2020-03-23 20:27:51 +0100 | [diff] [blame] | 80 | |
| 81 | n = b.capToContentLength(n) |
| 82 | |
| 83 | outArray := make([]string, n) |
| 84 | for i := 1; i <= n; i++ { |
| 85 | line := b.content[(b.length-i)%len(b.content)] |
| Serge Bazanski | 248b2ec | 2020-10-26 15:55:51 +0100 | [diff] [blame] | 86 | outArray[n-i] = line.String() |
| Lorenz Brun | 25b82a8 | 2020-03-23 20:27:51 +0100 | [diff] [blame] | 87 | } |
| 88 | return outArray |
| 89 | } |