writeLine writes a single line from the input, returning the remaining, unconsumed bytes.
(line []byte)
| 89 | // writeLine writes a single line from the input, returning the remaining, |
| 90 | // unconsumed bytes. |
| 91 | func (w *Writer) writeLine(line []byte) (remaining []byte) { |
| 92 | idx := bytes.IndexByte(line, '\n') |
| 93 | if idx < 0 { |
| 94 | // If there are no newlines, buffer the entire string. |
| 95 | w.buff.Write(line) |
| 96 | return nil |
| 97 | } |
| 98 | |
| 99 | // Split on the newline, buffer and flush the left. |
| 100 | line, remaining = line[:idx], line[idx+1:] |
| 101 | |
| 102 | // Fast path: if we don't have a partial message from a previous write |
| 103 | // in the buffer, skip the buffer and log directly. |
| 104 | if w.buff.Len() == 0 { |
| 105 | w.log(line) |
| 106 | return |
| 107 | } |
| 108 | |
| 109 | w.buff.Write(line) |
| 110 | |
| 111 | // Log empty messages in the middle of the stream so that we don't lose |
| 112 | // information when the user writes "foo\n\nbar". |
| 113 | w.flush(true /* allowEmpty */) |
| 114 | |
| 115 | return remaining |
| 116 | } |
| 117 | |
| 118 | // Close closes the writer, flushing any buffered data in the process. |
| 119 | // |