From ad5ecdca887e3028fdfb9a44751085f9d8079f6a Mon Sep 17 00:00:00 2001 From: Jonathan Harvey-Buschel Date: Mon, 26 Aug 2024 17:04:37 -0400 Subject: [PATCH] main: add logcompressor flag --- config.go | 12 ++++++++++++ doc.go | 2 ++ log.go | 7 +++++++ 3 files changed, 21 insertions(+) diff --git a/config.go b/config.go index 9bbce7f69a..70372e55c9 100644 --- a/config.go +++ b/config.go @@ -41,6 +41,7 @@ const ( defaultLogLevel = "info" defaultLogDirname = "logs" defaultLogFilename = "btcd.log" + defaultLogCompressor = "gzip" defaultMaxPeers = 125 defaultBanDuration = time.Hour * 24 defaultBanThreshold = 100 @@ -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. Valid algorithms are: gzip and 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"` @@ -428,6 +430,7 @@ func loadConfig() (*config, []string, error) { RPCMaxConcurrentReqs: defaultMaxRPCConcurrentReqs, DataDir: defaultDataDir, LogDir: defaultLogDir, + LogCompressor: defaultLogCompressor, DbType: defaultDbType, RPCKey: defaultRPCKeyFile, RPCCert: defaultRPCCertFile, @@ -661,6 +664,15 @@ func loadConfig() (*config, []string, error) { os.Exit(0) } + // Validate that the selected log compression algorithm is supported. + if _, ok := logCompressors[cfg.LogCompressor]; !ok { + 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)) diff --git a/doc.go b/doc.go index 47e4e626b7..6972f23baf 100644 --- a/doc.go +++ b/doc.go @@ -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 diff --git a/log.go b/log.go index 5707d7c23a..d333b74ca9 100644 --- a/log.go +++ b/log.go @@ -105,6 +105,13 @@ var subsystemLoggers = map[string]btclog.Logger{ "TXMP": txmpLog, } +// 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", +} + // 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.