File: log.go

package info (click to toggle)
golang-golang-x-net 1%3A0.27.0-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental, sid, trixie
  • size: 8,636 kB
  • sloc: asm: 18; makefile: 12; sh: 7
file content (69 lines) | stat: -rw-r--r-- 1,418 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright 2023 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build go1.21

package quic

import (
	"fmt"
	"os"
	"strings"
)

var logPackets bool

// Parse GODEBUG settings.
//
// GODEBUG=quiclogpackets=1 -- log every packet sent and received.
func init() {
	s := os.Getenv("GODEBUG")
	for len(s) > 0 {
		var opt string
		opt, s, _ = strings.Cut(s, ",")
		switch opt {
		case "quiclogpackets=1":
			logPackets = true
		}
	}
}

func logInboundLongPacket(c *Conn, p longPacket) {
	if !logPackets {
		return
	}
	prefix := c.String()
	fmt.Printf("%v recv %v %v\n", prefix, p.ptype, p.num)
	logFrames(prefix+"   <- ", p.payload)
}

func logInboundShortPacket(c *Conn, p shortPacket) {
	if !logPackets {
		return
	}
	prefix := c.String()
	fmt.Printf("%v recv 1-RTT %v\n", prefix, p.num)
	logFrames(prefix+"   <- ", p.payload)
}

func logSentPacket(c *Conn, ptype packetType, pnum packetNumber, src, dst, payload []byte) {
	if !logPackets || len(payload) == 0 {
		return
	}
	prefix := c.String()
	fmt.Printf("%v send %v %v\n", prefix, ptype, pnum)
	logFrames(prefix+"   -> ", payload)
}

func logFrames(prefix string, payload []byte) {
	for len(payload) > 0 {
		f, n := parseDebugFrame(payload)
		if n < 0 {
			fmt.Printf("%vBAD DATA\n", prefix)
			break
		}
		payload = payload[n:]
		fmt.Printf("%v%v\n", prefix, f)
	}
}