github.com/apernet/quic-go@v0.43.1-0.20240515053213-5e9e635fd9f0/integrationtests/tools/qlog.go (about)

     1  package tools
     2  
     3  import (
     4  	"bufio"
     5  	"context"
     6  	"fmt"
     7  	"io"
     8  	"log"
     9  	"os"
    10  	"time"
    11  
    12  	"github.com/apernet/quic-go"
    13  	"github.com/apernet/quic-go/internal/utils"
    14  	"github.com/apernet/quic-go/logging"
    15  	"github.com/apernet/quic-go/qlog"
    16  )
    17  
    18  func QlogTracer(logger io.Writer) *logging.Tracer {
    19  	filename := fmt.Sprintf("log_%s_transport.qlog", time.Now().Format("2006-01-02T15:04:05"))
    20  	fmt.Fprintf(logger, "Creating %s.\n", filename)
    21  	f, err := os.Create(filename)
    22  	if err != nil {
    23  		log.Fatalf("failed to create qlog file: %s", err)
    24  		return nil
    25  	}
    26  	bw := bufio.NewWriter(f)
    27  	return qlog.NewTracer(utils.NewBufferedWriteCloser(bw, f))
    28  }
    29  
    30  func NewQlogConnectionTracer(logger io.Writer) func(context.Context, logging.Perspective, quic.ConnectionID) *logging.ConnectionTracer {
    31  	return func(_ context.Context, p logging.Perspective, connID quic.ConnectionID) *logging.ConnectionTracer {
    32  		filename := fmt.Sprintf("log_%s_%s.qlog", connID, p.String())
    33  		fmt.Fprintf(logger, "Creating %s.\n", filename)
    34  		f, err := os.Create(filename)
    35  		if err != nil {
    36  			log.Fatalf("failed to create qlog file: %s", err)
    37  			return nil
    38  		}
    39  		bw := bufio.NewWriter(f)
    40  		return qlog.NewConnectionTracer(utils.NewBufferedWriteCloser(bw, f), p, connID)
    41  	}
    42  }