-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.go
58 lines (51 loc) · 1.15 KB
/
logger.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
package main
import (
"strings"
"time"
)
func curTime() time.Time {
currentTime := time.Now()
loc, err := time.LoadLocation("Asia/Nicosia")
_check(err)
return currentTime.In(loc)
}
func fits(logStr string, days int) bool {
splitted := strings.Split(logStr, " ")
dateStr := strings.Join(splitted[:4], " ")
layout := "02 Jan 2006 15:04"
t, err := time.Parse(layout, dateStr)
_check(err)
diff := time.Now().Sub(t)
dayDiff := diff.Hours() / 24
return dayDiff < float64(days)
}
type Logger struct {
logDir string
}
func (l Logger) log(file string, text string) {
path := l.logDir + file
createFile(path)
lines, err := readLines(path)
_check(err)
currentTime := curTime()
lines = append(lines, currentTime.Format("02 Jan 2006 15:04")+" "+text)
err = writeLines(lines, path)
_check(err)
}
func (l Logger) query(file string, days int) ([]string, error) {
path := l.logDir + file
lines, err := readLines(path)
if err != nil {
return nil, err
}
size := 0
var res []string
for i := len(lines) - 1; i >= 0; i-- {
str := lines[i]
size += len([]rune(str)) + 4
if fits(str, days) && size < 4096 {
res = append(res, str)
}
}
return res, nil
}