golang.org/x/net@v0.25.1-0.20240516223405-c87a5b62e243/quic/log.go (about)

     1  // Copyright 2023 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build go1.21
     6  
     7  package quic
     8  
     9  import (
    10  	"fmt"
    11  	"os"
    12  	"strings"
    13  )
    14  
    15  var logPackets bool
    16  
    17  // Parse GODEBUG settings.
    18  //
    19  // GODEBUG=quiclogpackets=1 -- log every packet sent and received.
    20  func init() {
    21  	s := os.Getenv("GODEBUG")
    22  	for len(s) > 0 {
    23  		var opt string
    24  		opt, s, _ = strings.Cut(s, ",")
    25  		switch opt {
    26  		case "quiclogpackets=1":
    27  			logPackets = true
    28  		}
    29  	}
    30  }
    31  
    32  func logInboundLongPacket(c *Conn, p longPacket) {
    33  	if !logPackets {
    34  		return
    35  	}
    36  	prefix := c.String()
    37  	fmt.Printf("%v recv %v %v\n", prefix, p.ptype, p.num)
    38  	logFrames(prefix+"   <- ", p.payload)
    39  }
    40  
    41  func logInboundShortPacket(c *Conn, p shortPacket) {
    42  	if !logPackets {
    43  		return
    44  	}
    45  	prefix := c.String()
    46  	fmt.Printf("%v recv 1-RTT %v\n", prefix, p.num)
    47  	logFrames(prefix+"   <- ", p.payload)
    48  }
    49  
    50  func logSentPacket(c *Conn, ptype packetType, pnum packetNumber, src, dst, payload []byte) {
    51  	if !logPackets || len(payload) == 0 {
    52  		return
    53  	}
    54  	prefix := c.String()
    55  	fmt.Printf("%v send %v %v\n", prefix, ptype, pnum)
    56  	logFrames(prefix+"   -> ", payload)
    57  }
    58  
    59  func logFrames(prefix string, payload []byte) {
    60  	for len(payload) > 0 {
    61  		f, n := parseDebugFrame(payload)
    62  		if n < 0 {
    63  			fmt.Printf("%vBAD DATA\n", prefix)
    64  			break
    65  		}
    66  		payload = payload[n:]
    67  		fmt.Printf("%v%v\n", prefix, f)
    68  	}
    69  }