github.com/metacubex/quic-go@v0.44.1-0.20240520163451-20b689a59136/internal/wire/log.go (about)

     1  package wire
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/metacubex/quic-go/internal/protocol"
     8  	"github.com/metacubex/quic-go/internal/utils"
     9  )
    10  
    11  // LogFrame logs a frame, either sent or received
    12  func LogFrame(logger utils.Logger, frame Frame, sent bool) {
    13  	if !logger.Debug() {
    14  		return
    15  	}
    16  	dir := "<-"
    17  	if sent {
    18  		dir = "->"
    19  	}
    20  	switch f := frame.(type) {
    21  	case *CryptoFrame:
    22  		dataLen := protocol.ByteCount(len(f.Data))
    23  		logger.Debugf("\t%s &wire.CryptoFrame{Offset: %d, Data length: %d, Offset + Data length: %d}", dir, f.Offset, dataLen, f.Offset+dataLen)
    24  	case *StreamFrame:
    25  		logger.Debugf("\t%s &wire.StreamFrame{StreamID: %d, Fin: %t, Offset: %d, Data length: %d, Offset + Data length: %d}", dir, f.StreamID, f.Fin, f.Offset, f.DataLen(), f.Offset+f.DataLen())
    26  	case *ResetStreamFrame:
    27  		logger.Debugf("\t%s &wire.ResetStreamFrame{StreamID: %d, ErrorCode: %#x, FinalSize: %d}", dir, f.StreamID, f.ErrorCode, f.FinalSize)
    28  	case *AckFrame:
    29  		hasECN := f.ECT0 > 0 || f.ECT1 > 0 || f.ECNCE > 0
    30  		var ecn string
    31  		if hasECN {
    32  			ecn = fmt.Sprintf(", ECT0: %d, ECT1: %d, CE: %d", f.ECT0, f.ECT1, f.ECNCE)
    33  		}
    34  		if len(f.AckRanges) > 1 {
    35  			ackRanges := make([]string, len(f.AckRanges))
    36  			for i, r := range f.AckRanges {
    37  				ackRanges[i] = fmt.Sprintf("{Largest: %d, Smallest: %d}", r.Largest, r.Smallest)
    38  			}
    39  			logger.Debugf("\t%s &wire.AckFrame{LargestAcked: %d, LowestAcked: %d, AckRanges: {%s}, DelayTime: %s%s}", dir, f.LargestAcked(), f.LowestAcked(), strings.Join(ackRanges, ", "), f.DelayTime.String(), ecn)
    40  		} else {
    41  			logger.Debugf("\t%s &wire.AckFrame{LargestAcked: %d, LowestAcked: %d, DelayTime: %s%s}", dir, f.LargestAcked(), f.LowestAcked(), f.DelayTime.String(), ecn)
    42  		}
    43  	case *MaxDataFrame:
    44  		logger.Debugf("\t%s &wire.MaxDataFrame{MaximumData: %d}", dir, f.MaximumData)
    45  	case *MaxStreamDataFrame:
    46  		logger.Debugf("\t%s &wire.MaxStreamDataFrame{StreamID: %d, MaximumStreamData: %d}", dir, f.StreamID, f.MaximumStreamData)
    47  	case *DataBlockedFrame:
    48  		logger.Debugf("\t%s &wire.DataBlockedFrame{MaximumData: %d}", dir, f.MaximumData)
    49  	case *StreamDataBlockedFrame:
    50  		logger.Debugf("\t%s &wire.StreamDataBlockedFrame{StreamID: %d, MaximumStreamData: %d}", dir, f.StreamID, f.MaximumStreamData)
    51  	case *MaxStreamsFrame:
    52  		switch f.Type {
    53  		case protocol.StreamTypeUni:
    54  			logger.Debugf("\t%s &wire.MaxStreamsFrame{Type: uni, MaxStreamNum: %d}", dir, f.MaxStreamNum)
    55  		case protocol.StreamTypeBidi:
    56  			logger.Debugf("\t%s &wire.MaxStreamsFrame{Type: bidi, MaxStreamNum: %d}", dir, f.MaxStreamNum)
    57  		}
    58  	case *StreamsBlockedFrame:
    59  		switch f.Type {
    60  		case protocol.StreamTypeUni:
    61  			logger.Debugf("\t%s &wire.StreamsBlockedFrame{Type: uni, MaxStreams: %d}", dir, f.StreamLimit)
    62  		case protocol.StreamTypeBidi:
    63  			logger.Debugf("\t%s &wire.StreamsBlockedFrame{Type: bidi, MaxStreams: %d}", dir, f.StreamLimit)
    64  		}
    65  	case *NewConnectionIDFrame:
    66  		logger.Debugf("\t%s &wire.NewConnectionIDFrame{SequenceNumber: %d, RetirePriorTo: %d, ConnectionID: %s, StatelessResetToken: %#x}", dir, f.SequenceNumber, f.RetirePriorTo, f.ConnectionID, f.StatelessResetToken)
    67  	case *RetireConnectionIDFrame:
    68  		logger.Debugf("\t%s &wire.RetireConnectionIDFrame{SequenceNumber: %d}", dir, f.SequenceNumber)
    69  	case *NewTokenFrame:
    70  		logger.Debugf("\t%s &wire.NewTokenFrame{Token: %#x}", dir, f.Token)
    71  	default:
    72  		logger.Debugf("\t%s %#v", dir, frame)
    73  	}
    74  }