Visualizer #3

Merged
thomas merged 12 commits from rudi_s into main 2024-05-28 11:21:00 +02:00
Showing only changes of commit d5aeb6c0bc - Show all commits

View file

@ -24,9 +24,9 @@ import (
var static embed.FS
func main() {
if len(os.Args) != 3 {
if len(os.Args) != 2 && len(os.Args) != 3 {
log.SetFlags(0)
log.Fatalf("usage: %s <fifo> <json-dup-prefix>", os.Args[0])
log.Fatalf("usage: %s <fifo> [<json-dup-prefix>]", os.Args[0])
}
var mutex sync.Mutex
@ -35,15 +35,21 @@ func main() {
go func() {
for {
path := os.Args[1]
dupPath := os.Args[2] + "." + time.Now().Format(time.RFC3339)
var dupPath string
if len(os.Args) == 3 {
dupPath = os.Args[2] + "." + time.Now().Format(time.RFC3339)
}
fh, err := os.Open(path)
if err != nil {
log.Fatal(err)
}
dupFh, err := os.Create(dupPath)
if err != nil {
log.Fatal(err)
var dupFh *os.File
if dupPath != "" {
dupFh, err = os.Create(dupPath)
if err != nil {
log.Fatal(err)
}
}
r := bufio.NewReader(fh)
@ -56,9 +62,11 @@ func main() {
// Duplicate JSON input to a file so it can be replayed later
// if necessary
_, err = dupFh.WriteString(line)
if err != nil {
log.Fatalf("write %q: %v", dupPath, err)
if dupFh != nil {
_, err = dupFh.WriteString(line)
if err != nil {
log.Fatalf("write %q: %v", dupPath, err)
}
}
// Send to all listeners
@ -73,9 +81,11 @@ func main() {
if err != nil {
log.Fatalf("close %q: %v", path, err)
}
err = dupFh.Close()
if err != nil {
log.Fatalf("close %q: %v", dupPath, err)
if dupFh != nil {
err = dupFh.Close()
if err != nil {
log.Fatalf("close %q: %v", dupPath, err)
}
}
}
}()