forked from quic-go/quic-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logging.go
50 lines (46 loc) · 1.25 KB
/
logging.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
package utils
import (
"bufio"
"context"
"fmt"
"io"
"log"
"os"
"strings"
"github.com/k4ra5u/quic-go"
"github.com/k4ra5u/quic-go/internal/utils"
"github.com/k4ra5u/quic-go/logging"
"github.com/k4ra5u/quic-go/qlog"
)
// GetSSLKeyLog creates a file for the TLS key log
func GetSSLKeyLog() (io.WriteCloser, error) {
filename := os.Getenv("SSLKEYLOGFILE")
if len(filename) == 0 {
return nil, nil
}
f, err := os.Create(filename)
if err != nil {
return nil, err
}
return f, nil
}
// NewQLOGConnectionTracer create a qlog file in QLOGDIR
func NewQLOGConnectionTracer(_ context.Context, p logging.Perspective, connID quic.ConnectionID) *logging.ConnectionTracer {
qlogDir := os.Getenv("QLOGDIR")
if len(qlogDir) == 0 {
return nil
}
if _, err := os.Stat(qlogDir); os.IsNotExist(err) {
if err := os.MkdirAll(qlogDir, 0o666); err != nil {
log.Fatalf("failed to create qlog dir %s: %v", qlogDir, err)
}
}
path := fmt.Sprintf("%s/%s.qlog", strings.TrimRight(qlogDir, "/"), connID)
f, err := os.Create(path)
if err != nil {
log.Printf("Failed to create qlog file %s: %s", path, err.Error())
return nil
}
log.Printf("Created qlog file: %s\n", path)
return qlog.NewConnectionTracer(utils.NewBufferedWriteCloser(bufio.NewWriter(f), f), p, connID)
}