diff options
| author | Paul Buetow <paul@buetow.org> | 2021-11-02 08:11:40 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2021-11-02 08:11:40 +0200 |
| commit | 1ec88deea93047a9d1a366e032b2a54aa3cd362b (patch) | |
| tree | 465f30673aa097a2b369e7c3d2032b041ff4f8e6 /internal/io | |
| parent | 2e9ce81c47d45dd1f2c607df6e19bdfdc3bb3cc8 (diff) | |
Bugfix: Dealing correctly with files without newline characters, also add more tests
Diffstat (limited to 'internal/io')
| -rw-r--r-- | internal/io/dlog/dlog.go | 4 | ||||
| -rw-r--r-- | internal/io/dlog/loggers/file.go | 15 | ||||
| -rw-r--r-- | internal/io/dlog/loggers/fout.go | 10 | ||||
| -rw-r--r-- | internal/io/dlog/loggers/logger.go | 2 | ||||
| -rw-r--r-- | internal/io/dlog/loggers/none.go | 18 | ||||
| -rw-r--r-- | internal/io/dlog/loggers/stdout.go | 20 | ||||
| -rw-r--r-- | internal/io/fs/readfile.go | 13 |
7 files changed, 61 insertions, 21 deletions
diff --git a/internal/io/dlog/dlog.go b/internal/io/dlog/dlog.go index ff2cef4..5713c1a 100644 --- a/internal/io/dlog/dlog.go +++ b/internal/io/dlog/dlog.go @@ -210,10 +210,10 @@ func (d *DLog) Devel(args ...interface{}) string { // Raw message logging. func (d *DLog) Raw(message string) string { if !config.Client.TermColorsEnable || !d.logger.SupportsColors() { - d.logger.Log(time.Now(), message) + d.logger.Raw(time.Now(), message) return message } - d.logger.LogWithColors(time.Now(), message, brush.Colorfy(message)) + d.logger.RawWithColors(time.Now(), message, brush.Colorfy(message)) return message } diff --git a/internal/io/dlog/loggers/file.go b/internal/io/dlog/loggers/file.go index 94824fe..9dce251 100644 --- a/internal/io/dlog/loggers/file.go +++ b/internal/io/dlog/loggers/file.go @@ -17,6 +17,7 @@ type fileWriter struct{} type fileMessageBuf struct { now time.Time message string + nl bool } type file struct { @@ -86,10 +87,18 @@ func (f *file) Start(ctx context.Context, wg *sync.WaitGroup) { } func (f *file) Log(now time.Time, message string) { - f.bufferCh <- &fileMessageBuf{now, message} + f.bufferCh <- &fileMessageBuf{now, message, true} } func (f *file) LogWithColors(now time.Time, message, coloredMessage string) { + f.RawWithColors(now, message, coloredMessage) +} + +func (f *file) Raw(now time.Time, message string) { + f.bufferCh <- &fileMessageBuf{now, message, false} +} + +func (f *file) RawWithColors(now time.Time, message, coloredMessage string) { panic("Colors not supported in file logger") } @@ -116,7 +125,9 @@ func (f *file) write(m *fileMessageBuf) { } writer.WriteString(m.message) - writer.WriteByte('\n') + if m.nl { + writer.WriteByte('\n') + } } func (f *file) getWriter(name string) *bufio.Writer { diff --git a/internal/io/dlog/loggers/fout.go b/internal/io/dlog/loggers/fout.go index 60c318d..6888d40 100644 --- a/internal/io/dlog/loggers/fout.go +++ b/internal/io/dlog/loggers/fout.go @@ -38,6 +38,16 @@ func (f *fout) LogWithColors(now time.Time, message, coloredMessage string) { f.file.Log(now, message) } +func (f *fout) Raw(now time.Time, message string) { + f.stdout.Raw(now, message) + f.file.Raw(now, message) +} + +func (f *fout) RawWithColors(now time.Time, message, coloredMessage string) { + f.stdout.RawWithColors(now, "", coloredMessage) + f.file.Raw(now, message) +} + func (f *fout) Flush() { f.stdout.Flush(); f.file.Flush() } func (f *fout) Pause() { f.stdout.Pause(); f.file.Pause() } func (f *fout) Resume() { f.stdout.Resume(); f.file.Resume() } diff --git a/internal/io/dlog/loggers/logger.go b/internal/io/dlog/loggers/logger.go index d4e85de..195108b 100644 --- a/internal/io/dlog/loggers/logger.go +++ b/internal/io/dlog/loggers/logger.go @@ -10,6 +10,8 @@ import ( type Logger interface { Log(now time.Time, message string) LogWithColors(now time.Time, message, messageWithColors string) + Raw(now time.Time, message string) + RawWithColors(now time.Time, message, messageWithColors string) Start(ctx context.Context, wg *sync.WaitGroup) Flush() Pause() diff --git a/internal/io/dlog/loggers/none.go b/internal/io/dlog/loggers/none.go index 270027f..973ae3c 100644 --- a/internal/io/dlog/loggers/none.go +++ b/internal/io/dlog/loggers/none.go @@ -9,13 +9,13 @@ import ( // don't log anything type none struct{} -func (none) Start(ctx context.Context, wg *sync.WaitGroup) { wg.Done() } -func (none) Log(now time.Time, message string) {} - +func (none) Start(ctx context.Context, wg *sync.WaitGroup) { wg.Done() } +func (none) Log(now time.Time, message string) {} func (none) LogWithColors(now time.Time, message, coloredMessage string) {} - -func (none) Flush() {} -func (none) Pause() {} -func (none) Resume() {} -func (none) Rotate() {} -func (none) SupportsColors() bool { return false } +func (none) Raw(now time.Time, message string) {} +func (none) RawWithColors(now time.Time, message, coloredMessage string) {} +func (none) Flush() {} +func (none) Pause() {} +func (none) Resume() {} +func (none) Rotate() {} +func (none) SupportsColors() bool { return false } diff --git a/internal/io/dlog/loggers/stdout.go b/internal/io/dlog/loggers/stdout.go index 05485c6..0369ed7 100644 --- a/internal/io/dlog/loggers/stdout.go +++ b/internal/io/dlog/loggers/stdout.go @@ -25,14 +25,22 @@ func (s *stdout) Start(ctx context.Context, wg *sync.WaitGroup) { } func (s *stdout) Log(now time.Time, message string) { - s.log(message) + s.log(message, true) } func (s *stdout) LogWithColors(now time.Time, message, coloredMessage string) { - s.log(coloredMessage) + s.log(coloredMessage, true) } -func (s *stdout) log(message string) { +func (s *stdout) Raw(now time.Time, message string) { + s.log(message, false) +} + +func (s *stdout) RawWithColors(now time.Time, message, coloredMessage string) { + s.log(coloredMessage, false) +} + +func (s *stdout) log(message string, nl bool) { s.mutex.Lock() defer s.mutex.Unlock() @@ -43,7 +51,11 @@ func (s *stdout) log(message string) { default: } - fmt.Println(message) + if nl { + fmt.Println(message) + return + } + fmt.Print(message) } func (s *stdout) Pause() { s.pauseCh <- struct{}{} } diff --git a/internal/io/fs/readfile.go b/internal/io/fs/readfile.go index 18c20c0..e499853 100644 --- a/internal/io/fs/readfile.go +++ b/internal/io/fs/readfile.go @@ -167,7 +167,6 @@ func (f readFile) read(ctx context.Context, fd *os.File, reader *bufio.Reader, rawLines chan *bytes.Buffer, truncate <-chan struct{}) error { var offset uint64 - lineLengthThreshold := 1024 * 1024 // 1mb warnedAboutLongLine := false message := pool.BytesBuffer.Get().(*bytes.Buffer) @@ -190,31 +189,38 @@ func (f readFile) read(ctx context.Context, fd *os.File, reader *bufio.Reader, } if !f.seekEOF { dlog.Common.Info(f.FilePath(), "End of file reached") + if len(message.Bytes()) > 0 { + select { + case rawLines <- message: + case <-ctx.Done(): + } + } return nil } time.Sleep(time.Millisecond * 100) continue } + offset++ + message.WriteByte(b) switch b { case '\n': select { case rawLines <- message: message = pool.BytesBuffer.Get().(*bytes.Buffer) - //fmt.Printf("%d %d %p\n", message.Len(), message.Cap(), message) warnedAboutLongLine = false case <-ctx.Done(): return nil } default: + // TODO: Add integration test with input file having a very long line. if message.Len() >= lineLengthThreshold { if !warnedAboutLongLine { f.serverMessages <- dlog.Common.Warn(f.filePath, "Long log line, splitting into multiple lines") warnedAboutLongLine = true } - message.WriteString("\n") select { case rawLines <- message: message = pool.BytesBuffer.Get().(*bytes.Buffer) @@ -222,7 +228,6 @@ func (f readFile) read(ctx context.Context, fd *os.File, reader *bufio.Reader, return nil } } - message.WriteByte(b) } } } |
