-
Notifications
You must be signed in to change notification settings - Fork 0
/
default_log.go
57 lines (50 loc) · 1.51 KB
/
default_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
package bufferlog
import (
"log"
"os"
"os/signal"
"strconv"
"syscall"
"time"
)
// Buffer is just for debug, you'd better new Buflog by yourself to control Flush on exiting by the exit channel
var Buffer *BufLog
func init() {
exit := make(chan struct{})
flushInterval := time.Millisecond * 100
Buffer = newBufferLog(1<<10, flushInterval, os.Stdout)
Buffer.exit = exit
go func() {
if err := Buffer.flushIntervally(); err != nil {
print(err.Error())
}
}()
go func() {
sigChan := make(chan os.Signal, 2)
signal.Notify(sigChan, syscall.SIGHUP, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGTERM)
oneSig := <-sigChan
close(exit)
time.Sleep(flushInterval) //make sure Buffer has exited, or invoke Close() directly
Buffer.Flush()
log.Printf("Buffer: receive exit signal %v\n", oneSig)
}()
}
func BufferDemo() {
bsWrite := []byte("abcd\n")
if n, err := Buffer.Write(bsWrite); err != nil {
errStr := "wrote " + strconv.Itoa(n) + " bytes want " + strconv.Itoa(len(bsWrite)) + " bytes, err:" + err.Error()
print(errStr)
}
sigChan := make(chan os.Signal, 2)
signal.Notify(sigChan, syscall.SIGHUP, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGTERM)
go func() {
log.Printf("will kill self in 1 seconds\n")
time.Sleep(time.Second * 1)
if err := syscall.Kill(syscall.Getpid(), syscall.SIGTERM); err != nil {
print(err.Error())
}
}()
gotSignal := <-sigChan
log.Printf("main: receive exit signal got %v\n", gotSignal)
time.Sleep(time.Second * 2) //make sure Buffer has exited, or invoke Close() directly
}