-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlog.go
79 lines (66 loc) · 1.55 KB
/
log.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
package main
import (
"fmt"
"os"
"path/filepath"
"time"
"github.com/aarzilli/yacco/buf"
"github.com/aarzilli/yacco/lsp"
"github.com/aarzilli/yacco/util"
)
/*
https://bitbucket.org/rsc/plan9port/commits/9e531f5eb3ab93ad9a208941f0cf1fab49d3cbf5
*/
var LogChans = map[string]chan string{}
var History = []HistoryEntry{}
type HistoryEntry struct {
When time.Time
Cmd string
Dir string
}
type LogOperation string
const (
LOP_NEW = LogOperation("new")
LOP_ZEROX = LogOperation("zerox")
LOP_GET = LogOperation("get")
LOP_PUT = LogOperation("put")
LOP_DEL = LogOperation("del")
)
func Log(wid int, op LogOperation, buf *buf.Buffer) {
s := fmt.Sprintf("%d %s %s\n", wid, op, filepath.Join(buf.Dir, buf.Name))
d := 1 * time.Second
t := time.NewTimer(d)
for k, ch := range LogChans {
t.Reset(d)
select {
case ch <- s:
// ok
case <-t.C:
close(ch)
delete(LogChans, k)
break
}
}
t.Stop()
if op == LOP_PUT {
srv, lspb := lsp.BufferToLsp(Wnd.tagbuf.Dir, buf, util.Sel{0, 0}, false, Warn, defaultLookForLsp)
if srv != nil {
srv.Changed(lspb)
}
}
}
func LogExec(cmd, dir string) {
History = append(History, HistoryEntry{time.Now(), cmd, dir})
}
func HistoryWrite() {
fh, err := os.OpenFile(os.ExpandEnv("$HOME/longhistory"), os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
fmt.Fprintf(os.Stderr, "Could not save history: %v\n", err)
return
}
defer fh.Close()
pid := os.Getpid()
for _, h := range History {
fmt.Fprintf(fh, "%d %s ### %s %s\n", pid, h.Cmd, h.When.Format("20060102 15:04"), h.Dir)
}
}