fix: add buffer to utils

This commit is contained in:
Rogee
2024-12-17 09:50:28 +08:00
parent 7dd4d76a71
commit be19daf5fe
2 changed files with 80 additions and 17 deletions

View File

@@ -0,0 +1,26 @@
package utils
import (
"bufio"
"io"
)
// NewLogBuffer creates a buffer that can be used to capture output stream
// and write to a logger in real time
func NewLogBuffer(output func(string)) io.Writer {
reader, writer := io.Pipe()
go func() {
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
output(scanner.Text())
}
}()
return writer
}
// NewCombinedBuffer combines multiple io.Writers
func NewCombinedBuffer(writers ...io.Writer) io.Writer {
return io.MultiWriter(writers...)
}