From ebee51fbcbaef0486120108804ce072948d4177c Mon Sep 17 00:00:00 2001 From: Donald Adu-Poku Date: Sat, 30 Sep 2017 13:10:14 +0000 Subject: [PATCH] config: add --nofilelogging option. --- config.go | 19 +++++++++++-------- dcrd.go | 3 +++ doc.go | 1 + log.go | 8 ++++++-- 4 files changed, 21 insertions(+), 10 deletions(-) diff --git a/config.go b/config.go index becab8cf..e01350dd 100644 --- a/config.go +++ b/config.go @@ -91,6 +91,7 @@ type config struct { ConfigFile string `short:"C" long:"configfile" description:"Path to configuration file"` DataDir string `short:"b" long:"datadir" description:"Directory to store data"` LogDir string `long:"logdir" description:"Directory to log output."` + NoFileLogging bool `long:"nofilelogging" description:"Disable file logging."` AddPeers []string `short:"a" long:"addpeer" description:"Add a peer to connect with at startup"` ConnectPeers []string `long:"connect" description:"Connect only to the specified peers at startup"` DisableListen bool `long:"nolisten" description:"Disable listening for incoming connections -- NOTE: Listening is automatically disabled if the --connect or --proxy options are used without also specifying listen interfaces via --listen"` @@ -602,11 +603,17 @@ func loadConfig() (*config, []string, error) { var oldTestNets []string oldTestNets = append(oldTestNets, filepath.Join(cfg.DataDir, "testnet")) cfg.DataDir = filepath.Join(cfg.DataDir, netName(activeNetParams)) + logRotator = nil + if !cfg.NoFileLogging { + // Append the network type to the log directory so it is "namespaced" + // per network in the same fashion as the data directory. + cfg.LogDir = cleanAndExpandPath(cfg.LogDir) + cfg.LogDir = filepath.Join(cfg.LogDir, netName(activeNetParams)) - // Append the network type to the log directory so it is "namespaced" - // per network in the same fashion as the data directory. - cfg.LogDir = cleanAndExpandPath(cfg.LogDir) - cfg.LogDir = filepath.Join(cfg.LogDir, netName(activeNetParams)) + // Initialize log rotation. After log rotation has been initialized, the + // logger variables may be used. + initLogRotator(filepath.Join(cfg.LogDir, defaultLogFilename)) + } // Special show command to list supported subsystems and exit. if cfg.DebugLevel == "show" { @@ -614,10 +621,6 @@ func loadConfig() (*config, []string, error) { os.Exit(0) } - // Initialize log rotation. After log rotation has been initialized, the - // logger variables may be used. - initLogRotator(filepath.Join(cfg.LogDir, defaultLogFilename)) - // Parse, validate, and set debug log level(s). if err := parseAndSetDebugLevels(cfg.DebugLevel); err != nil { err := fmt.Errorf("%s: %v", funcName, err.Error()) diff --git a/dcrd.go b/dcrd.go index 2613a1b0..fcb56d63 100644 --- a/dcrd.go +++ b/dcrd.go @@ -53,6 +53,9 @@ func dcrdMain(serverChan chan<- *server) error { // Show version and home dir at startup. dcrdLog.Infof("Version %s (Go version %s)", version(), runtime.Version()) dcrdLog.Infof("Home dir: %s", cfg.HomeDir) + if cfg.NoFileLogging { + dcrdLog.Info("File logging disabled") + } // Enable http profiling server if requested. if cfg.Profile != "" { diff --git a/doc.go b/doc.go index 03557f20..cf6b2c7b 100644 --- a/doc.go +++ b/doc.go @@ -26,6 +26,7 @@ Application Options: -C, --configfile= Path to configuration file -b, --datadir= Directory to store data --logdir= Directory to log output. + --nofilelogging= Disable file logging. -a, --addpeer= Add a peer to connect with at startup --connect= Connect only to the specified peers at startup --nolisten Disable listening for incoming connections -- NOTE: diff --git a/log.go b/log.go index 83426619..8685bdc8 100644 --- a/log.go +++ b/log.go @@ -35,7 +35,9 @@ type logWriter struct{} func (logWriter) Write(p []byte) (n int, err error) { os.Stdout.Write(p) - logRotator.Write(p) + if logRotator != nil { + logRotator.Write(p) + } return len(p), nil } @@ -166,6 +168,8 @@ func directionString(inbound bool) string { func fatalf(str string) { dcrdLog.Errorf("Unable to create profiler: %v", str) os.Stdout.Sync() - logRotator.Close() + if logRotator != nil { + logRotator.Close() + } os.Exit(1) }