File: debug.go

package info (click to toggle)
golang-github-mdlayher-netlink 1.7.2-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, sid, trixie
  • size: 368 kB
  • sloc: makefile: 5
file content (69 lines) | stat: -rw-r--r-- 1,312 bytes parent folder | download | duplicates (3)
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
package netlink

import (
	"fmt"
	"log"
	"os"
	"strconv"
	"strings"
)

// Arguments used to create a debugger.
var debugArgs []string

func init() {
	// Is netlink debugging enabled?
	s := os.Getenv("NLDEBUG")
	if s == "" {
		return
	}

	debugArgs = strings.Split(s, ",")
}

// A debugger is used to provide debugging information about a netlink connection.
type debugger struct {
	Log   *log.Logger
	Level int
}

// newDebugger creates a debugger by parsing key=value arguments.
func newDebugger(args []string) *debugger {
	d := &debugger{
		Log:   log.New(os.Stderr, "nl: ", 0),
		Level: 1,
	}

	for _, a := range args {
		kv := strings.Split(a, "=")
		if len(kv) != 2 {
			// Ignore malformed pairs and assume callers wants defaults.
			continue
		}

		switch kv[0] {
		// Select the log level for the debugger.
		case "level":
			level, err := strconv.Atoi(kv[1])
			if err != nil {
				panicf("netlink: invalid NLDEBUG level: %q", a)
			}

			d.Level = level
		}
	}

	return d
}

// debugf prints debugging information at the specified level, if d.Level is
// high enough to print the message.
func (d *debugger) debugf(level int, format string, v ...interface{}) {
	if d.Level >= level {
		d.Log.Printf(format, v...)
	}
}

func panicf(format string, a ...interface{}) {
	panic(fmt.Sprintf(format, a...))
}