add outfile option

using regular shell redirection has some issues with logrotate that I'm
trying to work around.
master
Jordan Orelli 11 years ago
parent 6bf82331a4
commit 19fad7e7ba

@ -1,11 +1,17 @@
package main
import (
"flag"
"fmt"
"math/rand"
"os"
"time"
)
var (
fname string
)
// generates a pseudorandom string of length n that is composed of alphanumeric
// characters.
func randomString(n int) string {
@ -17,12 +23,30 @@ func randomString(n int) string {
return string(buf)
}
func outFile() (*os.File, error) {
if fname == "" {
return os.Stdout, nil
}
options := os.O_WRONLY | os.O_APPEND | os.O_CREATE
return os.OpenFile(fname, options, 0644)
}
func main() {
flag.Parse()
f, err := outFile()
if err != nil {
fmt.Printf("ERROR: %v\n", err)
os.Exit(1)
}
defer f.Close()
for t := range time.Tick(100 * time.Millisecond) {
fmt.Printf("%v %v %v\n", t.UnixNano(), randomString(32), randomString(32))
fmt.Fprintf(f, "%v %v %v\n", t.UnixNano(), randomString(32), randomString(32))
}
}
func init() {
flag.StringVar(&fname, "file", "", "destination file to which random data will be appended")
rand.Seed(time.Now().UnixNano())
}

Loading…
Cancel
Save