Skip to content

Commit

Permalink
Add 'delay' option
Browse files Browse the repository at this point in the history
  • Loading branch information
mingrammer committed Dec 16, 2018
1 parent 693b934 commit 5562e55
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 33 deletions.
40 changes: 26 additions & 14 deletions flog.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ import (
// Generate generates the logs with given options
func Generate(option *Option) error {
splitCount := 1
delta := time.Duration(0)
created := time.Now()

delay := time.Duration(0)
if option.Delay > 0 {
delay = time.Duration(option.Delay*float64(time.Second/time.Millisecond)) * time.Millisecond
}

logFileName := option.Output
writer, err := NewWriter(option.Type, logFileName)
Expand All @@ -24,16 +29,22 @@ func Generate(option *Option) error {

if option.Forever {
for {
log := NewLog(option.Format, delta)
if delay > 0 {
time.Sleep(delay)
}
log := NewLog(option.Format, created)
writer.Write([]byte(log + "\n"))
delta += time.Duration(option.Sleep*float64(time.Second/time.Millisecond)) * time.Millisecond
created = created.Add(time.Duration(option.Sleep*float64(time.Second/time.Millisecond)) * time.Millisecond)
}
}

if option.Bytes == 0 {
// Generates the logs until the certain number of lines is reached
for line := 0; line < option.Number; line++ {
log := NewLog(option.Format, delta)
if delay > 0 {
time.Sleep(delay)
}
log := NewLog(option.Format, created)
writer.Write([]byte(log + "\n"))

if (option.Type != "stdout") && (option.SplitBy > 0) && (line > option.SplitBy*splitCount) {
Expand All @@ -45,14 +56,16 @@ func Generate(option *Option) error {

splitCount++
}

delta += time.Duration(option.Sleep*float64(time.Second/time.Millisecond)) * time.Millisecond
created = created.Add(time.Duration(option.Sleep*float64(time.Second/time.Millisecond)) * time.Millisecond)
}
} else {
// Generates the logs until the certain size in bytes is reached
bytes := 0
for bytes < option.Bytes {
log := NewLog(option.Format, delta)
if delay > 0 {
time.Sleep(delay)
}
log := NewLog(option.Format, created)
writer.Write([]byte(log + "\n"))

bytes += len(log)
Expand All @@ -65,8 +78,7 @@ func Generate(option *Option) error {

splitCount++
}

delta += time.Duration(option.Sleep*float64(time.Second/time.Millisecond)) * time.Millisecond
created = created.Add(time.Duration(option.Sleep*float64(time.Second/time.Millisecond)) * time.Millisecond)
}
}

Expand Down Expand Up @@ -100,16 +112,16 @@ func NewWriter(logType string, logFileName string) (io.WriteCloser, error) {
}

// NewLog creates a log for given format
func NewLog(format string, delta time.Duration) string {
func NewLog(format string, t time.Time) string {
switch format {
case "apache_common":
return NewApacheCommonLog(delta)
return NewApacheCommonLog(t)
case "apache_combined":
return NewApacheCombinedLog(delta)
return NewApacheCombinedLog(t)
case "apache_error":
return NewApacheErrorLog(delta)
return NewApacheErrorLog(t)
case "rfc3164":
return NewRFC3164Log(delta)
return NewRFC3164Log(t)
default:
return ""
}
Expand Down
11 changes: 6 additions & 5 deletions flog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ func ExampleNewLog() {
monkey.Patch(time.Now, func() time.Time { return stopped })
defer monkey.Unpatch(time.Now)

fmt.Println(NewLog("apache_common", 0))
fmt.Println(NewLog("apache_combined", 0))
fmt.Println(NewLog("apache_error", 0))
fmt.Println(NewLog("rfc3164", 0))
fmt.Println(NewLog("unknown", 0))
created := time.Now()
fmt.Println(NewLog("apache_common", created))
fmt.Println(NewLog("apache_combined", created))
fmt.Println(NewLog("apache_error", created))
fmt.Println(NewLog("rfc3164", created))
fmt.Println(NewLog("unknown", created))
// Output:
// 222.83.191.222 - Kozey7157 697 [2018-04-22T09:30:00Z] "DELETE /innovate/next-generation" 302 24570
// 174.144.199.149 - Mosciski7760 386 [2018-04-22T09:30:00Z] "GET /networks/revolutionary" 204 70360 "https://www.directeyeballs.org/streamline" "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_7) AppleWebKit/5312 (KHTML, like Gecko) Chrome/37.0.821.0 Mobile Safari/5312"
Expand Down
16 changes: 8 additions & 8 deletions log.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ const (
)

// NewApacheCommonLog creates a log string with apache common log format
func NewApacheCommonLog(delta time.Duration) string {
func NewApacheCommonLog(t time.Time) string {
return fmt.Sprintf(
ApacheCommonLog,
gofakeit.IPv4Address(),
gofakeit.Username(),
gofakeit.Number(0, 1000),
time.Now().Add(delta).Format(time.RFC3339),
t.Format(time.RFC3339),
gofakeit.HTTPMethod(),
RandResourceURI(),
gofakeit.StatusCode(),
Expand All @@ -34,13 +34,13 @@ func NewApacheCommonLog(delta time.Duration) string {
}

// NewApacheCombinedLog creates a log string with apache combined log format
func NewApacheCombinedLog(delta time.Duration) string {
func NewApacheCombinedLog(t time.Time) string {
return fmt.Sprintf(
ApacheCombinedLog,
gofakeit.IPv4Address(),
gofakeit.Username(),
gofakeit.Number(1, 1000),
time.Now().Add(delta).Format(time.RFC3339),
t.Format(time.RFC3339),
gofakeit.HTTPMethod(),
RandResourceURI(),
gofakeit.StatusCode(),
Expand All @@ -51,10 +51,10 @@ func NewApacheCombinedLog(delta time.Duration) string {
}

// NewApacheErrorLog creates a log string with apache error log format
func NewApacheErrorLog(delta time.Duration) string {
func NewApacheErrorLog(t time.Time) string {
return fmt.Sprintf(
ApacheErrorLog,
time.Now().Add(delta).Format(time.RFC3339),
t.Format(time.RFC3339),
gofakeit.Word(),
gofakeit.LogLevel("apache"),
gofakeit.Number(1, 10000),
Expand All @@ -65,11 +65,11 @@ func NewApacheErrorLog(delta time.Duration) string {
}

// NewRFC3164Log creates a log string with syslog (RFC3164) format
func NewRFC3164Log(delta time.Duration) string {
func NewRFC3164Log(t time.Time) string {
return fmt.Sprintf(
RFC3164Log,
gofakeit.Number(0, 191),
time.Now().Add(delta).Format(RFC3164),
t.Format(RFC3164),
gofakeit.Username(),
gofakeit.BuzzWord(),
gofakeit.Number(1, 10000),
Expand Down
12 changes: 8 additions & 4 deletions log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ func ExampleNewApacheCommonLog() {
monkey.Patch(time.Now, func() time.Time { return stopped })
defer monkey.Unpatch(time.Now)

fmt.Println(NewApacheCommonLog(0))
created := time.Now()
fmt.Println(NewApacheCommonLog(created))
// Output: 222.83.191.222 - Kozey7157 697 [2018-04-22T09:30:00Z] "DELETE /innovate/next-generation" 302 24570
}

Expand All @@ -26,7 +27,8 @@ func ExampleNewApacheCombinedLog() {
monkey.Patch(time.Now, func() time.Time { return stopped })
defer monkey.Unpatch(time.Now)

fmt.Println(NewApacheCombinedLog(0))
created := time.Now()
fmt.Println(NewApacheCombinedLog(created))
// Output: 222.83.191.222 - Kozey7157 119 [2018-04-22T09:30:00Z] "DELETE /innovate/next-generation" 302 81317 "https://www.forwardholistic.biz/mission-critical/synergize/morph/sticky" "Mozilla/5.0 (Windows NT 5.01) AppleWebKit/5320 (KHTML, like Gecko) Chrome/40.0.875.0 Mobile Safari/5320"
}

Expand All @@ -36,7 +38,8 @@ func ExampleNewApacheErrorLog() {
monkey.Patch(time.Now, func() time.Time { return stopped })
defer monkey.Unpatch(time.Now)

fmt.Println(NewApacheErrorLog(0))
created := time.Now()
fmt.Println(NewApacheErrorLog(created))
// Output: [2018-04-22T09:30:00Z] [quia:crit] [pid 4214:tid 6037] [client: 90.151.9.107] If we back up the program, we can get to the SSL sensor through the redundant SAS program!
}

Expand All @@ -46,6 +49,7 @@ func ExampleNewRFC3164Log() {
monkey.Patch(time.Now, func() time.Time { return stopped })
defer monkey.Unpatch(time.Now)

fmt.Println(NewRFC3164Log(0))
created := time.Now()
fmt.Println(NewRFC3164Log(created))
// Output: <24>Apr 22 09:30:00 Moen8727 concept[3160]: If we back up the program, we can get to the SSL sensor through the redundant SAS program!
}
19 changes: 17 additions & 2 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ Options:
-n, --number integer number of lines to generate.
-b, --bytes integer size of logs to generate (in bytes).
"bytes" will be ignored when "number" is set.
-s, --sleep numeric sleep interval time between lines (in seconds). It does not actually sleep every log generation.
-s, --sleep numeric creation time interval for each log (in seconds). It does not actually sleep.
-d, --delay numeric delay log generation speed (in seconds).
-p, --split-by integer set the maximum number of lines or maximum size in bytes of a log file.
with "number" option, the logs will be split whenever the maximum number of lines is reached.
with "byte" option, the logs will be split whenever the maximum size in bytes is reached.
Expand All @@ -41,6 +42,7 @@ type Option struct {
Number int
Bytes int
Sleep float64
Delay float64
SplitBy int
Overwrite bool
Forever bool
Expand Down Expand Up @@ -71,6 +73,7 @@ func defaultOptions() *Option {
Number: 1000,
Bytes: 0,
Sleep: 0.0,
Delay: 0.0,
SplitBy: 0,
Overwrite: false,
Forever: false,
Expand Down Expand Up @@ -117,6 +120,14 @@ func ParseSleep(sleep float64) (float64, error) {
return sleep, nil
}

// ParseDelay validates the given sleep
func ParseDelay(delay float64) (float64, error) {
if delay < 0 {
return 0.0, errors.New("delay can not be negative")
}
return delay, nil
}

// ParseSplitBy validates the given split-by
func ParseSplitBy(splitBy int) (int, error) {
if splitBy < 0 {
Expand All @@ -138,7 +149,8 @@ func ParseOptions() *Option {
logType := pflag.StringP("type", "t", opts.Type, "Log output type")
number := pflag.IntP("number", "n", opts.Number, "Number of lines to generate")
bytes := pflag.IntP("bytes", "b", opts.Bytes, "Size of logs to generate. (in bytes)")
sleep := pflag.Float64P("sleep", "s", opts.Sleep, "Sleep interval time between lines. (in seconds)")
sleep := pflag.Float64P("sleep", "s", opts.Sleep, "Creation time interval for each log (in seconds)")
delay := pflag.Float64P("delay", "d", opts.Delay, "Delay log generation speed (in seconds)")
splitBy := pflag.IntP("split", "p", opts.SplitBy, "Set the maximum number of lines or maximum size in bytes of a log file")
overwrite := pflag.BoolP("overwrite", "w", false, "Overwrite the existing log files")
forever := pflag.BoolP("loop", "l", false, "Loop output forever until killed")
Expand Down Expand Up @@ -168,6 +180,9 @@ func ParseOptions() *Option {
if opts.Sleep, err = ParseSleep(*sleep); err != nil {
errorExit(err)
}
if opts.Delay, err = ParseDelay(*delay); err != nil {
errorExit(err)
}
if opts.SplitBy, err = ParseSplitBy(*splitBy); err != nil {
errorExit(err)
}
Expand Down
16 changes: 16 additions & 0 deletions option_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,22 @@ func TestParseSleep(t *testing.T) {
a.Error(err, "there should be an error when negative is given")
}

func TestParseDelay(t *testing.T) {
a := assert.New(t)

delay, err := ParseDelay(10)
a.Equal(10.0, delay, "delay should be 10")
a.NoError(err, "there should be no error")

delay, err = ParseDelay(5.5)
a.Equal(5.5, delay, "delay should be 5.5")
a.NoError(err, "there should be no error")

delay, err = ParseDelay(-10)
a.Equal(0.0, delay, "delay should be 0 when negative is given")
a.Error(err, "there should be an error when negative is given")
}

func TestParseSplitBy(t *testing.T) {
a := assert.New(t)

Expand Down

0 comments on commit 5562e55

Please sign in to comment.