github.com/TugasAkhir-QUIC/quic-go@v0.0.2-0.20240215011318-d20e25a9054c/qlog/qlog_dir.go (about) 1 package qlog 2 3 import ( 4 "bufio" 5 "context" 6 "fmt" 7 "log" 8 "os" 9 "strings" 10 11 "github.com/TugasAkhir-QUIC/quic-go/internal/utils" 12 "github.com/TugasAkhir-QUIC/quic-go/logging" 13 ) 14 15 // DefaultTracer creates a qlog file in the qlog directory specified by the QLOGDIR environment variable. 16 // File names are <odcid>_<perspective>.qlog. 17 // Returns nil if QLOGDIR is not set. 18 func DefaultTracer(_ context.Context, p logging.Perspective, connID logging.ConnectionID) *logging.ConnectionTracer { 19 var label string 20 switch p { 21 case logging.PerspectiveClient: 22 label = "client" 23 case logging.PerspectiveServer: 24 label = "server" 25 } 26 return qlogDirTracer(p, connID, label) 27 } 28 29 // qlogDirTracer creates a qlog file in the qlog directory specified by the QLOGDIR environment variable. 30 // File names are <odcid>_<label>.qlog. 31 // Returns nil if QLOGDIR is not set. 32 func qlogDirTracer(p logging.Perspective, connID logging.ConnectionID, label string) *logging.ConnectionTracer { 33 qlogDir := os.Getenv("QLOGDIR") 34 if qlogDir == "" { 35 return nil 36 } 37 if _, err := os.Stat(qlogDir); os.IsNotExist(err) { 38 if err := os.MkdirAll(qlogDir, 0o755); err != nil { 39 log.Fatalf("failed to create qlog dir %s: %v", qlogDir, err) 40 } 41 } 42 path := fmt.Sprintf("%s/%s_%s.qlog", strings.TrimRight(qlogDir, "/"), connID, label) 43 f, err := os.Create(path) 44 if err != nil { 45 log.Printf("Failed to create qlog file %s: %s", path, err.Error()) 46 return nil 47 } 48 return NewConnectionTracer(utils.NewBufferedWriteCloser(bufio.NewWriter(f), f), p, connID) 49 }