-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathoutput.go
71 lines (56 loc) · 1.39 KB
/
output.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
package main
import (
"encoding/json"
"log"
"os"
"sync"
pstore "github.com/libp2p/go-libp2p-peerstore"
ma "github.com/multiformats/go-multiaddr"
)
type CrawlLog struct {
f *os.File
w *json.Encoder
mx sync.Mutex
}
type CrawlRecord struct {
ID string `json:"id"`
ConAddr string `json:"conaddr,omitempty"`
Addrs []string `json:"addrs"`
Status string `json:"status"`
Error string `json:"error,omitempty"`
}
func NewCrawlLog(path string) (*CrawlLog, error) {
f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return nil, err
}
enc := json.NewEncoder(f)
return &CrawlLog{f: f, w: enc}, nil
}
func (o *CrawlLog) LogConnect(ca ma.Multiaddr, pi pstore.PeerInfo) {
o.mx.Lock()
defer o.mx.Unlock()
err := o.w.Encode(peerInfoToCrawlRecord(pi, ca, "OK", ""))
if err != nil {
log.Fatal(err)
}
}
func (o *CrawlLog) LogError(pi pstore.PeerInfo, e error) {
o.mx.Lock()
defer o.mx.Unlock()
err := o.w.Encode(peerInfoToCrawlRecord(pi, nil, "ERROR", e.Error()))
if err != nil {
log.Fatal(err)
}
}
func peerInfoToCrawlRecord(pi pstore.PeerInfo, ca ma.Multiaddr, status, e string) CrawlRecord {
addrs := make([]string, len(pi.Addrs))
for i, a := range pi.Addrs {
addrs[i] = a.String()
}
var cas string
if ca != nil {
cas = ca.String()
}
return CrawlRecord{ID: pi.ID.Pretty(), Addrs: addrs, Status: status, Error: e, ConAddr: cas}
}