+63
-9
lines changedFilter options
+63
-9
lines changed Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
1
1
# Changelog
2
2
3
+
## pg_back 2.0.2
4
+
5
+
* Add quiet mode with the commnad line option -q/--quiet. It takes precedence
6
+
over verbose mode.
7
+
3
8
## pg_back 2.0.1
4
9
5
10
* Use /var/run/postgresql as default host for connections
Original file line number Diff line number Diff line change
@@ -70,6 +70,7 @@ type options struct {
70
70
CfgFile string
71
71
TimeFormat string
72
72
Verbose bool
73
+
Quiet bool
73
74
}
74
75
75
76
func defaultOptions() options {
@@ -190,6 +191,7 @@ func parseCli(args []string) (options, []string, error) {
190
191
pflag.StringVarP(&opts.ConnDb, "dbname", "d", "", "connect to database name\n")
191
192
pflag.StringVar(&pce.LegacyConfig, "convert-legacy-config", "", "convert a pg_back v1 configuration file")
192
193
pflag.BoolVar(&pce.ShowConfig, "print-default-config", false, "print the default configuration\n")
194
+
pflag.BoolVarP(&opts.Quiet, "quiet", "q", false, "quiet mode")
193
195
pflag.BoolVarP(&opts.Verbose, "verbose", "v", false, "verbose mode\n")
194
196
pflag.BoolVarP(&pce.ShowHelp, "help", "?", false, "print usage")
195
197
pflag.BoolVarP(&pce.ShowVersion, "version", "V", false, "print version")
Original file line number Diff line number Diff line change
@@ -35,6 +35,7 @@ import (
35
35
type LevelLog struct {
36
36
logger *log.Logger
37
37
verbose bool
38
+
quiet bool
38
39
}
39
40
40
41
var l = NewLevelLog()
@@ -44,11 +45,20 @@ func NewLevelLog() *LevelLog {
44
45
return &LevelLog{
45
46
logger: log.New(os.Stderr, "", log.LstdFlags|log.Lmsgprefix),
46
47
verbose: false,
48
+
quiet: false,
47
49
}
48
50
}
49
51
50
52
// SetVerbose toggles verbose mode
51
-
func (l *LevelLog) SetVerbose(verbose bool) {
53
+
func (l *LevelLog) SetVerbosity(verbose bool, quiet bool) {
54
+
if quiet {
55
+
l.quiet = quiet
56
+
l.verbose = false
57
+
58
+
// Quiet mode takes over verbose mode
59
+
return
60
+
}
61
+
52
62
l.verbose = verbose
53
63
if verbose {
54
64
l.logger.SetFlags(log.LstdFlags | log.Lmsgprefix | log.Lmicroseconds)
@@ -73,14 +83,18 @@ func (l *LevelLog) Verboseln(v ...interface{}) {
73
83
74
84
// Infof prints a message with INFO: prefix using log.Printf
75
85
func (l *LevelLog) Infof(format string, v ...interface{}) {
76
-
l.logger.SetPrefix("INFO: ")
77
-
l.logger.Printf(format, v...)
86
+
if !l.quiet {
87
+
l.logger.SetPrefix("INFO: ")
88
+
l.logger.Printf(format, v...)
89
+
}
78
90
}
79
91
80
92
// Infoln prints a message with INFO: prefix using log.Println
81
93
func (l *LevelLog) Infoln(v ...interface{}) {
82
-
l.logger.SetPrefix("INFO: ")
83
-
l.logger.Println(v...)
94
+
if !l.quiet {
95
+
l.logger.SetPrefix("INFO: ")
96
+
l.logger.Println(v...)
97
+
}
84
98
}
85
99
86
100
// Warnf prints a message with WARN: prefix using log.Printf
Original file line number Diff line number Diff line change
@@ -38,7 +38,7 @@ func TestLevelLogSetVerbose(t *testing.T) {
38
38
l := NewLevelLog()
39
39
for i, subt := range tests {
40
40
t.Run(fmt.Sprintf("%v", i), func(t *testing.T) {
41
-
l.SetVerbose(subt)
41
+
l.SetVerbosity(subt, false)
42
42
if l.verbose != subt {
43
43
t.Errorf("got %v, want %v", l.verbose, subt)
44
44
}
@@ -65,7 +65,7 @@ func TestLevelLogVerbose(t *testing.T) {
65
65
t.Run(fmt.Sprintf("%v", i), func(t *testing.T) {
66
66
buf := new(bytes.Buffer)
67
67
l.logger.SetOutput(buf)
68
-
l.SetVerbose(subt.verbose)
68
+
l.SetVerbosity(subt.verbose, false)
69
69
if subt.fOrln {
70
70
l.Verbosef("%s", subt.message)
71
71
} else {
@@ -230,3 +230,36 @@ func TestLevelLogFatal(t *testing.T) {
230
230
})
231
231
}
232
232
}
233
+
234
+
func TestLevelLogQuiet(t *testing.T) {
235
+
l := NewLevelLog()
236
+
237
+
// Set verbose and quiet to ensure quiet takes over verbose when both are true
238
+
l.SetVerbosity(true, true)
239
+
240
+
buf := new(bytes.Buffer)
241
+
l.logger.SetOutput(buf)
242
+
243
+
l.Verbosef("test")
244
+
if buf.Len() > 0 {
245
+
t.Errorf("log function Verbosef has printed data when it should not")
246
+
}
247
+
248
+
buf.Reset()
249
+
l.Verboseln("test")
250
+
if buf.Len() > 0 {
251
+
t.Errorf("log function Verboseln has printed data when it should not")
252
+
}
253
+
254
+
buf.Reset()
255
+
l.Infof("test")
256
+
if buf.Len() > 0 {
257
+
t.Errorf("log function Infof has printed data when it should not")
258
+
}
259
+
260
+
buf.Reset()
261
+
l.Infoln("test")
262
+
if buf.Len() > 0 {
263
+
t.Errorf("log function Infoln has printed data when it should not")
264
+
}
265
+
}
Original file line number Diff line number Diff line change
@@ -127,8 +127,8 @@ func main() {
127
127
}
128
128
}
129
129
130
-
// Enable verbose mode as soon as possible
131
-
l.SetVerbose(cliOpts.Verbose)
130
+
// Enable verbose mode or quiet mode as soon as possible
131
+
l.SetVerbosity(cliOpts.Verbose, cliOpts.Quiet)
132
132
133
133
// Load configuration file and allow the default configuration
134
134
// file to be absent
You can’t perform that action at this time.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4