• 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

14.63
/build/log.go
1
package build
2

3
import (
4
        "os"
5

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

9
// LogType is an indicating the type of logging specified by the build flag.
10
type LogType byte
11

12
const (
13
        // LogTypeNone indicates no logging.
14
        LogTypeNone LogType = iota
15

16
        // LogTypeStdOut all logging is written directly to stdout.
17
        LogTypeStdOut
18

19
        // LogTypeDefault logs to both stdout and a given io.PipeWriter.
20
        LogTypeDefault
21
)
22

23
// String returns a human readable identifier for the logging type.
24
func (t LogType) String() string {
×
25
        switch t {
×
26
        case LogTypeNone:
×
27
                return "none"
×
28
        case LogTypeStdOut:
×
29
                return "stdout"
×
30
        case LogTypeDefault:
×
31
                return "default"
×
32
        default:
×
33
                return "unknown"
×
34
        }
35
}
36

37
// Declare the supported log file compressors as exported consts for easier use
38
// from other projects.
39
const (
40
        // Gzip is the default compressor.
41
        Gzip = "gzip"
42

43
        // Zstd is a modern compressor that compresses better than Gzip, in less
44
        // time.
45
        Zstd = "zstd"
46
)
47

48
// logCompressors maps the identifier for each supported compression algorithm
49
// to the extension used for the compressed log files.
50
var logCompressors = map[string]string{
51
        Gzip: "gz",
52
        Zstd: "zst",
53
}
54

55
// SupportedLogCompressor returns whether or not logCompressor is a supported
56
// compression algorithm for log files.
57
func SupportedLogCompressor(logCompressor string) bool {
×
58
        _, ok := logCompressors[logCompressor]
×
59

×
60
        return ok
×
61
}
×
62

63
// NewSubLogger constructs a new subsystem log from the current LogWriter
64
// implementation. This is primarily intended for use with stdlog, as the actual
65
// writer is shared amongst all instantiations.
66
func NewSubLogger(subsystem string,
67
        genSubLogger func(string) btclog.Logger) btclog.Logger {
669✔
68

669✔
69
        switch Deployment {
669✔
70

71
        // For production builds, generate a new subsystem logger from the
72
        // primary log backend. If no function is provided, logging will be
73
        // disabled.
74
        case Production:
×
75
                if genSubLogger != nil {
×
76
                        return genSubLogger(subsystem)
×
77
                }
×
78

79
        // For development builds, we must handle two distinct types of logging:
80
        // unit tests and running the live daemon, e.g. for integration testing.
81
        case Development:
669✔
82
                switch LoggingType {
669✔
83

84
                // Default logging is used when running the standalone daemon.
85
                // We'll use the optional sublogger constructor to mimic the
86
                // production behavior.
87
                case LogTypeDefault:
×
88
                        if genSubLogger != nil {
×
89
                                return genSubLogger(subsystem)
×
90
                        }
×
91

92
                // Logging to stdout is used in unit tests. It is not important
93
                // that they share the same backend, since all output is written
94
                // to std out.
95
                case LogTypeStdOut:
×
96
                        backend := btclog.NewDefaultHandler(os.Stdout)
×
97
                        logger := btclog.NewSLogger(
×
98
                                backend.SubSystem(subsystem),
×
99
                        )
×
100

×
101
                        // Set the logging level of the stdout logger to use the
×
102
                        // configured logging level specified by build flags.
×
103
                        level, _ := btclog.LevelFromString(LogLevel)
×
104
                        logger.SetLevel(level)
×
105

×
106
                        return logger
×
107
                }
108
        }
109

110
        // For any other configurations, we'll disable logging.
111
        return btclog.Disabled
669✔
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