-
Notifications
You must be signed in to change notification settings - Fork 6
/
log.go
82 lines (70 loc) · 1.89 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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
package dtls
import (
"fmt"
"log"
"time"
)
const (
LogLevelError string = "error"
LogLevelWarn string = "warn"
LogLevelInfo string = "info"
LogLevelDebug string = "debug"
)
type LogFunc func(ts time.Time, level string, peer *Peer, err error, msg string)
var logFunc LogFunc = defaultLogFunc
var logLevel int = 0
func SetLogFunc(lf LogFunc) {
logFunc = lf
}
func SetLogLevel(level string) {
switch level {
case LogLevelError:
logLevel = 1
case LogLevelWarn:
logLevel = 2
case LogLevelInfo:
logLevel = 3
case LogLevelDebug:
logLevel = 4
default:
logLevel = 0
}
}
func defaultLogFunc(ts time.Time, level string, peer *Peer, err error, msg string) {
if err != nil {
log.Printf(" [" + level + "] [" + peer.RemoteAddr() + "] " + msg + "(err: " + err.Error() + ")")
} else {
log.Printf(" [" + level + "] [" + peer.RemoteAddr() + "] " + msg)
}
}
func logError(peer *Peer, rec *record, err error, f string, args ...interface{}) {
if logLevel < 1 {
return
}
logFunc(time.Now(), LogLevelError, peer, err, fmt.Sprintf(f, args...))
}
func logWarn(peer *Peer, rec *record, err error, f string, args ...interface{}) {
if logLevel < 2 {
return
}
logFunc(time.Now(), LogLevelWarn, peer, err, fmt.Sprintf(f, args...))
}
func logInfo(peer *Peer, rec *record, f string, args ...interface{}) {
if logLevel < 3 {
return
}
logFunc(time.Now(), LogLevelInfo, peer, nil, fmt.Sprintf(f, args...))
}
func logDebug(peer *Peer, rec *record, f string, args ...interface{}) {
if logLevel < 4 {
return
}
prefix := "dtls[-][-]: "
if rec != nil {
prefix = fmt.Sprintf("dtls[%d][%d]: ", rec.Epoch, rec.Sequence)
}
logFunc(time.Now(), LogLevelDebug, peer, nil, fmt.Sprintf(prefix+f, args...))
}