Skip to content

Commit

Permalink
fixup! main: add logcompressor flag
Browse files Browse the repository at this point in the history
  • Loading branch information
jharveyb committed Sep 11, 2024
1 parent 6072ae2 commit 966d7dc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
6 changes: 3 additions & 3 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const (
defaultLogLevel = "info"
defaultLogDirname = "logs"
defaultLogFilename = "btcd.log"
defaultLogCompressor = "gzip"
defaultLogCompressor = Gzip
defaultMaxPeers = 125
defaultBanDuration = time.Hour * 24
defaultBanThreshold = 100
Expand Down Expand Up @@ -126,7 +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."`
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 @@ -665,7 +665,7 @@ func loadConfig() (*config, []string, error) {
}

// Validate that the selected log compression algorithm is supported.
if _, ok := logCompressors[cfg.LogCompressor]; !ok {
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)
Expand Down
24 changes: 20 additions & 4 deletions log.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,23 @@ 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",
Gzip: "gz",
Zstd: "zst",
}

func supportedCompressor(compressor string) bool {
_, ok := logCompressors[compressor]
return ok
}

// initLogRotator initializes the logging rotater to write logs to logFile and
Expand All @@ -130,8 +142,12 @@ func initLogRotator(logFile, logCompressor string) {
os.Exit(1)
}

// The selected compressor was validated by the caller, so we don't need
// to handle an unknown compressor here.
// Reject unknown compressors.
if !supportedCompressor(cfg.LogCompressor) {
fmt.Printf("specified log compressor [%v] is invalid",
cfg.LogCompressor)
}

var c rotator.Compressor
switch logCompressor {
case "gzip":
Expand Down

0 comments on commit 966d7dc

Please sign in to comment.