mirror of
https://github.com/anchore/syft.git
synced 2026-10-30 14:02:26 +01:00
* chore(deps): update tools to latest versions Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * fix linting Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> --------- Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com> Co-authored-by: spiffcs <32073428+spiffcs@users.noreply.github.com> Co-authored-by: Alex Goodman <wagoodman@users.noreply.github.com>
68 lines
1.3 KiB
Go
68 lines
1.3 KiB
Go
package ui
|
|
|
|
import (
|
|
"io"
|
|
"os"
|
|
"time"
|
|
|
|
"github.com/anchore/syft/internal/log"
|
|
)
|
|
|
|
const defaultStdoutLogBufferSize = 1024
|
|
|
|
// CaptureStdoutToTraceLog replaces stdout and redirects output to the log as trace lines. The return value is a
|
|
// function, which is used to stop the current capturing of output and restore the original file.
|
|
// Example:
|
|
//
|
|
// restore := CaptureStdoutToTraceLog()
|
|
// // here, stdout will be captured and redirected to the provided writer
|
|
// restore() // block until the output has all been sent to the writer and restore the original stdout
|
|
func CaptureStdoutToTraceLog() func() {
|
|
return capture(&os.Stdout, newLogWriter(), defaultStdoutLogBufferSize)
|
|
}
|
|
|
|
func capture(target **os.File, writer io.Writer, bufSize int) func() {
|
|
original := *target
|
|
|
|
r, w, _ := os.Pipe()
|
|
|
|
*target = w
|
|
|
|
done := make(chan struct{}, 1)
|
|
|
|
go func() {
|
|
defer func() {
|
|
done <- struct{}{}
|
|
}()
|
|
|
|
buf := make([]byte, bufSize)
|
|
for {
|
|
if original == nil {
|
|
break
|
|
}
|
|
|
|
n, err := r.Read(buf)
|
|
if n > 0 {
|
|
_, _ = writer.Write(buf[0:n])
|
|
}
|
|
|
|
if err != nil {
|
|
break
|
|
}
|
|
}
|
|
}()
|
|
|
|
return func() {
|
|
if original != nil {
|
|
_ = w.Close()
|
|
select {
|
|
case <-done:
|
|
case <-time.After(1 * time.Second):
|
|
log.Debugf("stdout buffer timed out after 1 second")
|
|
}
|
|
*target = original
|
|
original = nil
|
|
}
|
|
}
|
|
}
|