-
-
Save lmas/4f044439a900e5db038ae86446b6ff19 to your computer and use it in GitHub Desktop.
Separate chunks of live log output (like spaceror tailer)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"io" | |
"os" | |
"strings" | |
"time" | |
) | |
func main() { | |
timer := time.NewTimer(0) | |
timer.Stop() // don't let the timer fire yet | |
go func() { | |
last := time.Now() | |
for ts := range timer.C { | |
fmt.Fprintf(os.Stderr, | |
"%s %s %s\n", | |
ts.Format(time.RFC3339), | |
ts.Sub(last).Truncate(100*time.Millisecond), | |
strings.Repeat("-", 20), | |
) | |
last = ts | |
} | |
}() | |
io.Copy( | |
writeFunc(func(p []byte) (int, error) { | |
timer.Reset(3 * time.Second) | |
return os.Stdout.Write(p) | |
}), | |
os.Stdin, | |
) | |
} | |
type writeFunc func(p []byte) (int, error) | |
func (wf writeFunc) Write(p []byte) (int, error) { return wf(p) } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment