-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
95 lines (80 loc) · 2.1 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package main
//go:generate go run generators/BOFHgenerator.go
//go:generate go fmt
import (
"flag"
"log"
"math/rand"
"os"
"time"
)
var logLevel = [...]string{
"DEBUG", "INFO", "WARN", "ERROR", "FATAL",
}
var app = [...]string{
"httpd", "builder", "fluxcomp", "core", "coffeemaker",
}
var (
bofhLen = len(BOFH)
logLevelLen = len(logLevel)
appLen = len(app)
param commandLineParameter
)
type commandLineParameter struct {
freq int64
count int64
stdout bool
emitter string
}
func main() {
parseCmdLineArguments()
if param.stdout {
log.SetOutput(os.Stdout)
}
emitterString := emitterName()
if param.count > 0 {
for ; param.count > 0; param.count-- {
logAndWait(emitterString())
}
return //job done
}
//we are here? loop forever!
for {
logAndWait(emitterString())
}
}
func logAndWait(emitter string) {
log.Printf("%s|%s|%s\n", logLevel[rand.Intn(logLevelLen)], emitter, BOFH[rand.Intn(bofhLen)])
time.Sleep(time.Second / time.Duration(param.freq))
}
func parseCmdLineArguments() {
var help bool
flag.Int64Var(¶m.freq, "freq", 2, "frequency of emitted fake lines (per second, min. 1)")
flag.Int64Var(¶m.freq, "f", 2, "short for -freq")
flag.Int64Var(¶m.count, "count", 0, "number of generated fake lines (0 infinite) (default 0)")
flag.Int64Var(¶m.count, "c", 0, "short for -count (default 0)")
flag.BoolVar(¶m.stdout, "stdout", false, "log to stdout instead of stderr")
flag.StringVar(¶m.emitter, "emitter", "", "use as emitter instead of changing it randomly")
flag.StringVar(¶m.emitter, "e", "", "short for -emitter")
flag.BoolVar(&help, "h", false, "show help")
flag.BoolVar(&help, "help", false, "show help")
flag.Parse()
if help {
flag.PrintDefaults()
os.Exit(1)
}
}
func emitterName() func() string {
// this is nice and saves the if in every log output.
// Is it clear and easy to understand?
// Fun and clever does not serve the purpose here.
// Will be gone in the next commit.
if param.emitter != "" {
return func() string {
return param.emitter
}
}
return func() string {
return app[rand.Intn(appLen)]
}
}