-
Notifications
You must be signed in to change notification settings - Fork 16
/
log.go
87 lines (74 loc) · 1.97 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
80
81
82
83
84
85
86
87
// Lightly modified from Mint
package minq
import (
"fmt"
"log"
"os"
"strings"
)
// We use this environment variable to control logging. It should be a
// comma-separated list of log tags (see below) or "*" to enable all logging.
const logConfigVar = "MINQ_LOG"
// Pre-defined log types
const (
logTypeAead = "aead"
logTypeCodec = "codec"
logTypeConnBuffer = "connbuffer"
logTypeConnection = "connection"
logTypeAck = "ack"
logTypeFrame = "frame"
logTypeHandshake = "handshake"
logTypeTls = "tls"
logTypeTrace = "trace"
logTypeServer = "server"
logTypeUdp = "udp"
logTypeStream = "stream"
logTypeFlowControl = "flow"
logTypePacket = "packet" // Just send notes on which packets are sent and received
logTypeCongestion = "congestion"
)
var (
logFunction = log.Printf
logAll = false
logSettings = map[string]bool{}
)
func init() {
parseLogEnv(os.Environ())
}
func parseLogEnv(env []string) {
for _, stmt := range env {
if strings.HasPrefix(stmt, logConfigVar+"=") {
val := stmt[len(logConfigVar)+1:]
if val == "*" {
logAll = true
} else {
for _, t := range strings.Split(val, ",") {
logSettings[t] = true
}
}
}
}
}
func logf(tag string, format string, args ...interface{}) {
if logAll || logSettings[tag] {
fullFormat := fmt.Sprintf("[%s] %s", tag, format)
logFunction(fullFormat, args...)
}
}
type loggingFunction func(string, string, ...interface{})
func SetLogOutput(f func(string, ...interface{})) {
logFunction = f
}
func newConnectionLogger(c *Connection) loggingFunction {
return func(tag string, format string, args ...interface{}) {
if logAll || logSettings[tag] {
logf(tag, c.String()+": "+format, args...)
}
}
}
func newStreamLogger(id uint64, dir string, f loggingFunction) loggingFunction {
extra := fmt.Sprintf("%s stream %d: ", dir, id)
return func(tag string, format string, args ...interface{}) {
f(tag, extra+format, args...)
}
}