• 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

36.11
/build/config.go
1
package build
2

3
import (
4
        "fmt"
5

6
        "github.com/btcsuite/btclog/v2"
7
)
8

9
const (
10
        callSiteOff   = "off"
11
        callSiteShort = "short"
12
        callSiteLong  = "long"
13

14
        defaultLogCompressor = Gzip
15

16
        // DefaultMaxLogFiles is the default maximum number of log files to
17
        // keep.
18
        DefaultMaxLogFiles = 10
19

20
        // DefaultMaxLogFileSize is the default maximum log file size in MB.
21
        DefaultMaxLogFileSize = 20
22
)
23

24
// LogConfig holds logging configuration options.
25
//
26
//nolint:ll
27
type LogConfig struct {
28
        Console      *consoleLoggerCfg `group:"console" namespace:"console" description:"The logger writing to stdout and stderr."`
29
        File         *FileLoggerConfig `group:"file" namespace:"file" description:"The logger writing to LND's standard log file."`
30
        NoCommitHash bool              `long:"no-commit-hash" description:"If set, the commit-hash of the current build will not be included in log lines by default."`
31
}
32

33
// Validate validates the LogConfig struct values.
34
func (c *LogConfig) Validate() error {
×
35
        if !SupportedLogCompressor(c.File.Compressor) {
×
36
                return fmt.Errorf("invalid log compressor: %v",
×
37
                        c.File.Compressor)
×
38
        }
×
39

40
        return nil
×
41
}
42

43
// LoggerConfig holds options for a particular logger.
44
//
45
//nolint:ll
46
type LoggerConfig struct {
47
        Disable      bool   `long:"disable" description:"Disable this logger."`
48
        NoTimestamps bool   `long:"no-timestamps" description:"Omit timestamps from log lines."`
49
        CallSite     string `long:"call-site" description:"Include the call-site of each log line." choice:"off" choice:"short" choice:"long"`
50
}
51

52
// DefaultLogConfig returns the default logging config options.
53
func DefaultLogConfig() *LogConfig {
1✔
54
        return &LogConfig{
1✔
55
                Console: defaultConsoleLoggerCfg(),
1✔
56
                File: &FileLoggerConfig{
1✔
57
                        Compressor:     defaultLogCompressor,
1✔
58
                        MaxLogFiles:    DefaultMaxLogFiles,
1✔
59
                        MaxLogFileSize: DefaultMaxLogFileSize,
1✔
60
                        LoggerConfig: LoggerConfig{
1✔
61
                                CallSite: callSiteOff,
1✔
62
                        },
1✔
63
                },
1✔
64
        }
1✔
65
}
1✔
66

67
// HandlerOptions returns the set of btclog.HandlerOptions that the state of the
68
// config struct translates to.
69
func (cfg *LoggerConfig) HandlerOptions() []btclog.HandlerOption {
×
70
        opts := []btclog.HandlerOption{
×
71
                // We wrap the logger provided by the logging library with
×
72
                // another layer of abstraction with the handlerSet, and so we
×
73
                // need to increase the default skip depth by 1.
×
74
                btclog.WithCallSiteSkipDepth(btclog.DefaultSkipDepth + 1),
×
75
        }
×
76

×
77
        if cfg.NoTimestamps {
×
78
                opts = append(opts, btclog.WithNoTimestamp())
×
79
        }
×
80

81
        switch cfg.CallSite {
×
82
        case callSiteShort:
×
83
                opts = append(opts, btclog.WithCallerFlags(btclog.Lshortfile))
×
84
        case callSiteLong:
×
85
                opts = append(opts, btclog.WithCallerFlags(btclog.Llongfile))
×
86
        }
87

88
        return opts
×
89
}
90

91
// FileLoggerConfig extends LoggerConfig with specific log file options.
92
//
93
//nolint:ll
94
type FileLoggerConfig struct {
95
        LoggerConfig
96
        Compressor     string `long:"compressor" description:"Compression algorithm to use when rotating logs." choice:"gzip" choice:"zstd"`
97
        MaxLogFiles    int    `long:"max-files" description:"Maximum logfiles to keep (0 for no rotation)"`
98
        MaxLogFileSize int    `long:"max-file-size" description:"Maximum logfile size in MB"`
99
}
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