Skip to content

Commit

Permalink
main: add logcompressor flag
Browse files Browse the repository at this point in the history
  • Loading branch information
jharveyb committed Sep 12, 2024
1 parent 2b53ed1 commit 8507e15
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
12 changes: 12 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const (
defaultLogLevel = "info"
defaultLogDirname = "logs"
defaultLogFilename = "btcd.log"
defaultLogCompressor = Gzip
defaultMaxPeers = 125
defaultBanDuration = time.Hour * 24
defaultBanThreshold = 100
Expand Down Expand Up @@ -125,6 +126,7 @@ type config struct {
FreeTxRelayLimit float64 `long:"limitfreerelay" description:"Limit relay of transactions with no transaction fee to the given amount in thousands of bytes per minute"`
Listeners []string `long:"listen" description:"Add an interface/port to listen for connections (default all interfaces port: 8333, testnet: 18333)"`
LogDir string `long:"logdir" description:"Directory to log output."`
LogCompressor string `long:"logcompressor" description:"Compression algorithm to use when rotating logs." choice:"gzip" choice:"zstd"`
MaxOrphanTxs int `long:"maxorphantx" description:"Max number of orphan transactions to keep in memory"`
MaxPeers int `long:"maxpeers" description:"Max number of inbound and outbound peers"`
MiningAddrs []string `long:"miningaddr" description:"Add the specified payment address to the list of addresses to use for generated blocks -- At least one address is required if the generate option is set"`
Expand Down Expand Up @@ -428,6 +430,7 @@ func loadConfig() (*config, []string, error) {
RPCMaxConcurrentReqs: defaultMaxRPCConcurrentReqs,
DataDir: defaultDataDir,
LogDir: defaultLogDir,
LogCompressor: defaultLogCompressor,
DbType: defaultDbType,
RPCKey: defaultRPCKeyFile,
RPCCert: defaultRPCCertFile,
Expand Down Expand Up @@ -661,6 +664,15 @@ func loadConfig() (*config, []string, error) {
os.Exit(0)
}

// Validate that the selected log compression algorithm is supported.
if !supportedCompressor(cfg.LogCompressor) {
str := "%s: The specified log compressor [%v] is invalid"
err := fmt.Errorf(str, funcName, cfg.LogCompressor)
fmt.Fprintln(os.Stderr, err)
fmt.Fprintln(os.Stderr, usageMessage)
return nil, nil, err
}

// Initialize log rotation. After log rotation has been initialized, the
// logger variables may be used.
initLogRotator(filepath.Join(cfg.LogDir, defaultLogFilename))
Expand Down
2 changes: 2 additions & 0 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ Application Options:
(default all interfaces port: 8333, testnet:
18333, signet: 38333)
--logdir= Directory to log output
--logcompressor= Compression algorithm to use when rotating logs.
(default: gzip)
--maxorphantx= Max number of orphan transactions to keep in
memory (default: 100)
--maxpeers= Max number of inbound and outbound peers
Expand Down
28 changes: 28 additions & 0 deletions log.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,27 @@ var subsystemLoggers = map[string]btclog.Logger{
"TXMP": txmpLog,
}

// Declare the supported compressors as exported consts for easier use from
// other projects.
const (
Gzip = "gzip"
Zstd = "zstd"
)

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

// supportedCompressor checks that the named compressor is known and supported
// for use during log rotation.
func supportedCompressor(compressor string) bool {
_, ok := logCompressors[compressor]
return ok
}

// initLogRotator initializes the logging rotater to write logs to logFile and
// create roll files in the same directory. It must be called before the
// package-global log rotater variables are used.
Expand All @@ -121,6 +142,13 @@ func initLogRotator(logFile string) {
os.Exit(1)
}

// Reject unknown compressors.
if !supportedCompressor(cfg.LogCompressor) {
fmt.Fprintf(os.Stderr, "specified log compressor [%v] is "+
"invalid", cfg.LogCompressor)
os.Exit(1)
}

logRotator = r
}

Expand Down

0 comments on commit 8507e15

Please sign in to comment.