github.com/daeuniverse/quic-go@v0.0.0-20240413031024-943f218e0810/qlog/tracer.go (about)

     1  package qlog
     2  
     3  import (
     4  	"io"
     5  	"net"
     6  	"time"
     7  
     8  	"github.com/daeuniverse/quic-go/internal/protocol"
     9  	"github.com/daeuniverse/quic-go/logging"
    10  )
    11  
    12  func NewTracer(w io.WriteCloser) *logging.Tracer {
    13  	tr := &trace{
    14  		VantagePoint: vantagePoint{Type: "transport"},
    15  		CommonFields: commonFields{ReferenceTime: time.Now()},
    16  	}
    17  	wr := *newWriter(w, tr)
    18  	go wr.Run()
    19  	return &logging.Tracer{
    20  		SentPacket: func(_ net.Addr, hdr *logging.Header, size logging.ByteCount, frames []logging.Frame) {
    21  			fs := make([]frame, 0, len(frames))
    22  			for _, f := range frames {
    23  				fs = append(fs, frame{Frame: f})
    24  			}
    25  			wr.RecordEvent(time.Now(), &eventPacketSent{
    26  				Header: transformHeader(hdr),
    27  				Length: size,
    28  				Frames: fs,
    29  			})
    30  		},
    31  		SentVersionNegotiationPacket: func(_ net.Addr, dest, src logging.ArbitraryLenConnectionID, versions []logging.VersionNumber) {
    32  			ver := make([]versionNumber, len(versions))
    33  			for i, v := range versions {
    34  				ver[i] = versionNumber(v)
    35  			}
    36  			wr.RecordEvent(time.Now(), &eventVersionNegotiationSent{
    37  				Header: packetHeaderVersionNegotiation{
    38  					SrcConnectionID:  src,
    39  					DestConnectionID: dest,
    40  				},
    41  				SupportedVersions: ver,
    42  			})
    43  		},
    44  		DroppedPacket: func(addr net.Addr, p logging.PacketType, count logging.ByteCount, reason logging.PacketDropReason) {
    45  			wr.RecordEvent(time.Now(), eventPacketDropped{
    46  				PacketType:   p,
    47  				PacketNumber: protocol.InvalidPacketNumber,
    48  				PacketSize:   count,
    49  				Trigger:      packetDropReason(reason),
    50  			})
    51  		},
    52  		Debug: func(name, msg string) {
    53  			wr.RecordEvent(time.Now(), &eventGeneric{
    54  				name: name,
    55  				msg:  msg,
    56  			})
    57  		},
    58  		Close: func() { wr.Close() },
    59  	}
    60  }