Skip to content

Commit

Permalink
Use a buffered channel instead of a mutex to coordinate the terminal …
Browse files Browse the repository at this point in the history
…output with minimal impact on the observed program.
  • Loading branch information
spytheman committed Feb 2, 2019
1 parent 48ee1c0 commit 4837e7b
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 18 deletions.
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,11 @@ func main() {
}
}

wg.Wait()

if settings.showEnd {
terminal.Out("End of '" + cmdline + "'")
}

wg.Wait()
terminal.Shutdown()
}
62 changes: 45 additions & 17 deletions terminal/terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"
"io"
"os"
"sync"
"time"
)

Expand Down Expand Up @@ -50,17 +49,44 @@ func TurnOnAbsoluteTimestamps() {
absoluteTimestamps = true
}

var timestampRoundResolution time.Duration = time.Millisecond
var timestampRoundResolution = time.Millisecond

func TurnOnMicrosecondTimestampResolution() {
timestampRoundResolution = time.Microsecond
}

var t time.Time
var mutex = &sync.Mutex{}
var previousTerminalLineTime time.Time

type terminalLine struct {
timestamp time.Time
stream io.Writer
tCode string
s string
}

func newTerminalLine(stream io.Writer, tCode string, s string) terminalLine {
return terminalLine{timestamp: time.Now(), stream: stream, tCode: tCode, s: s}
}

var tLines = make(chan terminalLine, 10)
var allTerminalLinesAreFlushed = make(chan bool)

func init() {
TurnOnColor()
go func() {
c := 0
for line := range tLines {
line.s = fmt.Sprintf("%10d : %s", c, line.s)
writeTerminalLine(line)
c++
}
allTerminalLinesAreFlushed <- true
}()
}

func Shutdown() {
close(tLines)
<-allTerminalLinesAreFlushed // blocks till the tLines channel is drained
}

var combineStderrAndStdout = false
Expand Down Expand Up @@ -88,28 +114,30 @@ func Err(s string) {
}

func lineOut(stream io.Writer, tCode string, s string) {
mutex.Lock()
tLines <- newTerminalLine(stream, tCode, s)
}

func writeTerminalLine(tLine terminalLine) {
if absoluteTimestamps {
now := time.Now()
fmt.Fprintf(stream, "%s[%04d-%02d-%02d %02d:%02d:%02d.%06d]%s %s\n",
tCode,
now := tLine.timestamp
fmt.Fprintf(tLine.stream, "%s[%04d-%02d-%02d %02d:%02d:%02d.%06d]%s %s\n",
tLine.tCode,
now.Year(), now.Month(), now.Day(),
now.Hour(), now.Minute(), now.Second(),
now.Nanosecond()/1000,
tColorLineEnd,
s)
tLine.s)
} else {
if t.IsZero() {
t = time.Now()
if previousTerminalLineTime.IsZero() {
previousTerminalLineTime = tLine.timestamp
}
fmt.Fprintf(stream, "%s[%12s]%s %s\n",
tCode,
time.Since(t).Round(timestampRoundResolution).String(),
fmt.Fprintf(tLine.stream, "%s[%12s]%s %s\n",
tLine.tCode,
time.Since(previousTerminalLineTime).Round(timestampRoundResolution).String(),
tColorLineEnd,
s)
tLine.s)
if !timeRelativeToStart {
t = time.Now()
previousTerminalLineTime = tLine.timestamp
}
}
mutex.Unlock()
}

0 comments on commit 4837e7b

Please sign in to comment.