github.com/TugasAkhir-QUIC/quic-go@v0.0.2-0.20240215011318-d20e25a9054c/integrationtests/tools/qlog.go (about)

     1  package tools
     2  
     3  import (
     4  	"bufio"
     5  	"context"
     6  	"fmt"
     7  	"io"
     8  	"log"
     9  	"os"
    10  
    11  	"github.com/TugasAkhir-QUIC/quic-go"
    12  	"github.com/TugasAkhir-QUIC/quic-go/internal/utils"
    13  	"github.com/TugasAkhir-QUIC/quic-go/logging"
    14  	"github.com/TugasAkhir-QUIC/quic-go/qlog"
    15  )
    16  
    17  func NewQlogger(logger io.Writer) func(context.Context, logging.Perspective, quic.ConnectionID) *logging.ConnectionTracer {
    18  	return func(_ context.Context, p logging.Perspective, connID quic.ConnectionID) *logging.ConnectionTracer {
    19  		role := "server"
    20  		if p == logging.PerspectiveClient {
    21  			role = "client"
    22  		}
    23  		filename := fmt.Sprintf("log_%s_%s.qlog", connID, role)
    24  		fmt.Fprintf(logger, "Creating %s.\n", filename)
    25  		f, err := os.Create(filename)
    26  		if err != nil {
    27  			log.Fatalf("failed to create qlog file: %s", err)
    28  			return nil
    29  		}
    30  		bw := bufio.NewWriter(f)
    31  		return qlog.NewConnectionTracer(utils.NewBufferedWriteCloser(bw, f), p, connID)
    32  	}
    33  }