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

lightningnetwork / lnd / 13236757158

10 Feb 2025 08:39AM UTC coverage: 57.649% (-1.2%) from 58.815%
13236757158

Pull #9493

github

ziggie1984
lncli: for some cmds we don't replace the data of the response.

For some cmds it is not very practical to replace the json output
because we might pipe it into other commands. For example when
creating the route we want to pipe it into sendtoRoute.
Pull Request #9493: For some lncli cmds we should not replace the content with other data

0 of 9 new or added lines in 2 files covered. (0.0%)

19535 existing lines in 252 files now uncovered.

103517 of 179563 relevant lines covered (57.65%)

24878.49 hits per line

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

5.45
/build/logrotator.go
1
package build
2

3
import (
4
        "compress/gzip"
5
        "fmt"
6
        "io"
7
        "os"
8
        "path/filepath"
9

10
        "github.com/jrick/logrotate/rotator"
11
        "github.com/klauspost/compress/zstd"
12
)
13

14
// RotatingLogWriter is a wrapper around the LogWriter that supports log file
15
// rotation.
16
type RotatingLogWriter struct {
17
        // pipe is the write-end pipe for writing to the log rotator.
18
        pipe *io.PipeWriter
19

20
        rotator *rotator.Rotator
21
}
22

23
// NewRotatingLogWriter creates a new file rotating log writer.
24
//
25
// NOTE: `InitLogRotator` must be called to set up log rotation after creating
26
// the writer.
27
func NewRotatingLogWriter() *RotatingLogWriter {
1✔
28
        return &RotatingLogWriter{}
1✔
29
}
1✔
30

31
// InitLogRotator initializes the log file rotator to write logs to logFile and
32
// create roll files in the same directory. It should be called as early on
33
// startup and possible and must be closed on shutdown by calling `Close`.
34
func (r *RotatingLogWriter) InitLogRotator(cfg *FileLoggerConfig,
UNCOV
35
        logFile string) error {
×
UNCOV
36

×
UNCOV
37
        logDir, _ := filepath.Split(logFile)
×
UNCOV
38
        err := os.MkdirAll(logDir, 0700)
×
UNCOV
39
        if err != nil {
×
40
                return fmt.Errorf("failed to create log directory: %w", err)
×
41
        }
×
42

UNCOV
43
        r.rotator, err = rotator.New(
×
UNCOV
44
                logFile, int64(cfg.MaxLogFileSize*1024), false, cfg.MaxLogFiles,
×
UNCOV
45
        )
×
UNCOV
46
        if err != nil {
×
47
                return fmt.Errorf("failed to create file rotator: %w", err)
×
48
        }
×
49

50
        // Reject unknown compressors.
UNCOV
51
        if !SupportedLogCompressor(cfg.Compressor) {
×
52
                return fmt.Errorf("unknown log compressor: %v", cfg.Compressor)
×
53
        }
×
54

UNCOV
55
        var c rotator.Compressor
×
UNCOV
56
        switch cfg.Compressor {
×
UNCOV
57
        case Gzip:
×
UNCOV
58
                c = gzip.NewWriter(nil)
×
59

60
        case Zstd:
×
61
                c, err = zstd.NewWriter(nil)
×
62
                if err != nil {
×
63
                        return fmt.Errorf("failed to create zstd compressor: "+
×
64
                                "%w", err)
×
65
                }
×
66
        }
67

68
        // Apply the compressor and its file suffix to the log rotator.
UNCOV
69
        r.rotator.SetCompressor(c, logCompressors[cfg.Compressor])
×
UNCOV
70

×
UNCOV
71
        // Run rotator as a goroutine now but make sure we catch any errors
×
UNCOV
72
        // that happen in case something with the rotation goes wrong during
×
UNCOV
73
        // runtime (like running out of disk space or not being allowed to
×
UNCOV
74
        // create a new logfile for whatever reason).
×
UNCOV
75
        pr, pw := io.Pipe()
×
UNCOV
76
        go func() {
×
UNCOV
77
                err := r.rotator.Run(pr)
×
UNCOV
78
                if err != nil {
×
79
                        _, _ = fmt.Fprintf(os.Stderr,
×
80
                                "failed to run file rotator: %v\n", err)
×
81
                }
×
82
        }()
83

UNCOV
84
        r.pipe = pw
×
UNCOV
85

×
UNCOV
86
        return nil
×
87
}
88

89
// Write writes the byte slice to the log rotator, if present.
UNCOV
90
func (r *RotatingLogWriter) Write(b []byte) (int, error) {
×
UNCOV
91
        if r.rotator != nil {
×
UNCOV
92
                return r.rotator.Write(b)
×
UNCOV
93
        }
×
94

95
        return len(b), nil
×
96
}
97

98
// Close closes the underlying log rotator if it has already been created.
UNCOV
99
func (r *RotatingLogWriter) Close() error {
×
UNCOV
100
        if r.rotator != nil {
×
UNCOV
101
                return r.rotator.Close()
×
UNCOV
102
        }
×
103

104
        return nil
×
105
}
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