-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgenerator.go
More file actions
76 lines (67 loc) · 1.41 KB
/
Copy pathgenerator.go
File metadata and controls
76 lines (67 loc) · 1.41 KB
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
66
67
68
69
70
71
72
73
74
75
76
package main
import (
"bufio"
"fmt"
"os"
"flag"
"time"
"strconv"
)
func openFile(timestamp string) os.File {
file, err := os.OpenFile(
timestamp + ".log",
os.O_CREATE|os.O_RDWR|os.O_APPEND,
os.FileMode(0644),
)
if err != nil {
fmt.Println(err)
os.Exit(0)
}
return *file
}
func generateTimeStamp() string {
currentTime := time.Now()
return strconv.Itoa(currentTime.Year()) + AppendCharacter(int(currentTime.Month())) + AppendCharacter(currentTime.Day())
}
func main() {
timeStamp := generateTimeStamp()
file := openFile(timeStamp)
defer file.Close()
w := bufio.NewWriter(&file)
mode := flag.String("mode", "", "realtime or onetime")
count := flag.Int("count", 0, "log line count")
flag.Parse()
if *mode == "onetime" {
if *count == 0 {
fmt.Println("count is not defined")
os.Exit(0)
}
for i:=0; i < *count; i++ {
w.WriteString(GetLogLine() + "\n")
}
w.Flush()
} else if *mode == "realtime" {
var logCount int
if *count < 10 {
logCount = 10
} else {
logCount = *count
}
for {
for i:=0; i < logCount; i++ {
w.WriteString(GetLogLine() + "\n")
}
w.Flush()
time.Sleep(1000 * time.Millisecond)
if generateTimeStamp() != timeStamp {
timeStamp = generateTimeStamp()
file.Close()
file = openFile(timeStamp)
w = bufio.NewWriter(&file)
}
}
} else {
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
flag.PrintDefaults()
}
}