-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
65 lines (54 loc) · 1.31 KB
/
options.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
package logging
import (
"flag"
)
type Options struct {
Module string
Defaults *Options
Debug bool
Verbose bool
Quiet bool
}
func (options *Options) InitFlags() {
var flagSuffix = ""
var descSuffix = ""
if options.Module != "" {
flagSuffix = "." + options.Module
descSuffix = " for " + options.Module
}
flag.BoolVar(&options.Debug, "debug"+flagSuffix, false, "Log debug"+descSuffix)
flag.BoolVar(&options.Verbose, "verbose"+flagSuffix, false, "Log info"+descSuffix)
flag.BoolVar(&options.Quiet, "quiet"+flagSuffix, false, "Do not log warnings"+descSuffix)
}
func (options *Options) applyDefaults() {
if options.Defaults.Debug {
options.Debug = true
}
if options.Defaults.Verbose {
options.Verbose = true
}
if options.Defaults.Quiet {
options.Quiet = true
}
}
func (options *Options) MakeLogging() Logging {
var logging = Logging{}
var logSuffix = ": "
if options.Module != "" {
logSuffix = " " + options.Module + logSuffix
}
if options.Defaults != nil {
options.applyDefaults()
}
if options.Debug {
logging.Debug = MakeLogger("DEBUG" + logSuffix)
}
if options.Debug || options.Verbose {
logging.Info = MakeLogger("INFO" + logSuffix)
}
if !options.Quiet {
logging.Warn = MakeLogger("WARN" + logSuffix)
}
logging.Error = MakeLogger("ERROR" + logSuffix)
return logging
}