-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsort.go
48 lines (37 loc) · 1.04 KB
/
sort.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
package main
import (
"bufio"
"log"
"os"
"sort"
)
// LogRecords stuct to sort all records
type LogRecords []*LineLog
// RawRecords is full list of records
var rawRecords LogRecords
// InitRecords start the records
func InitRecords() {
rawRecords = make([]*LineLog, 0)
}
// RecordAdd include record in big slice
func RecordAdd(line *LineLog) {
rawRecords = append(rawRecords, line)
}
func (a LogRecords) Len() int { return len(a) }
func (a LogRecords) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a LogRecords) Less(i, j int) bool { return a[j].Time.After(a[i].Time) }
// saveSortedLog order by date
func saveSortedLog(save string) {
file, e := os.OpenFile(save, os.O_CREATE|os.O_TRUNC|os.O_WRONLY|os.O_APPEND, 0660)
if e != nil {
log.Panicln("Coudn't open file for writeing", save)
}
defer log.Printf("Sorted log file saved in %s", file.Name())
defer file.Close()
ob := bufio.NewWriter(file)
defer ob.Flush()
sort.Sort(rawRecords)
for _, r := range rawRecords {
ob.Write(r.Filelog.Pointer.GetLine(r.Seek, r.Len))
}
}