• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

lightningnetwork / lnd / 12312390362

13 Dec 2024 08:44AM UTC coverage: 57.458% (+8.5%) from 48.92%
12312390362

Pull #9343

github

ellemouton
fn: rework the ContextGuard and add tests

In this commit, the ContextGuard struct is re-worked such that the
context that its new main WithCtx method provides is cancelled in sync
with a parent context being cancelled or with it's quit channel being
cancelled. Tests are added to assert the behaviour. In order for the
close of the quit channel to be consistent with the cancelling of the
derived context, the quit channel _must_ be contained internal to the
ContextGuard so that callers are only able to close the channel via the
exposed Quit method which will then take care to first cancel any
derived context that depend on the quit channel before returning.
Pull Request #9343: fn: expand the ContextGuard and add tests

101853 of 177264 relevant lines covered (57.46%)

24972.93 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

13.21
/build/config_dev.go
1
//go:build dev
2
// +build dev
3

4
package build
5

6
import (
7
        "fmt"
8
        "strings"
9

10
        btclogv1 "github.com/btcsuite/btclog"
11
        "github.com/btcsuite/btclog/v2"
12
)
13

14
const (
15
        resetSeq = "0"
16
        boldSeq  = "1"
17
        faintSeq = "2"
18
        esc      = '\x1b'
19
        csi      = string(esc) + "["
20
)
21

22
// consoleLoggerCfg extends the LoggerConfig struct by adding a Color option
23
// which is only available for a console logger.
24
//
25
//nolint:ll
26
type consoleLoggerCfg struct {
27
        LoggerConfig
28
        Style bool `long:"style" description:"If set, the output will be styled with color and fonts"`
29
}
30

31
// defaultConsoleLoggerCfg returns the default consoleLoggerCfg for the dev
32
// console logger.
33
func defaultConsoleLoggerCfg() *consoleLoggerCfg {
1✔
34
        return &consoleLoggerCfg{
1✔
35
                LoggerConfig: LoggerConfig{
1✔
36
                        CallSite: callSiteShort,
1✔
37
                },
1✔
38
        }
1✔
39
}
1✔
40

41
// HandlerOptions returns the set of btclog.HandlerOptions that the state of the
42
// config struct translates to.
43
func (cfg *consoleLoggerCfg) HandlerOptions() []btclog.HandlerOption {
×
44
        opts := cfg.LoggerConfig.HandlerOptions()
×
45

×
46
        if !cfg.Style {
×
47
                return opts
×
48
        }
×
49

50
        return append(
×
51
                opts, btclog.WithStyledLevel(
×
52
                        func(l btclogv1.Level) string {
×
53
                                return styleString(
×
54
                                        fmt.Sprintf("[%s]", l),
×
55
                                        boldSeq,
×
56
                                        string(ansiColoSeq(l)),
×
57
                                )
×
58
                        },
×
59
                ),
60
                btclog.WithStyledCallSite(
61
                        func(file string, line int) string {
×
62
                                str := fmt.Sprintf("%s:%d", file, line)
×
63

×
64
                                return styleString(str, faintSeq)
×
65
                        },
×
66
                ),
67
                btclog.WithStyledKeys(func(key string) string {
×
68
                        return styleString(key, faintSeq)
×
69
                }),
×
70
        )
71
}
72

73
func styleString(s string, styles ...string) string {
×
74
        if len(styles) == 0 {
×
75
                return s
×
76
        }
×
77

78
        seq := strings.Join(styles, ";")
×
79
        if seq == "" {
×
80
                return s
×
81
        }
×
82

83
        return fmt.Sprintf("%s%sm%s%sm", csi, seq, s, csi+resetSeq)
×
84
}
85

86
type ansiColorSeq string
87

88
const (
89
        ansiColorSeqDarkTeal  ansiColorSeq = "38;5;30"
90
        ansiColorSeqDarkBlue  ansiColorSeq = "38;5;63"
91
        ansiColorSeqLightBlue ansiColorSeq = "38;5;86"
92
        ansiColorSeqYellow    ansiColorSeq = "38;5;192"
93
        ansiColorSeqRed       ansiColorSeq = "38;5;204"
94
        ansiColorSeqPink      ansiColorSeq = "38;5;134"
95
)
96

97
func ansiColoSeq(l btclogv1.Level) ansiColorSeq {
×
98
        switch l {
×
99
        case btclog.LevelTrace:
×
100
                return ansiColorSeqDarkTeal
×
101
        case btclog.LevelDebug:
×
102
                return ansiColorSeqDarkBlue
×
103
        case btclog.LevelWarn:
×
104
                return ansiColorSeqYellow
×
105
        case btclog.LevelError:
×
106
                return ansiColorSeqRed
×
107
        case btclog.LevelCritical:
×
108
                return ansiColorSeqPink
×
109
        default:
×
110
                return ansiColorSeqLightBlue
×
111
        }
112
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc