timestamp formatting

master
Jordan Orelli 10 years ago
parent b8bd673a31
commit 840f562ef4

@ -11,11 +11,13 @@ import (
) )
var ( var (
fname string fname string // output filename
ftruncate bool freq float64 // frequency at which lines are written
reopen bool ftruncate bool // whether or not to truncate file on open
freq float64 pidfile string // path of pidfile to write out
pidfile string reopen bool // whether or not to reopen the file handle on every line write
tsformat string // timestamp format
ts func() string // function to get a timestamp string
) )
// generates a pseudorandom string of length n that is composed of alphanumeric // generates a pseudorandom string of length n that is composed of alphanumeric
@ -75,7 +77,7 @@ START:
fmt.Printf("ERROR: unable to write line: %v", err) fmt.Printf("ERROR: unable to write line: %v", err)
} }
case <-hup: case <-hup:
fmt.Fprintf(f, "%v HUP\n", time.Now().UnixNano()) fmt.Fprintf(f, "%s HUP\n", ts())
f.Close() f.Close()
goto START goto START
} }
@ -102,20 +104,51 @@ func writePid() {
fmt.Fprintln(f, os.Getpid()) fmt.Fprintln(f, os.Getpid())
} }
func main() { func flags() {
flag.Parse() flag.Parse()
switch tsformat {
case "":
ts = func() string {
t := time.Now()
return fmt.Sprintf("%s %4.4d", t.Format("15:04:05"), t.Nanosecond()/1e5)
}
case "ns":
ts = func() string {
t := time.Now()
return fmt.Sprintf("%d", t.UnixNano())
}
case "ms":
ts = func() string {
t := time.Now()
return fmt.Sprintf("%d", t.UnixNano()/1e3)
}
case "epoch", "unix":
ts = func() string {
t := time.Now()
return fmt.Sprintf("%d", t.Unix())
}
default:
ts = func() string {
return time.Now().Format(tsformat)
}
}
}
func main() {
flags()
writePid() writePid()
c := make(chan string) c := make(chan string)
writeLines(c) writeLines(c)
for t := range time.Tick(time.Duration(1e9 / freq)) { for _ = range time.Tick(time.Duration(1e9 / freq)) {
c <- fmt.Sprintf("%v %v %v\n", t.UnixNano(), randomString(32), randomString(32)) c <- fmt.Sprintf("%s %s %s\n", ts(), randomString(32), randomString(32))
} }
} }
func init() { func init() {
flag.StringVar(&fname, "file", "", "destination file to which random data will be written") flag.StringVar(&fname, "file", "", "destination file to which random data will be written")
flag.StringVar(&tsformat, "ts-format", "", "timestamp format")
flag.StringVar(&pidfile, "pidfile", "", "file to which a pid is written") flag.StringVar(&pidfile, "pidfile", "", "file to which a pid is written")
flag.BoolVar(&ftruncate, "truncate", false, "truncate file on opening instead of appending") flag.BoolVar(&ftruncate, "truncate", false, "truncate file on opening instead of appending")
flag.BoolVar(&reopen, "reopen", false, "reopen file handle on every write instead of using a persistent handle") flag.BoolVar(&reopen, "reopen", false, "reopen file handle on every write instead of using a persistent handle")

Loading…
Cancel
Save