-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.go
More file actions
128 lines (117 loc) · 3.17 KB
/
Copy pathlogger.go
File metadata and controls
128 lines (117 loc) · 3.17 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package main
import (
"fmt"
"io"
"log"
"os"
"path/filepath"
"sync"
"time"
)
var (
errorLogger *log.Logger
errorLogPath string
errorLogOnce sync.Once
debugLogger *log.Logger
debugLogPath string
debugLogOnce sync.Once
// debugPacketDumpLen limits how many bytes of a packet payload are logged.
// A value of 0 dumps the entire payload.
debugPacketDumpLen = 256
)
func setupLogging(debug bool) {
logDir := "logs"
if !isWASM {
if err := os.MkdirAll(logDir, 0755); err != nil {
log.Printf("could not create log directory: %v", err)
}
}
ts := time.Now().Format("20060102-150405")
errorLogPath = filepath.Join(logDir, fmt.Sprintf("error-%s.log", ts))
errorLogOnce = sync.Once{}
errorLogger = log.New(os.Stdout, "", log.LstdFlags)
log.SetOutput(errorLogger.Writer())
setDebugLogging(debug)
}
func logError(format string, v ...interface{}) {
if errorLogger != nil {
errorLogOnce.Do(func() {
if isWASM {
// No file backend in WASM; keep stdout only.
return
}
if f, err := os.Create(errorLogPath); err == nil {
errorLogger.SetOutput(io.MultiWriter(os.Stdout, f))
log.SetOutput(errorLogger.Writer())
}
})
errorLogger.Printf(format, v...)
}
if !silent {
consoleMessage(fmt.Sprintf(format, v...))
}
}
func logDebug(format string, v ...interface{}) {
if debugLogger != nil {
debugLogOnce.Do(func() {
if isWASM {
return
}
if f, err := os.Create(debugLogPath); err == nil {
debugLogger.SetOutput(io.MultiWriter(os.Stdout, f))
}
})
debugLogger.Printf(format, v...)
}
}
func logWarn(format string, v ...interface{}) {
msg := fmt.Sprintf(format, v...)
if errorLogger != nil {
errorLogOnce.Do(func() {
if isWASM {
return
}
if f, err := os.Create(errorLogPath); err == nil {
errorLogger.SetOutput(io.MultiWriter(os.Stdout, f))
log.SetOutput(errorLogger.Writer())
}
})
errorLogger.Printf("warning: %s", msg)
}
if !silent {
consoleMessage(fmt.Sprintf("warning: %s", msg))
}
}
func logDebugPacket(prefix string, data []byte) {
if debugLogger == nil {
return
}
debugLogOnce.Do(func() {
if isWASM {
return
}
if f, err := os.Create(debugLogPath); err == nil {
debugLogger.SetOutput(io.MultiWriter(os.Stdout, f))
}
})
n := len(data)
dump := data
if debugPacketDumpLen > 0 && n > debugPacketDumpLen {
dump = data[:debugPacketDumpLen]
}
debugLogger.Printf("%s len=%d payload=% x", prefix, n, dump)
}
func setDebugLogging(enabled bool) {
if enabled {
logDir := "logs"
if err := os.MkdirAll(logDir, 0755); err != nil {
log.Printf("could not create log directory: %v", err)
}
ts := time.Now().Format("20060102-150405")
debugLogPath = filepath.Join(logDir, fmt.Sprintf("debug-%s.log", ts))
debugLogOnce = sync.Once{}
debugLogger = log.New(os.Stdout, "", log.LstdFlags)
} else {
debugLogger = nil
}
}