diff --git a/blockchain/log.go b/blockchain/log.go index 6233fc9b..d5fb2625 100644 --- a/blockchain/log.go +++ b/blockchain/log.go @@ -5,9 +5,6 @@ package blockchain import ( - "errors" - "io" - "github.com/btcsuite/btclog" ) @@ -22,37 +19,12 @@ func init() { } // DisableLog disables all library log output. Logging output is disabled -// by default until either UseLogger or SetLogWriter are called. +// by default until UseLogger is called. func DisableLog() { log = btclog.Disabled } // UseLogger uses a specified Logger to output package logging info. -// This should be used in preference to SetLogWriter if the caller is also -// using btclog. func UseLogger(logger btclog.Logger) { log = logger } - -// SetLogWriter uses a specified io.Writer to output package logging info. -// This allows a caller to direct package logging output without needing a -// dependency on seelog. If the caller is also using btclog, UseLogger should -// be used instead. -func SetLogWriter(w io.Writer, level string) error { - if w == nil { - return errors.New("nil writer") - } - - lvl, ok := btclog.LogLevelFromString(level) - if !ok { - return errors.New("invalid log level") - } - - l, err := btclog.NewLoggerFromWriter(w, lvl) - if err != nil { - return err - } - - UseLogger(l) - return nil -} diff --git a/btcd.go b/btcd.go index 88921192..fef72f4b 100644 --- a/btcd.go +++ b/btcd.go @@ -39,7 +39,11 @@ func btcdMain(serverChan chan<- *server) error { return err } cfg = tcfg - defer backendLog.Flush() + defer func() { + if logRotator != nil { + logRotator.Close() + } + }() // Get a channel that will be closed when a shutdown signal has been // triggered either from an OS signal such as SIGINT (Ctrl+C) or from diff --git a/cmd/addblock/addblock.go b/cmd/addblock/addblock.go index 30a8c802..15b61b68 100644 --- a/cmd/addblock/addblock.go +++ b/cmd/addblock/addblock.go @@ -69,12 +69,12 @@ func realMain() error { cfg = tcfg // Setup logging. - backendLogger := btclog.NewDefaultBackendLogger() - defer backendLogger.Flush() - log = btclog.NewSubsystemLogger(backendLogger, "") - database.UseLogger(btclog.NewSubsystemLogger(backendLogger, "BCDB: ")) - blockchain.UseLogger(btclog.NewSubsystemLogger(backendLogger, "CHAN: ")) - indexers.UseLogger(btclog.NewSubsystemLogger(backendLogger, "INDX: ")) + backendLogger := btclog.NewBackend(os.Stdout) + defer os.Stdout.Sync() + log = backendLogger.Logger("MAIN") + database.UseLogger(backendLogger.Logger("BCDB")) + blockchain.UseLogger(backendLogger.Logger("CHAN")) + indexers.UseLogger(backendLogger.Logger("INDX")) // Load the block database. db, err := loadBlockDB() diff --git a/config.go b/config.go index ecbdb334..d34fa5d4 100644 --- a/config.go +++ b/config.go @@ -579,9 +579,9 @@ func loadConfig() (*config, []string, error) { os.Exit(0) } - // Initialize logging at the default logging level. - initSeelogLogger(filepath.Join(cfg.LogDir, defaultLogFilename)) - setLogLevels(defaultLogLevel) + // 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 { diff --git a/connmgr/connmanager_test.go b/connmgr/connmanager_test.go index c947b2b7..03b6dd2e 100644 --- a/connmgr/connmanager_test.go +++ b/connmgr/connmanager_test.go @@ -5,15 +5,12 @@ package connmgr import ( - "bytes" "errors" "io" "net" "sync/atomic" "testing" "time" - - "github.com/btcsuite/btclog" ) func init() { @@ -85,15 +82,6 @@ func TestNewConfig(t *testing.T) { } } -// TestUseLogger tests that a logger can be passed to UseLogger -func TestUseLogger(t *testing.T) { - l, err := btclog.NewLoggerFromWriter(bytes.NewBuffer(nil), btclog.InfoLvl) - if err != nil { - t.Fatal(err) - } - UseLogger(l) -} - // TestStartStop tests that the connection manager starts and stops as // expected. func TestStartStop(t *testing.T) { diff --git a/database/cmd/dbtool/main.go b/database/cmd/dbtool/main.go index e6cfd62f..9eccd598 100644 --- a/database/cmd/dbtool/main.go +++ b/database/cmd/dbtool/main.go @@ -61,11 +61,11 @@ func loadBlockDB() (database.DB, error) { // around the fact that deferred functions do not run when os.Exit() is called. func realMain() error { // Setup logging. - backendLogger := btclog.NewDefaultBackendLogger() - defer backendLogger.Flush() - log = btclog.NewSubsystemLogger(backendLogger, "") - dbLog := btclog.NewSubsystemLogger(backendLogger, "BCDB: ") - dbLog.SetLevel(btclog.DebugLvl) + backendLogger := btclog.NewBackend(os.Stdout) + defer os.Stdout.Sync() + log = backendLogger.Logger("MAIN") + dbLog := backendLogger.Logger("BCDB") + dbLog.SetLevel(btclog.LevelDebug) database.UseLogger(dbLog) // Setup the parser options and commands. diff --git a/database/ffldb/blockio.go b/database/ffldb/blockio.go index 868e1e28..bfbdbe14 100644 --- a/database/ffldb/blockio.go +++ b/database/ffldb/blockio.go @@ -676,7 +676,7 @@ func (s *blockStore) handleRollback(oldBlockFileNum, oldBlockOffset uint32) { } for ; wc.curFileNum > oldBlockFileNum; wc.curFileNum-- { if err := s.deleteFileFunc(wc.curFileNum); err != nil { - _ = log.Warnf("ROLLBACK: Failed to delete block file "+ + log.Warnf("ROLLBACK: Failed to delete block file "+ "number %d: %v", wc.curFileNum, err) return } @@ -688,7 +688,7 @@ func (s *blockStore) handleRollback(oldBlockFileNum, oldBlockOffset uint32) { obf, err := s.openWriteFileFunc(wc.curFileNum) if err != nil { wc.curFile.Unlock() - _ = log.Warnf("ROLLBACK: %v", err) + log.Warnf("ROLLBACK: %v", err) return } wc.curFile.file = obf @@ -697,7 +697,7 @@ func (s *blockStore) handleRollback(oldBlockFileNum, oldBlockOffset uint32) { // Truncate the to the provided rollback offset. if err := wc.curFile.file.Truncate(int64(oldBlockOffset)); err != nil { wc.curFile.Unlock() - _ = log.Warnf("ROLLBACK: Failed to truncate file %d: %v", + log.Warnf("ROLLBACK: Failed to truncate file %d: %v", wc.curFileNum, err) return } @@ -706,7 +706,7 @@ func (s *blockStore) handleRollback(oldBlockFileNum, oldBlockOffset uint32) { err := wc.curFile.file.Sync() wc.curFile.Unlock() if err != nil { - _ = log.Warnf("ROLLBACK: Failed to sync file %d: %v", + log.Warnf("ROLLBACK: Failed to sync file %d: %v", wc.curFileNum, err) return } diff --git a/database/ffldb/reconcile.go b/database/ffldb/reconcile.go index 1f75d1c1..e2c4d6bb 100644 --- a/database/ffldb/reconcile.go +++ b/database/ffldb/reconcile.go @@ -109,7 +109,7 @@ func reconcileDB(pdb *db, create bool) (database.DB, error) { str := fmt.Sprintf("metadata claims file %d, offset %d, but "+ "block data is at file %d, offset %d", curFileNum, curOffset, wc.curFileNum, wc.curOffset) - _ = log.Warnf("***Database corruption detected***: %v", str) + log.Warnf("***Database corruption detected***: %v", str) return nil, makeDbErr(database.ErrCorruption, str, nil) } diff --git a/database/log.go b/database/log.go index 4a7b9a88..d2c5c44e 100644 --- a/database/log.go +++ b/database/log.go @@ -5,9 +5,6 @@ package database import ( - "errors" - "io" - "github.com/btcsuite/btclog" ) @@ -22,14 +19,12 @@ func init() { } // DisableLog disables all library log output. Logging output is disabled -// by default until either UseLogger or SetLogWriter are called. +// by default until UseLogger is called. func DisableLog() { log = btclog.Disabled } // UseLogger uses a specified Logger to output package logging info. -// This should be used in preference to SetLogWriter if the caller is also -// using btclog. func UseLogger(logger btclog.Logger) { log = logger @@ -40,26 +35,3 @@ func UseLogger(logger btclog.Logger) { } } } - -// SetLogWriter uses a specified io.Writer to output package logging info. -// This allows a caller to direct package logging output without needing a -// dependency on seelog. If the caller is also using btclog, UseLogger should -// be used instead. -func SetLogWriter(w io.Writer, level string) error { - if w == nil { - return errors.New("nil writer") - } - - lvl, ok := btclog.LogLevelFromString(level) - if !ok { - return errors.New("invalid log level") - } - - l, err := btclog.NewLoggerFromWriter(w, lvl) - if err != nil { - return err - } - - UseLogger(l) - return nil -} diff --git a/database/log_test.go b/database/log_test.go deleted file mode 100644 index 6b37cf67..00000000 --- a/database/log_test.go +++ /dev/null @@ -1,67 +0,0 @@ -// Copyright (c) 2015-2016 The btcsuite developers -// Use of this source code is governed by an ISC -// license that can be found in the LICENSE file. - -package database_test - -import ( - "errors" - "io" - "os" - "testing" - - "github.com/btcsuite/btcd/database" -) - -// TestSetLogWriter ensures the -func TestSetLogWriter(t *testing.T) { - tests := []struct { - name string - w io.Writer - level string - expected error - }{ - { - name: "nil writer", - w: nil, - level: "trace", - expected: errors.New("nil writer"), - }, - { - name: "invalid log level", - w: os.Stdout, - level: "wrong", - expected: errors.New("invalid log level"), - }, - { - name: "use off level", - w: os.Stdout, - level: "off", - expected: errors.New("min level can't be greater than max. Got min: 6, max: 5"), - }, - { - name: "pass", - w: os.Stdout, - level: "debug", - expected: nil, - }, - } - - t.Logf("Running %d tests", len(tests)) - for i, test := range tests { - err := database.SetLogWriter(test.w, test.level) - if err != nil { - if err.Error() != test.expected.Error() { - t.Errorf("SetLogWriter #%d (%s) wrong result\n"+ - "got: %v\nwant: %v", i, test.name, err, - test.expected) - } - } else { - if test.expected != nil { - t.Errorf("SetLogWriter #%d (%s) wrong result\n"+ - "got: %v\nwant: %v", i, test.name, err, - test.expected) - } - } - } -} diff --git a/glide.lock b/glide.lock index c36803cd..d79e7451 100644 --- a/glide.lock +++ b/glide.lock @@ -1,10 +1,10 @@ -hash: 4a7317ba20a3d8dd1ea253b86d86231d97cf85c16ca368471a50e8c42771daf0 -updated: 2017-05-08T23:05:30.5569509-05:00 +hash: 9e111ebb6989c8b23db0ef5c0523e8dde1ccaa964fa7a42faccbb7a6a60b2860 +updated: 2017-06-19T16:45:22.5258215-04:00 imports: - name: github.com/btcsuite/btclog - version: 73889fb79bd687870312b6e40effcecffbd57d30 + version: 96c2a91a67da03552a5e6554fe3ccbfbc7f860be - name: github.com/btcsuite/btcrpcclient - version: abcdfb702a7ca67e4a32c10be380c63216bd69fe + version: 45b9cb481d2aead4e80aab32d7aa86db386430ab - name: github.com/btcsuite/btcutil version: dcd4997b0664bcfd6ef48e4ae9da8396e08b1cd9 subpackages: @@ -30,8 +30,6 @@ imports: - leveldb/storage - leveldb/table - leveldb/util -- name: github.com/btcsuite/seelog - version: 313961b101eb55f65ae0f03ddd4e322731763b6c - name: github.com/btcsuite/snappy-go version: 0bdef8d067237991ddaa1bb6072a740bc40601ba - name: github.com/btcsuite/websocket @@ -50,6 +48,10 @@ imports: - spew - name: github.com/jessevdk/go-flags version: 1679536dcc895411a9f5848d9a0250be7856448c +- name: github.com/jrick/logrotate + version: 4ed05ed86ef17d10ff99cce77481e0fcf6f2c7b0 + subpackages: + - rotator - name: golang.org/x/crypto version: 122d919ec1efcfb58483215da23f815853e24b81 subpackages: diff --git a/glide.yaml b/glide.yaml index 2de57b2c..c4c0c425 100644 --- a/glide.yaml +++ b/glide.yaml @@ -21,7 +21,6 @@ import: - leveldb/iterator - leveldb/opt - leveldb/util -- package: github.com/btcsuite/seelog - package: github.com/btcsuite/websocket - package: github.com/btcsuite/winsvc subpackages: @@ -33,3 +32,4 @@ import: - spew - package: github.com/jessevdk/go-flags version: 1679536dcc895411a9f5848d9a0250be7856448c +- package: github.com/jrick/logrotate diff --git a/log.go b/log.go index a4d403e6..437bfeca 100644 --- a/log.go +++ b/log.go @@ -1,4 +1,5 @@ -// Copyright (c) 2013-2016 The btcsuite developers +// Copyright (c) 2013-2017 The btcsuite developers +// Copyright (c) 2017 The Decred developers // Use of this source code is governed by an ISC // license that can be found in the LICENSE file. @@ -6,7 +7,9 @@ package main import ( "fmt" + "io" "os" + "path/filepath" "github.com/btcsuite/btcd/addrmgr" "github.com/btcsuite/btcd/blockchain" @@ -19,32 +22,72 @@ import ( "github.com/btcsuite/btcd/peer" "github.com/btcsuite/btcd/txscript" "github.com/btcsuite/btclog" - "github.com/btcsuite/seelog" + "github.com/jrick/logrotate/rotator" ) -// Loggers per subsystem. Note that backendLog is a seelog logger that all of -// the subsystem loggers route their messages to. When adding new subsystems, -// add a reference here, to the subsystemLoggers map, and the useLogger -// function. +// logWriter implements an io.Writer that outputs to both standard output and +// the write-end pipe of an initialized log rotator. +type logWriter struct{} + +func (logWriter) Write(p []byte) (n int, err error) { + os.Stdout.Write(p) + logRotatorPipe.Write(p) + return len(p), nil +} + +// Loggers per subsystem. A single backend logger is created and all subsytem +// loggers created from it will write to the backend. When adding new +// subsystems, add the subsystem logger variable here and to the +// subsystemLoggers map. +// +// Loggers can not be used before the log rotator has been initialized with a +// log file. This must be performed early during application startup by calling +// initLogRotator. var ( - backendLog = seelog.Disabled - adxrLog = btclog.Disabled - amgrLog = btclog.Disabled - cmgrLog = btclog.Disabled - bcdbLog = btclog.Disabled - bmgrLog = btclog.Disabled - btcdLog = btclog.Disabled - chanLog = btclog.Disabled - discLog = btclog.Disabled - indxLog = btclog.Disabled - minrLog = btclog.Disabled - peerLog = btclog.Disabled - rpcsLog = btclog.Disabled - scrpLog = btclog.Disabled - srvrLog = btclog.Disabled - txmpLog = btclog.Disabled + // backendLog is the logging backend used to create all subsystem loggers. + // The backend must not be used before the log rotator has been initialized, + // or data races and/or nil pointer dereferences will occur. + backendLog = btclog.NewBackend(logWriter{}) + + // logRotator is one of the logging outputs. It should be closed on + // application shutdown. + logRotator *rotator.Rotator + + // logRotatorPipe is the write-end pipe for writing to the log rotator. It + // is written to by the Write method of the logWriter type. + logRotatorPipe *io.PipeWriter + + adxrLog = backendLog.Logger("ADXR") + amgrLog = backendLog.Logger("AMGR") + cmgrLog = backendLog.Logger("CMGR") + bcdbLog = backendLog.Logger("BCDB") + bmgrLog = backendLog.Logger("BMGR") + btcdLog = backendLog.Logger("BTCD") + chanLog = backendLog.Logger("CHAN") + discLog = backendLog.Logger("DISC") + indxLog = backendLog.Logger("INDX") + minrLog = backendLog.Logger("MINR") + peerLog = backendLog.Logger("PEER") + rpcsLog = backendLog.Logger("RPCS") + scrpLog = backendLog.Logger("SCRP") + srvrLog = backendLog.Logger("SRVR") + txmpLog = backendLog.Logger("TXMP") ) +// Initialize package-global logger variables. +func init() { + addrmgr.UseLogger(amgrLog) + connmgr.UseLogger(cmgrLog) + database.UseLogger(bcdbLog) + blockchain.UseLogger(chanLog) + indexers.UseLogger(indxLog) + mining.UseLogger(minrLog) + cpuminer.UseLogger(minrLog) + peer.UseLogger(peerLog) + txscript.UseLogger(scrpLog) + mempool.UseLogger(txmpLog) +} + // subsystemLoggers maps each subsystem identifier to its associated logger. var subsystemLoggers = map[string]btclog.Logger{ "ADXR": adxrLog, @@ -64,95 +107,27 @@ var subsystemLoggers = map[string]btclog.Logger{ "TXMP": txmpLog, } -// useLogger updates the logger references for subsystemID to logger. Invalid -// subsystems are ignored. -func useLogger(subsystemID string, logger btclog.Logger) { - if _, ok := subsystemLoggers[subsystemID]; !ok { - return - } - subsystemLoggers[subsystemID] = logger - - switch subsystemID { - case "ADXR": - adxrLog = logger - - case "AMGR": - amgrLog = logger - addrmgr.UseLogger(logger) - - case "CMGR": - cmgrLog = logger - connmgr.UseLogger(logger) - - case "BCDB": - bcdbLog = logger - database.UseLogger(logger) - - case "BMGR": - bmgrLog = logger - - case "BTCD": - btcdLog = logger - - case "CHAN": - chanLog = logger - blockchain.UseLogger(logger) - - case "DISC": - discLog = logger - - case "INDX": - indxLog = logger - indexers.UseLogger(logger) - - case "MINR": - minrLog = logger - mining.UseLogger(logger) - cpuminer.UseLogger(logger) - - case "PEER": - peerLog = logger - peer.UseLogger(logger) - - case "RPCS": - rpcsLog = logger - - case "SCRP": - scrpLog = logger - txscript.UseLogger(logger) - - case "SRVR": - srvrLog = logger - - case "TXMP": - txmpLog = logger - mempool.UseLogger(logger) - } -} - -// initSeelogLogger initializes a new seelog logger that is used as the backend -// for all logging subsystems. -func initSeelogLogger(logFile string) { - config := ` - - - - - - - - - ` - config = fmt.Sprintf(config, logFile) - - logger, err := seelog.LoggerFromConfigAsString(config) +// 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. +func initLogRotator(logFile string) { + logDir, _ := filepath.Split(logFile) + err := os.MkdirAll(logDir, 0700) if err != nil { - fmt.Fprintf(os.Stderr, "failed to create logger: %v", err) + fmt.Fprintf(os.Stderr, "failed to create log directory: %v\n", err) + os.Exit(1) + } + pr, pw := io.Pipe() + r, err := rotator.New(pr, logFile, 10*1024, false, 3) + if err != nil { + fmt.Fprintf(os.Stderr, "failed to create file rotator: %v\n", err) os.Exit(1) } - backendLog = logger + go r.Run() + + logRotator = r + logRotatorPipe = pw } // setLogLevel sets the logging level for provided subsystem. Invalid @@ -165,17 +140,8 @@ func setLogLevel(subsystemID string, logLevel string) { return } - // Default to info if the log level is invalid. - level, ok := btclog.LogLevelFromString(logLevel) - if !ok { - level = btclog.InfoLvl - } - - // Create new logger for the subsystem if needed. - if logger == btclog.Disabled { - logger = btclog.NewSubsystemLogger(backendLog, subsystemID+": ") - useLogger(subsystemID, logger) - } + // Defaults to info if the log level is invalid. + level, _ := btclog.LevelFromString(logLevel) logger.SetLevel(level) } diff --git a/mining/cpuminer/log.go b/mining/cpuminer/log.go index ff59dbfe..22c4dc72 100644 --- a/mining/cpuminer/log.go +++ b/mining/cpuminer/log.go @@ -5,9 +5,6 @@ package cpuminer import ( - "errors" - "io" - "github.com/btcsuite/btclog" ) @@ -22,37 +19,12 @@ func init() { } // DisableLog disables all library log output. Logging output is disabled -// by default until either UseLogger or SetLogWriter are called. +// by default until UseLogger is called. func DisableLog() { log = btclog.Disabled } // UseLogger uses a specified Logger to output package logging info. -// This should be used in preference to SetLogWriter if the caller is also -// using btclog. func UseLogger(logger btclog.Logger) { log = logger } - -// SetLogWriter uses a specified io.Writer to output package logging info. -// This allows a caller to direct package logging output without needing a -// dependency on seelog. If the caller is also using btclog, UseLogger should -// be used instead. -func SetLogWriter(w io.Writer, level string) error { - if w == nil { - return errors.New("nil writer") - } - - lvl, ok := btclog.LogLevelFromString(level) - if !ok { - return errors.New("invalid log level") - } - - l, err := btclog.NewLoggerFromWriter(w, lvl) - if err != nil { - return err - } - - UseLogger(l) - return nil -} diff --git a/mining/log.go b/mining/log.go index 09aa4024..5e792d9a 100644 --- a/mining/log.go +++ b/mining/log.go @@ -5,9 +5,6 @@ package mining import ( - "errors" - "io" - "github.com/btcsuite/btclog" ) @@ -22,37 +19,12 @@ func init() { } // DisableLog disables all library log output. Logging output is disabled -// by default until either UseLogger or SetLogWriter are called. +// by default until UseLogger is called. func DisableLog() { log = btclog.Disabled } // UseLogger uses a specified Logger to output package logging info. -// This should be used in preference to SetLogWriter if the caller is also -// using btclog. func UseLogger(logger btclog.Logger) { log = logger } - -// SetLogWriter uses a specified io.Writer to output package logging info. -// This allows a caller to direct package logging output without needing a -// dependency on seelog. If the caller is also using btclog, UseLogger should -// be used instead. -func SetLogWriter(w io.Writer, level string) error { - if w == nil { - return errors.New("nil writer") - } - - lvl, ok := btclog.LogLevelFromString(level) - if !ok { - return errors.New("invalid log level") - } - - l, err := btclog.NewLoggerFromWriter(w, lvl) - if err != nil { - return err - } - - UseLogger(l) - return nil -} diff --git a/peer/log.go b/peer/log.go index e37a0bf1..755673f2 100644 --- a/peer/log.go +++ b/peer/log.go @@ -5,9 +5,7 @@ package peer import ( - "errors" "fmt" - "io" "strings" "time" @@ -34,41 +32,16 @@ func init() { } // DisableLog disables all library log output. Logging output is disabled -// by default until either UseLogger or SetLogWriter are called. +// by default until UseLogger is called. func DisableLog() { log = btclog.Disabled } // UseLogger uses a specified Logger to output package logging info. -// This should be used in preference to SetLogWriter if the caller is also -// using btclog. func UseLogger(logger btclog.Logger) { log = logger } -// SetLogWriter uses a specified io.Writer to output package logging info. -// This allows a caller to direct package logging output without needing a -// dependency on seelog. If the caller is also using btclog, UseLogger should -// be used instead. -func SetLogWriter(w io.Writer, level string) error { - if w == nil { - return errors.New("nil writer") - } - - lvl, ok := btclog.LogLevelFromString(level) - if !ok { - return errors.New("invalid log level") - } - - l, err := btclog.NewLoggerFromWriter(w, lvl) - if err != nil { - return err - } - - UseLogger(l) - return nil -} - // LogClosure is a closure that can be printed with %v to be used to // generate expensive-to-create data for a detailed log level and avoid doing // the work if the data isn't printed. diff --git a/peer/log_test.go b/peer/log_test.go deleted file mode 100644 index 39b49388..00000000 --- a/peer/log_test.go +++ /dev/null @@ -1,65 +0,0 @@ -// Copyright (c) 2015 The btcsuite developers Use of this source code is -// governed by an ISC license that can be found in the LICENSE file. - -package peer_test - -import ( - "bytes" - "errors" - "io" - "testing" - - "github.com/btcsuite/btcd/peer" -) - -func TestSetLogWriter(t *testing.T) { - tests := []struct { - name string - w io.Writer - level string - expected error - }{ - { - name: "nil writer", - w: nil, - level: "trace", - expected: errors.New("nil writer"), - }, - { - name: "invalid log level", - w: bytes.NewBuffer(nil), - level: "wrong", - expected: errors.New("invalid log level"), - }, - { - name: "use off level", - w: bytes.NewBuffer(nil), - level: "off", - expected: errors.New("min level can't be greater than max. Got min: 6, max: 5"), - }, - { - name: "pass", - w: bytes.NewBuffer(nil), - level: "debug", - expected: nil, - }, - } - - t.Logf("Running %d tests", len(tests)) - for i, test := range tests { - err := peer.SetLogWriter(test.w, test.level) - if err != nil { - if err.Error() != test.expected.Error() { - t.Errorf("SetLogWriter #%d (%s) wrong result\n"+ - "got: %v\nwant: %v", i, test.name, err, - test.expected) - } - } else { - if test.expected != nil { - t.Errorf("SetLogWriter #%d (%s) wrong result\n"+ - "got: %v\nwant: %v", i, test.name, err, - test.expected) - } - } - } -} diff --git a/rpcserver.go b/rpcserver.go index 4b7fbbf7..46fbe35e 100644 --- a/rpcserver.go +++ b/rpcserver.go @@ -2039,11 +2039,11 @@ func handleGetBlockTemplateProposal(s *rpcServer, request *btcjson.TemplateReque isOrphan, err := s.server.blockManager.ProcessBlock(block, flags) if err != nil { if _, ok := err.(blockchain.RuleError); !ok { - err := rpcsLog.Errorf("Failed to process block "+ - "proposal: %v", err) + errStr := fmt.Sprintf("Failed to process block proposal: %v", err) + rpcsLog.Error(errStr) return nil, &btcjson.RPCError{ Code: btcjson.ErrRPCVerify, - Message: err.Error(), + Message: errStr, } } diff --git a/txscript/log.go b/txscript/log.go index f63da728..86ab4088 100644 --- a/txscript/log.go +++ b/txscript/log.go @@ -5,9 +5,6 @@ package txscript import ( - "errors" - "io" - "github.com/btcsuite/btclog" ) @@ -22,41 +19,16 @@ func init() { } // DisableLog disables all library log output. Logging output is disabled -// by default until either UseLogger or SetLogWriter are called. +// by default until UseLogger is called. func DisableLog() { log = btclog.Disabled } // UseLogger uses a specified Logger to output package logging info. -// This should be used in preference to SetLogWriter if the caller is also -// using btclog. func UseLogger(logger btclog.Logger) { log = logger } -// SetLogWriter uses a specified io.Writer to output package logging info. -// This allows a caller to direct package logging output without needing a -// dependency on seelog. If the caller is also using btclog, UseLogger should -// be used instead. -func SetLogWriter(w io.Writer, level string) error { - if w == nil { - return errors.New("nil writer") - } - - lvl, ok := btclog.LogLevelFromString(level) - if !ok { - return errors.New("invalid log level") - } - - l, err := btclog.NewLoggerFromWriter(w, lvl) - if err != nil { - return err - } - - UseLogger(l) - return nil -} - // LogClosure is a closure that can be printed with %v to be used to // generate expensive-to-create data for a detailed log level and avoid doing // the work if the data isn't printed. diff --git a/txscript/log_test.go b/txscript/log_test.go deleted file mode 100644 index 11baa08f..00000000 --- a/txscript/log_test.go +++ /dev/null @@ -1,64 +0,0 @@ -// Copyright (c) 2013-2016 The btcsuite developers -// Use of this source code is governed by an ISC -// license that can be found in the LICENSE file. - -package txscript - -import ( - "errors" - "io" - "os" - "testing" -) - -func TestSetLogWriter(t *testing.T) { - tests := []struct { - name string - w io.Writer - level string - expected error - }{ - { - name: "nil writer", - w: nil, - level: "trace", - expected: errors.New("nil writer"), - }, - { - name: "invalid log level", - w: os.Stdout, - level: "wrong", - expected: errors.New("invalid log level"), - }, - { - name: "use off level", - w: os.Stdout, - level: "off", - expected: errors.New("min level can't be greater than max. Got min: 6, max: 5"), - }, - { - name: "pass", - w: os.Stdout, - level: "debug", - expected: nil, - }, - } - - t.Logf("Running %d tests", len(tests)) - for i, test := range tests { - err := SetLogWriter(test.w, test.level) - if err != nil { - if err.Error() != test.expected.Error() { - t.Errorf("SetLogWriter #%d (%s) wrong result\n"+ - "got: %v\nwant: %v", i, test.name, err, - test.expected) - } - } else { - if test.expected != nil { - t.Errorf("SetLogWriter #%d (%s) wrong result\n"+ - "got: %v\nwant: %v", i, test.name, err, - test.expected) - } - } - } -}