github.com/MerlinKodo/quic-go@v0.39.2/interop/utils/logging.go (about)

     1  package utils
     2  
     3  import (
     4  	"bufio"
     5  	"context"
     6  	"fmt"
     7  	"io"
     8  	"log"
     9  	"os"
    10  	"strings"
    11  
    12  	"github.com/MerlinKodo/quic-go"
    13  	"github.com/MerlinKodo/quic-go/internal/utils"
    14  	"github.com/MerlinKodo/quic-go/logging"
    15  	"github.com/MerlinKodo/quic-go/qlog"
    16  )
    17  
    18  // GetSSLKeyLog creates a file for the TLS key log
    19  func GetSSLKeyLog() (io.WriteCloser, error) {
    20  	filename := os.Getenv("SSLKEYLOGFILE")
    21  	if len(filename) == 0 {
    22  		return nil, nil
    23  	}
    24  	f, err := os.Create(filename)
    25  	if err != nil {
    26  		return nil, err
    27  	}
    28  	return f, nil
    29  }
    30  
    31  // NewQLOGConnectionTracer create a qlog file in QLOGDIR
    32  func NewQLOGConnectionTracer(_ context.Context, p logging.Perspective, connID quic.ConnectionID) *logging.ConnectionTracer {
    33  	qlogDir := os.Getenv("QLOGDIR")
    34  	if len(qlogDir) == 0 {
    35  		return nil
    36  	}
    37  	if _, err := os.Stat(qlogDir); os.IsNotExist(err) {
    38  		if err := os.MkdirAll(qlogDir, 0o666); err != nil {
    39  			log.Fatalf("failed to create qlog dir %s: %v", qlogDir, err)
    40  		}
    41  	}
    42  	path := fmt.Sprintf("%s/%x.qlog", strings.TrimRight(qlogDir, "/"), connID)
    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  	log.Printf("Created qlog file: %s\n", path)
    49  	return qlog.NewConnectionTracer(utils.NewBufferedWriteCloser(bufio.NewWriter(f), f), p, connID)
    50  }