Files
quic-go/qlog/qlog_dir.go
Benedikt Spies 2cd9ed38f1 qlog: add a default tracer that writes to QLOGDIR (#4233)
* add qlog default tracer which writes to QLOGDIR

* gofumpt

* add qlog default tracer which writes to QLOGDIR

* fix flaky tests

* Update README.md

Co-authored-by: Marten Seemann <martenseemann@gmail.com>

* Update README.md

Co-authored-by: Marten Seemann <martenseemann@gmail.com>

* Update README.md

Co-authored-by: Marten Seemann <martenseemann@gmail.com>

* Update README.md

Co-authored-by: Marten Seemann <martenseemann@gmail.com>

---------

Co-authored-by: Marten Seemann <martenseemann@gmail.com>
2024-01-12 03:11:53 -08:00

50 lines
1.5 KiB
Go

package qlog
import (
"bufio"
"context"
"fmt"
"log"
"os"
"strings"
"github.com/quic-go/quic-go/internal/utils"
"github.com/quic-go/quic-go/logging"
)
// DefaultTracer creates a qlog file in the qlog directory specified by the QLOGDIR environment variable.
// File names are <odcid>_<perspective>.qlog.
// Returns nil if QLOGDIR is not set.
func DefaultTracer(_ context.Context, p logging.Perspective, connID logging.ConnectionID) *logging.ConnectionTracer {
var label string
switch p {
case logging.PerspectiveClient:
label = "client"
case logging.PerspectiveServer:
label = "server"
}
return qlogDirTracer(p, connID, label)
}
// qlogDirTracer creates a qlog file in the qlog directory specified by the QLOGDIR environment variable.
// File names are <odcid>_<label>.qlog.
// Returns nil if QLOGDIR is not set.
func qlogDirTracer(p logging.Perspective, connID logging.ConnectionID, label string) *logging.ConnectionTracer {
qlogDir := os.Getenv("QLOGDIR")
if qlogDir == "" {
return nil
}
if _, err := os.Stat(qlogDir); os.IsNotExist(err) {
if err := os.MkdirAll(qlogDir, 0o755); err != nil {
log.Fatalf("failed to create qlog dir %s: %v", qlogDir, err)
}
}
path := fmt.Sprintf("%s/%s_%s.qlog", strings.TrimRight(qlogDir, "/"), connID, label)
f, err := os.Create(path)
if err != nil {
log.Printf("Failed to create qlog file %s: %s", path, err.Error())
return nil
}
return NewConnectionTracer(utils.NewBufferedWriteCloser(bufio.NewWriter(f), f), p, connID)
}