Skip to content

Commit

Permalink
add a -csv option, that just shows the raw nanoseconds then , the…
Browse files Browse the repository at this point in the history
…n the line, making the output convenient for ploting/analysing with a spreadsheet app like Excel
  • Loading branch information
spytheman committed Nov 6, 2022
1 parent 0ff431a commit 717a032
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 12 deletions.
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/spytheman/gostamp

go 1.19
5 changes: 5 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type programSettings struct {
showVersion bool
useColor bool
useAbsolute bool
useCsv bool
showStart bool
showEnd bool
mergeErr bool
Expand All @@ -41,6 +42,7 @@ func init() {
}
flag.BoolVar(&settings.showVersion, "version", false, "show the tool version")
flag.BoolVar(&settings.useColor, "color", true, "colorize the output")
flag.BoolVar(&settings.useCsv, "csv", false, "do not format the output at all, just show the time in ns, followed by ',' then the output")
flag.BoolVar(&settings.useAbsolute, "absolute", false, "use absolute timestamps")
flag.BoolVar(&settings.showStart, "start", true, "timestamp the start of the execution")
flag.BoolVar(&settings.showEnd, "end", true, "timestamp the end of the execution")
Expand All @@ -57,6 +59,9 @@ func init() {
if !settings.useColor {
terminal.TurnOffColor()
}
if settings.useCsv {
terminal.TurnOnCsv()
}
if settings.useAbsolute {
terminal.TurnOnAbsoluteTimestamps()
}
Expand Down
44 changes: 32 additions & 12 deletions terminal/terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,21 +119,41 @@ func lineOut(stream io.Writer, tCode string, s string) {
func writeTerminalLine(tLine terminalLine) {
if absoluteTimestamps {
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,
tLine.s)
if csvOutput {
fmt.Fprintf(tLine.stream, "%04d-%02d-%02d %02d:%02d:%02d.%06d,%s\n",
now.Year(), now.Month(), now.Day(),
now.Hour(), now.Minute(), now.Second(),
now.Nanosecond()/1000,
tLine.s)
} else {
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,
tLine.s)
}
} else {
fmt.Fprintf(tLine.stream, "%s[%12s]%s %s\n",
tLine.tCode,
time.Since(previousTerminalLineTime).Round(timestampRoundResolution).String(),
tColorLineEnd,
tLine.s)
if csvOutput {
fmt.Fprintf(tLine.stream, "%12d,%s\n",
time.Since(previousTerminalLineTime),
tLine.s)
} else {
fmt.Fprintf(tLine.stream, "%s[%12s]%s %s\n",
tLine.tCode,
time.Since(previousTerminalLineTime).Round(timestampRoundResolution).String(),
tColorLineEnd,
tLine.s)
}
if !timeRelativeToStart {
previousTerminalLineTime = tLine.timestamp
}
}
}

var csvOutput = false

func TurnOnCsv() {
csvOutput = true
}

0 comments on commit 717a032

Please sign in to comment.