@@ -4,26 +4,40 @@ import (
4
4
"errors"
5
5
"fmt"
6
6
"os"
7
+ "strconv"
8
+ "strings"
9
+ "time"
7
10
8
11
"github.com/spf13/pflag"
9
12
)
10
13
11
- const version = "0.4.2 "
14
+ const version = "0.4.3 "
12
15
const usage = `flog is a fake log generator for common log formats
13
16
14
17
Usage: flog [options]
15
18
16
19
Version: %s
17
20
18
21
Options:
19
- -f, --format string choose log format. ("apache_common"|"apache_combined"|"apache_error"|"rfc3164"|"rfc5424"|"json") (default "apache_common")
22
+ -f, --format string log format. available formats:
23
+ - apache_common (default)
24
+ - apache_combined
25
+ - apache_error
26
+ - rfc3164
27
+ - rfc5424
28
+ - json
20
29
-o, --output string output filename. Path-like is allowed. (default "generated.log")
21
- -t, --type string log output type. ("stdout"|"log"|"gz") (default "stdout")
30
+ -t, --type string log output type. available types:
31
+ - stdout (default)
32
+ - log
33
+ - gz
22
34
-n, --number integer number of lines to generate.
23
35
-b, --bytes integer size of logs to generate (in bytes).
24
36
"bytes" will be ignored when "number" is set.
25
- -s, --sleep numeric fix creation time interval for each log (in seconds). It does not actually sleep.
26
- -d, --delay numeric delay log generation speed (in seconds).
37
+ -s, --sleep duration fix creation time interval for each log (default unit "seconds"). It does not actually sleep.
38
+ examples: 10, 20ms, 5s, 1m
39
+ -d, --delay duration delay log generation speed (default unit "seconds").
40
+ examples: 10, 20ms, 5s, 1m
27
41
-p, --split-by integer set the maximum number of lines or maximum size in bytes of a log file.
28
42
with "number" option, the logs will be split whenever the maximum number of lines is reached.
29
43
with "byte" option, the logs will be split whenever the maximum size in bytes is reached.
@@ -41,8 +55,8 @@ type Option struct {
41
55
Type string
42
56
Number int
43
57
Bytes int
44
- Sleep float64
45
- Delay float64
58
+ Sleep time. Duration
59
+ Delay time. Duration
46
60
SplitBy int
47
61
Overwrite bool
48
62
Forever bool
@@ -113,19 +127,33 @@ func ParseBytes(bytes int) (int, error) {
113
127
}
114
128
115
129
// ParseSleep validates the given sleep
116
- func ParseSleep (sleep float64 ) (float64 , error ) {
130
+ func ParseSleep (sleepString string ) (time.Duration , error ) {
131
+ if strings .ContainsAny (sleepString , "nsuµmh" ) {
132
+ return time .ParseDuration (sleepString )
133
+ }
134
+ sleep , err := strconv .ParseFloat (sleepString , 64 )
135
+ if err != nil {
136
+ return 0 , err
137
+ }
117
138
if sleep < 0 {
118
- return 0.0 , errors .New ("sleep can not be negative " )
139
+ return 0.0 , errors .New ("sleep time must be positive " )
119
140
}
120
- return sleep , nil
141
+ return time . Duration ( sleep * float64 ( time . Second )) , nil
121
142
}
122
143
123
144
// ParseDelay validates the given sleep
124
- func ParseDelay (delay float64 ) (float64 , error ) {
145
+ func ParseDelay (delayString string ) (time.Duration , error ) {
146
+ if strings .ContainsAny (delayString , "nsuµmh" ) {
147
+ return time .ParseDuration (delayString )
148
+ }
149
+ delay , err := strconv .ParseFloat (delayString , 64 )
150
+ if err != nil {
151
+ return 0 , err
152
+ }
125
153
if delay < 0 {
126
- return 0.0 , errors .New ("delay can not be negative " )
154
+ return 0.0 , errors .New ("delay time must be positive " )
127
155
}
128
- return delay , nil
156
+ return time . Duration ( delay * float64 ( time . Second )) , nil
129
157
}
130
158
131
159
// ParseSplitBy validates the given split-by
@@ -142,16 +170,16 @@ func ParseOptions() *Option {
142
170
143
171
opts := defaultOptions ()
144
172
145
- help := pflag .BoolP ("help" , "h" , false , "Show usage " )
173
+ help := pflag .BoolP ("help" , "h" , false , "Show this help message " )
146
174
version := pflag .BoolP ("version" , "v" , false , "Show version" )
147
175
format := pflag .StringP ("format" , "f" , opts .Format , "Log format" )
148
- output := pflag .StringP ("output" , "o" , opts .Output , "Output filename. Path-like filename is allowed " )
176
+ output := pflag .StringP ("output" , "o" , opts .Output , "Path-like output filename " )
149
177
logType := pflag .StringP ("type" , "t" , opts .Type , "Log output type" )
150
178
number := pflag .IntP ("number" , "n" , opts .Number , "Number of lines to generate" )
151
179
bytes := pflag .IntP ("bytes" , "b" , opts .Bytes , "Size of logs to generate. (in bytes)" )
152
- sleep := pflag .Float64P ("sleep" , "s" , opts . Sleep , "Creation time interval for each log (in seconds)" )
153
- delay := pflag .Float64P ("delay" , "d" , opts . Delay , "Delay log generation speed (in seconds)" )
154
- splitBy := pflag .IntP ("split" , "p" , opts .SplitBy , "Set the maximum number of lines or maximum size in bytes of a log file" )
180
+ sleepString := pflag .StringP ("sleep" , "s" , "0s" , "Creation time interval (default unit: seconds)" )
181
+ delayString := pflag .StringP ("delay" , "d" , "0s" , "Log generation speed (default unit: seconds)" )
182
+ splitBy := pflag .IntP ("split" , "p" , opts .SplitBy , "Maximum number of lines or size of a log file" )
155
183
overwrite := pflag .BoolP ("overwrite" , "w" , false , "Overwrite the existing log files" )
156
184
forever := pflag .BoolP ("loop" , "l" , false , "Loop output forever until killed" )
157
185
@@ -177,10 +205,10 @@ func ParseOptions() *Option {
177
205
if opts .Bytes , err = ParseBytes (* bytes ); err != nil {
178
206
errorExit (err )
179
207
}
180
- if opts .Sleep , err = ParseSleep (* sleep ); err != nil {
208
+ if opts .Sleep , err = ParseSleep (* sleepString ); err != nil {
181
209
errorExit (err )
182
210
}
183
- if opts .Delay , err = ParseDelay (* delay ); err != nil {
211
+ if opts .Delay , err = ParseDelay (* delayString ); err != nil {
184
212
errorExit (err )
185
213
}
186
214
if opts .SplitBy , err = ParseSplitBy (* splitBy ); err != nil {
0 commit comments