-
Notifications
You must be signed in to change notification settings - Fork 10
/
logs.go
59 lines (50 loc) · 1.13 KB
/
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
52
53
54
55
56
57
58
59
package main
import (
"fmt"
"log"
"os"
)
func jLog(jctx *JCtx, msg string) {
if *logMux {
log.Print(fmt.Sprintf("[%s]:%s", jctx.config.Host, msg))
return
}
if jctx.config.Log.logger != nil {
jctx.config.Log.logger.Printf(msg)
}
}
func logStop(jctx *JCtx) {
if jctx.config.Log.out != nil {
if jctx.config.Log.out != os.Stdout {
jctx.config.Log.out.Close()
}
jctx.config.Log.out = nil
jctx.config.Log.logger = nil
}
}
func logInit(jctx *JCtx) {
if *logMux {
return
}
file := jctx.config.Log.File
var out *os.File
if *print {
out = os.Stdout
if file != "" {
log.Println("Both print and log options are used, ignoring log")
}
} else if file != "" {
var err error
out, err = os.OpenFile(file, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0600)
if err != nil {
log.Printf("Could not create log file(%s): %v\n", file, err)
}
}
if out != nil {
flags := 0
jctx.config.Log.logger = log.New(out, "", flags)
jctx.config.Log.out = out
log.Printf("logging in %s for %s:%d [periodic stats every %d seconds]\n",
jctx.config.Log.File, jctx.config.Host, jctx.config.Port, jctx.config.Log.PeriodicStats)
}
}