Skip to content

Commit

Permalink
feat: add sync stdout
Browse files Browse the repository at this point in the history
Signed-off-by: Zxilly <[email protected]>
  • Loading branch information
Zxilly committed Sep 1, 2024
1 parent 31f7eaa commit 1f7ac0e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
2 changes: 1 addition & 1 deletion cmd/gsa/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func entry() error {
return err
}
} else {
writer = os.Stdout
writer = utils.SyncStdout
if Options.Web {
writer = new(bytes.Buffer)
}
Expand Down
16 changes: 12 additions & 4 deletions internal/utils/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ var startTime time.Time

func InitLogger(level slog.Level) {
startTime = time.Now()
slog.SetDefault(slog.New(slog.NewTextHandler(Stderr, &slog.HandlerOptions{
slog.SetDefault(slog.New(slog.NewTextHandler(SyncStderr, &slog.HandlerOptions{
ReplaceAttr: func(_ []string, a slog.Attr) slog.Attr {
// remove time
// replace time with duration
if a.Key == "time" {
return slog.Duration(slog.TimeKey, time.Since(startTime))
}
Expand Down Expand Up @@ -60,9 +60,17 @@ func (s *SyncOutput) SetOutput(output io.Writer) {
s.output = output
}

var Stderr = &SyncOutput{
var SyncStdout = &SyncOutput{
Mutex: sync.Mutex{},
output: os.Stdout,
}

var SyncStderr = &SyncOutput{
Mutex: sync.Mutex{},
output: os.Stderr,
}

var _ io.Writer = Stderr
var (
_ io.Writer = SyncStdout
_ io.Writer = SyncStderr
)

0 comments on commit 1f7ac0e

Please sign in to comment.