| Tim Windelschmidt | 6d33a43 | 2025-02-04 14:34:25 +0100 | [diff] [blame^] | 1 | // Copyright The Monogon Project Authors. |
| Serge Bazanski | 248b2ec | 2020-10-26 15:55:51 +0100 | [diff] [blame] | 2 | // SPDX-License-Identifier: Apache-2.0 |
| Serge Bazanski | 248b2ec | 2020-10-26 15:55:51 +0100 | [diff] [blame] | 3 | |
| 4 | package logbuffer |
| 5 | |
| 6 | import ( |
| 7 | "bytes" |
| 8 | "fmt" |
| 9 | "strings" |
| 10 | "sync" |
| Serge Bazanski | b027218 | 2020-11-02 18:39:44 +0100 | [diff] [blame] | 11 | |
| Tim Windelschmidt | 9f21f53 | 2024-05-07 15:14:20 +0200 | [diff] [blame] | 12 | lpb "source.monogon.dev/osbase/logtree/proto" |
| Serge Bazanski | 248b2ec | 2020-10-26 15:55:51 +0100 | [diff] [blame] | 13 | ) |
| 14 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 15 | // Line is a line stored in the log buffer - a string, that has been perhaps |
| 16 | // truncated (due to exceeded limits). |
| Serge Bazanski | 248b2ec | 2020-10-26 15:55:51 +0100 | [diff] [blame] | 17 | type Line struct { |
| 18 | Data string |
| 19 | OriginalLength int |
| 20 | } |
| 21 | |
| 22 | // Truncated returns whether this line has been truncated to fit limits. |
| 23 | func (l *Line) Truncated() bool { |
| 24 | return l.OriginalLength > len(l.Data) |
| 25 | } |
| 26 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 27 | // String returns the line with an ellipsis at the end (...) if the line has been |
| 28 | // truncated, or the original line otherwise. |
| Serge Bazanski | 248b2ec | 2020-10-26 15:55:51 +0100 | [diff] [blame] | 29 | func (l *Line) String() string { |
| 30 | if l.Truncated() { |
| 31 | return l.Data + "..." |
| 32 | } |
| 33 | return l.Data |
| 34 | } |
| 35 | |
| Serge Bazanski | b027218 | 2020-11-02 18:39:44 +0100 | [diff] [blame] | 36 | // ProtoLog returns a Logging-specific protobuf structure. |
| Tim Windelschmidt | 8814f52 | 2024-05-08 00:41:13 +0200 | [diff] [blame] | 37 | func (l *Line) ProtoLog() *lpb.LogEntry_Raw { |
| 38 | return &lpb.LogEntry_Raw{ |
| Serge Bazanski | b027218 | 2020-11-02 18:39:44 +0100 | [diff] [blame] | 39 | Data: l.Data, |
| 40 | OriginalLength: int64(l.OriginalLength), |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | // LineFromLogProto converts a Logging-specific protobuf message back into a Line. |
| Tim Windelschmidt | 8814f52 | 2024-05-08 00:41:13 +0200 | [diff] [blame] | 45 | func LineFromLogProto(raw *lpb.LogEntry_Raw) (*Line, error) { |
| Serge Bazanski | b027218 | 2020-11-02 18:39:44 +0100 | [diff] [blame] | 46 | if raw.OriginalLength < int64(len(raw.Data)) { |
| 47 | return nil, fmt.Errorf("original_length smaller than length of data") |
| 48 | } |
| 49 | originalLength := int(raw.OriginalLength) |
| 50 | if int64(originalLength) < raw.OriginalLength { |
| 51 | return nil, fmt.Errorf("original_length larger than native int size") |
| 52 | } |
| 53 | return &Line{ |
| 54 | Data: raw.Data, |
| 55 | OriginalLength: originalLength, |
| 56 | }, nil |
| 57 | } |
| 58 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 59 | // LineBuffer is a io.WriteCloser that will call a given callback every time a line |
| 60 | // is completed. |
| Serge Bazanski | 248b2ec | 2020-10-26 15:55:51 +0100 | [diff] [blame] | 61 | type LineBuffer struct { |
| 62 | maxLineLength int |
| 63 | cb LineBufferCallback |
| 64 | |
| 65 | mu sync.Mutex |
| 66 | cur strings.Builder |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 67 | // length is the length of the line currently being written - this will continue to |
| 68 | // increase, even if the string exceeds maxLineLength. |
| Serge Bazanski | 248b2ec | 2020-10-26 15:55:51 +0100 | [diff] [blame] | 69 | length int |
| 70 | closed bool |
| 71 | } |
| 72 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 73 | // LineBufferCallback is a callback that will get called any time the line is |
| 74 | // completed. The function must not cause another write to the LineBuffer, or the |
| 75 | // program will deadlock. |
| Serge Bazanski | 248b2ec | 2020-10-26 15:55:51 +0100 | [diff] [blame] | 76 | type LineBufferCallback func(*Line) |
| 77 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 78 | // NewLineBuffer creates a new LineBuffer with a given line length limit and |
| 79 | // callback. |
| Serge Bazanski | 248b2ec | 2020-10-26 15:55:51 +0100 | [diff] [blame] | 80 | func NewLineBuffer(maxLineLength int, cb LineBufferCallback) *LineBuffer { |
| 81 | return &LineBuffer{ |
| 82 | maxLineLength: maxLineLength, |
| 83 | cb: cb, |
| 84 | } |
| 85 | } |
| 86 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 87 | // writeLimited writes to the internal buffer, making sure that its size does not |
| 88 | // exceed the maxLineLength. |
| Serge Bazanski | 248b2ec | 2020-10-26 15:55:51 +0100 | [diff] [blame] | 89 | func (l *LineBuffer) writeLimited(data []byte) { |
| 90 | l.length += len(data) |
| 91 | if l.cur.Len()+len(data) > l.maxLineLength { |
| 92 | data = data[:l.maxLineLength-l.cur.Len()] |
| 93 | } |
| 94 | l.cur.Write(data) |
| 95 | } |
| 96 | |
| 97 | // comitLine calls the callback and resets the builder. |
| 98 | func (l *LineBuffer) commitLine() { |
| 99 | l.cb(&Line{ |
| 100 | Data: l.cur.String(), |
| 101 | OriginalLength: l.length, |
| 102 | }) |
| 103 | l.cur.Reset() |
| 104 | l.length = 0 |
| 105 | } |
| 106 | |
| 107 | func (l *LineBuffer) Write(data []byte) (int, error) { |
| 108 | var pos = 0 |
| 109 | |
| 110 | l.mu.Lock() |
| 111 | defer l.mu.Unlock() |
| 112 | |
| 113 | if l.closed { |
| 114 | return 0, fmt.Errorf("closed") |
| 115 | } |
| 116 | |
| 117 | for { |
| 118 | nextNewline := bytes.IndexRune(data[pos:], '\n') |
| 119 | |
| 120 | // No newline in the data, write everything to the current line |
| 121 | if nextNewline == -1 { |
| 122 | l.writeLimited(data[pos:]) |
| 123 | break |
| 124 | } |
| 125 | |
| 126 | // Write this line and update position |
| 127 | l.writeLimited(data[pos : pos+nextNewline]) |
| 128 | l.commitLine() |
| 129 | pos += nextNewline + 1 |
| 130 | |
| 131 | // Data ends with a newline, stop now without writing an empty line |
| 132 | if nextNewline == len(data)-1 { |
| 133 | break |
| 134 | } |
| 135 | } |
| 136 | return len(data), nil |
| 137 | } |
| 138 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 139 | // Close will emit any leftover data in the buffer to the callback. Subsequent |
| 140 | // calls to Write will fail. Subsequent calls to Close will also fail. |
| Serge Bazanski | 248b2ec | 2020-10-26 15:55:51 +0100 | [diff] [blame] | 141 | func (l *LineBuffer) Close() error { |
| 142 | if l.closed { |
| 143 | return fmt.Errorf("already closed") |
| 144 | } |
| 145 | l.mu.Lock() |
| 146 | defer l.mu.Unlock() |
| 147 | l.closed = true |
| 148 | if l.length > 0 { |
| 149 | l.commitLine() |
| 150 | } |
| 151 | return nil |
| 152 | } |
| Serge Bazanski | 6c8ee0b | 2023-04-05 12:29:57 +0200 | [diff] [blame] | 153 | |
| 154 | func (l *LineBuffer) Sync() error { |
| 155 | return nil |
| 156 | } |