-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlogs.go
51 lines (43 loc) · 934 Bytes
/
logs.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
package main
import (
"net"
"strings"
"sync"
"time"
)
const MaxLogs = 20
type Log struct {
Received string
From string
To string
Subject string
Filter string
Route string
Status string
Error string
}
type LogList struct {
sync.RWMutex
Logs []Log
}
func (ll *LogList) Add(origin net.IP, from string, to []string, subject string, filter string, route string, status string, error string) {
ll.Lock()
defer ll.Unlock()
l := Log{
Received: time.Now().Format("2006-01-02 15:04:05"),
From: from,
To: strings.Join(to, ", "),
Subject: subject,
Filter: filter,
Route: route,
Status: status,
Error: error,
}
// Expand the log list out to the max size.
if len(ll.Logs) < MaxLogs {
ll.Logs = append(ll.Logs, Log{})
}
// Shuffle the existing log entries down one slot and put the new one in the first slot.
copy(ll.Logs[1:], ll.Logs[0:])
ll.Logs[0] = l
}