Skip to content

Commit

Permalink
fix: bug that timestamp is not updated when using -d option
Browse files Browse the repository at this point in the history
  • Loading branch information
mingrammer committed May 6, 2020
1 parent ad24808 commit c6eea53
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
34 changes: 18 additions & 16 deletions flog.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,20 @@ import (

// Generate generates the logs with given options
func Generate(option *Option) error {
splitCount := 1
created := time.Now()
var (
splitCount = 1
created = time.Now()

interval time.Duration
delay time.Duration
)

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

logFileName := option.Output
Expand All @@ -29,21 +37,17 @@ func Generate(option *Option) error {

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

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

Expand All @@ -56,15 +60,13 @@ func Generate(option *Option) error {

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

Expand All @@ -78,7 +80,7 @@ func Generate(option *Option) error {

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

Expand Down
2 changes: 1 addition & 1 deletion option.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ 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 creation time interval for each log (in seconds). It does not actually sleep.
-s, --sleep numeric fix 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.
Expand Down

0 comments on commit c6eea53

Please sign in to comment.