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

     1  package logging
     2  
     3  import "net"
     4  
     5  // A Tracer traces events.
     6  type Tracer struct {
     7  	SentPacket                   func(net.Addr, *Header, ByteCount, []Frame)
     8  	SentVersionNegotiationPacket func(_ net.Addr, dest, src ArbitraryLenConnectionID, _ []VersionNumber)
     9  	DroppedPacket                func(net.Addr, PacketType, ByteCount, PacketDropReason)
    10  }
    11  
    12  // NewMultiplexedTracer creates a new tracer that multiplexes events to multiple tracers.
    13  func NewMultiplexedTracer(tracers ...*Tracer) *Tracer {
    14  	if len(tracers) == 0 {
    15  		return nil
    16  	}
    17  	if len(tracers) == 1 {
    18  		return tracers[0]
    19  	}
    20  	return &Tracer{
    21  		SentPacket: func(remote net.Addr, hdr *Header, size ByteCount, frames []Frame) {
    22  			for _, t := range tracers {
    23  				if t.SentPacket != nil {
    24  					t.SentPacket(remote, hdr, size, frames)
    25  				}
    26  			}
    27  		},
    28  		SentVersionNegotiationPacket: func(remote net.Addr, dest, src ArbitraryLenConnectionID, versions []VersionNumber) {
    29  			for _, t := range tracers {
    30  				if t.SentVersionNegotiationPacket != nil {
    31  					t.SentVersionNegotiationPacket(remote, dest, src, versions)
    32  				}
    33  			}
    34  		},
    35  		DroppedPacket: func(remote net.Addr, typ PacketType, size ByteCount, reason PacketDropReason) {
    36  			for _, t := range tracers {
    37  				if t.DroppedPacket != nil {
    38  					t.DroppedPacket(remote, typ, size, reason)
    39  				}
    40  			}
    41  		},
    42  	}
    43  }