-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
102 lines (93 loc) · 2.71 KB
/
main.go
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package main
import (
"flag"
"fmt"
"os"
"regexp"
"time"
)
type filePart struct {
fileHandler *os.File
from int64
to int64
}
type timeBorders struct {
from time.Time
to time.Time
}
type timeParams struct {
regex *regexp.Regexp
borders timeBorders
junkLines int64
}
func assertFlagIsPositiveInt(value int64) {
if value < 0 {
flag.Usage()
}
}
func getTimeStampRegex(timestampCustomRegex *string, timestampType *string) *regexp.Regexp {
var timestampRegexString string
if *timestampCustomRegex != "" {
timestampRegexString = *timestampCustomRegex
} else {
lookupRegexString, ok := TimestampTypes[*timestampType]
if ok {
timestampRegexString = lookupRegexString
} else {
fmt.Fprintf(os.Stderr, "Unkown type %s, possible values are: ", *timestampType)
for k := range TimestampTypes {
fmt.Fprint(os.Stderr, k, " ")
}
fmt.Fprintln(os.Stderr, "")
os.Exit(1)
}
}
return regexp.MustCompile(timestampRegexString)
}
func getTimeBorders(fromTime, deltaSeconds *int64) timeBorders {
var timeRightBorder time.Time
if *fromTime != 0 {
timeRightBorder = time.Unix(*fromTime, 0)
} else {
timeRightBorder = time.Now().Round(time.Second)
}
timeLeftBorder := timeRightBorder.Add(-time.Duration(*deltaSeconds) * time.Second)
return timeBorders{timeLeftBorder, timeRightBorder}
}
func main() {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s [options] <log files>...\n", os.Args[0])
flag.PrintDefaults()
os.Exit(1)
}
deltaSeconds := flag.Int64("n", 300, "Delta time count from now() to look at from the end of the <log file>")
fromTime := flag.Int64("b", 0, "From which datetime start to operate seconds of the <log file>")
timestampType := flag.String("t", "common", "Timestamp type")
timestampCustomRegex := flag.String("r", "", "Regexp to pick timestamp from string ($1 must select timestamp)")
junkLines := flag.Int64("j", 500, "Max number of junk lines to read")
utcLogTime := flag.Bool("utc", false, "Enable UTC timezone for time parse")
flag.Parse()
logFiles := flag.Args()
if len(logFiles) == 0 {
flag.Usage()
}
assertFlagIsPositiveInt(*deltaSeconds)
assertFlagIsPositiveInt(*fromTime)
assertFlagIsPositiveInt(*junkLines)
if *timestampType != "common" && *timestampCustomRegex != "" {
flag.Usage()
}
timestampRegex := getTimeStampRegex(timestampCustomRegex, timestampType)
timeBordersVal := getTimeBorders(fromTime, deltaSeconds)
if *utcLogTime {
utcLoc, _ := time.LoadLocation("UTC")
time.Local = utcLoc
}
partsChannel := make(chan filePart)
for _, logFile := range logFiles {
go searchFilePart(logFile, timeParams{timestampRegex, timeBordersVal, *junkLines}, partsChannel)
}
for i := 0; i < len(logFiles); i++ {
readFile(<-partsChannel)
}
}