add outfile option
using regular shell redirection has some issues with logrotate that I'm trying to work around.master
parent
6bf82331a4
commit
19fad7e7ba
@ -1,28 +1,52 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"flag"
|
||||||
"math/rand"
|
"fmt"
|
||||||
"time"
|
"math/rand"
|
||||||
|
"os"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
fname 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
|
||||||
// characters.
|
// characters.
|
||||||
func randomString(n int) string {
|
func randomString(n int) string {
|
||||||
var alpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
var alpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
||||||
buf := make([]byte, n)
|
buf := make([]byte, n)
|
||||||
for i := 0; i < len(buf); i++ {
|
for i := 0; i < len(buf); i++ {
|
||||||
buf[i] = alpha[rand.Intn(len(alpha)-1)]
|
buf[i] = alpha[rand.Intn(len(alpha)-1)]
|
||||||
}
|
}
|
||||||
return string(buf)
|
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() {
|
func main() {
|
||||||
for t := range time.Tick(100 * time.Millisecond) {
|
flag.Parse()
|
||||||
fmt.Printf("%v %v %v\n", t.UnixNano(), randomString(32), randomString(32))
|
|
||||||
}
|
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.Fprintf(f, "%v %v %v\n", t.UnixNano(), randomString(32), randomString(32))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
rand.Seed(time.Now().UnixNano())
|
flag.StringVar(&fname, "file", "", "destination file to which random data will be appended")
|
||||||
|
rand.Seed(time.Now().UnixNano())
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue