github.com/sagernet/quic-go@v0.43.1-beta.1/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  	Debug                        func(name, msg string)
    11  	Close                        func()
    12  }
    13  
    14  // NewMultiplexedTracer creates a new tracer that multiplexes events to multiple tracers.
    15  func NewMultiplexedTracer(tracers ...*Tracer) *Tracer {
    16  	if len(tracers) == 0 {
    17  		return nil
    18  	}
    19  	if len(tracers) == 1 {
    20  		return tracers[0]
    21  	}
    22  	return &Tracer{
    23  		SentPacket: func(remote net.Addr, hdr *Header, size ByteCount, frames []Frame) {
    24  			for _, t := range tracers {
    25  				if t.SentPacket != nil {
    26  					t.SentPacket(remote, hdr, size, frames)
    27  				}
    28  			}
    29  		},
    30  		SentVersionNegotiationPacket: func(remote net.Addr, dest, src ArbitraryLenConnectionID, versions []VersionNumber) {
    31  			for _, t := range tracers {
    32  				if t.SentVersionNegotiationPacket != nil {
    33  					t.SentVersionNegotiationPacket(remote, dest, src, versions)
    34  				}
    35  			}
    36  		},
    37  		DroppedPacket: func(remote net.Addr, typ PacketType, size ByteCount, reason PacketDropReason) {
    38  			for _, t := range tracers {
    39  				if t.DroppedPacket != nil {
    40  					t.DroppedPacket(remote, typ, size, reason)
    41  				}
    42  			}
    43  		},
    44  		Debug: func(name, msg string) {
    45  			for _, t := range tracers {
    46  				if t.Debug != nil {
    47  					t.Debug(name, msg)
    48  				}
    49  			}
    50  		},
    51  		Close: func() {
    52  			for _, t := range tracers {
    53  				if t.Close != nil {
    54  					t.Close()
    55  				}
    56  			}
    57  		},
    58  	}
    59  }