diff --git a/Makefile b/Makefile index 2c7b4aa2..3efde731 100644 --- a/Makefile +++ b/Makefile @@ -22,8 +22,8 @@ HAVE_LINTER := $(shell command -v $(LINT_BIN) 2> /dev/null) BTCD_DIR :=${GOPATH}/src/$(BTCD_PKG) -COMMIT := $(shell git rev-parse HEAD) -LDFLAGS := -ldflags "-X main.Commit=$(COMMIT)" +COMMIT := $(shell git describe --abbrev=40 --dirty --broken) +LDFLAGS := -ldflags "-X $(PKG)/build.Commit=$(COMMIT)" GLIDE_COMMIT := 84607742b10f492430762d038e954236bbaf23f7 BTCD_COMMIT := $(shell cat Gopkg.toml | \ @@ -47,8 +47,11 @@ XARGS := xargs -L 1 include make/testing_flags.mk +DEV_TAGS := $(if ${tags},$(DEV_TAGS) ${tags},$(DEV_TAGS)) +PROD_TAGS := $(if ${tags},$(PROD_TAGS) ${tags},$(PROD_TAGS)) + COVER = for dir in $(GOLISTCOVER); do \ - $(GOTEST) -tags="$(TEST_TAGS)" \ + $(GOTEST) -tags="$(DEV_TAGS) $(LOG_TAGS)" \ -covermode=count \ -coverprofile=$$dir/profile.tmp $$dir; \ \ @@ -130,13 +133,13 @@ btcd: $(GLIDE_BIN) $(BTCD_DIR) build: @$(call print, "Building debug lnd and lncli.") - $(GOBUILD) -tags="$(TEST_TAGS) ${tags}" -o lnd-debug $(LDFLAGS) $(PKG) - $(GOBUILD) -tags="$(TEST_TAGS) ${tags}" -o lncli-debug $(LDFLAGS) $(PKG)/cmd/lncli + $(GOBUILD) -tags="$(DEV_TAGS)" -o lnd-debug $(LDFLAGS) $(PKG) + $(GOBUILD) -tags="$(DEV_TAGS)" -o lncli-debug $(LDFLAGS) $(PKG)/cmd/lncli install: @$(call print, "Installing lnd and lncli.") - go install -v -tags="${tags}" $(LDFLAGS) $(PKG) - go install -v -tags="${tags}" $(LDFLAGS) $(PKG)/cmd/lncli + go install -v -tags="$(PROD_TAGS)" $(LDFLAGS) $(PKG) + go install -v -tags="$(PROD_TAGS)" $(LDFLAGS) $(PKG)/cmd/lncli scratch: dep build diff --git a/autopilot/log.go b/autopilot/log.go index 70f49947..34e53c84 100644 --- a/autopilot/log.go +++ b/autopilot/log.go @@ -1,6 +1,9 @@ package autopilot -import "github.com/btcsuite/btclog" +import ( + "github.com/btcsuite/btclog" + "github.com/lightningnetwork/lnd/build" +) // log is a logger that is initialized with no output filters. This // means the package will not perform any logging by default until the caller @@ -9,13 +12,13 @@ var log btclog.Logger // The default amount of logging is none. func init() { - DisableLog() + UseLogger(build.NewSubLogger("ATPL", nil)) } // DisableLog disables all library log output. Logging output is disabled // by default until UseLogger is called. func DisableLog() { - log = btclog.Disabled + UseLogger(btclog.Disabled) } // UseLogger uses a specified Logger to output package logging info. diff --git a/breacharbiter.go b/breacharbiter.go index 07fb5b25..df19bff1 100644 --- a/breacharbiter.go +++ b/breacharbiter.go @@ -15,6 +15,7 @@ import ( "github.com/btcsuite/btcutil" "github.com/coreos/bbolt" "github.com/davecgh/go-spew/spew" + "github.com/lightningnetwork/lnd/chainntnfs" "github.com/lightningnetwork/lnd/channeldb" "github.com/lightningnetwork/lnd/htlcswitch" diff --git a/breacharbiter_test.go b/breacharbiter_test.go index 8f6c90a3..31fc5a04 100644 --- a/breacharbiter_test.go +++ b/breacharbiter_test.go @@ -22,7 +22,6 @@ import ( "github.com/btcsuite/btcd/chaincfg/chainhash" "github.com/btcsuite/btcd/txscript" "github.com/btcsuite/btcd/wire" - "github.com/btcsuite/btclog" "github.com/btcsuite/btcutil" "github.com/go-errors/errors" "github.com/lightningnetwork/lnd/chainntnfs" @@ -268,10 +267,6 @@ var ( ) func init() { - channeldb.UseLogger(btclog.Disabled) - lnwallet.UseLogger(btclog.Disabled) - brarLog = btclog.Disabled - // Ensure that breached outputs are initialized before starting tests. if err := initBreachedOutputs(); err != nil { panic(err) diff --git a/build/deployment.go b/build/deployment.go new file mode 100644 index 00000000..410f7e96 --- /dev/null +++ b/build/deployment.go @@ -0,0 +1,36 @@ +package build + +// DeploymentType is an enum specifying the deployment to compile. +type DeploymentType byte + +const ( + // Development is a deployment that includes extra testing hooks and + // logging configurations. + Development DeploymentType = iota + + // Production is a deployment that strips out testing logic and uses + // Default logging. + Production +) + +// String returns a human readable name for a build type. +func (b DeploymentType) String() string { + switch b { + case Development: + return "development" + case Production: + return "production" + default: + return "unknown" + } +} + +// IsProdBuild returns true if this is a production build. +func IsProdBuild() bool { + return Deployment == Production +} + +// IsDevBuild returns true if this is a development build. +func IsDevBuild() bool { + return Deployment == Development +} diff --git a/build/deployment_dev.go b/build/deployment_dev.go new file mode 100644 index 00000000..fb2bb2b9 --- /dev/null +++ b/build/deployment_dev.go @@ -0,0 +1,6 @@ +// +build dev + +package build + +// Deployment specifies a development build. +const Deployment = Development diff --git a/build/deployment_prod.go b/build/deployment_prod.go new file mode 100644 index 00000000..247f25ae --- /dev/null +++ b/build/deployment_prod.go @@ -0,0 +1,6 @@ +// +build !dev + +package build + +// Deployment specifies a production build. +const Deployment = Production diff --git a/build/log.go b/build/log.go new file mode 100644 index 00000000..31be5d4d --- /dev/null +++ b/build/log.go @@ -0,0 +1,95 @@ +package build + +import ( + "io" + + "github.com/btcsuite/btclog" +) + +// LogType is an indicating the type of logging specified by the build flag. +type LogType byte + +const ( + // LogTypeNone indicates no logging. + LogTypeNone LogType = iota + + // LogTypeStdOut all logging is written directly to stdout. + LogTypeStdOut + + // LogTypeDefault logs to both stdout and a given io.PipeWriter. + LogTypeDefault +) + +// String returns a human readable identifier for the logging type. +func (t LogType) String() string { + switch t { + case LogTypeNone: + return "none" + case LogTypeStdOut: + return "stdout" + case LogTypeDefault: + return "default" + default: + return "unknown" + } +} + +// LogWriter is a stub type whose behavior can be changed using the build flags +// "stdlog" and "nolog". The default behavior is to write to both stdout and the +// RotatorPipe. Passing "stdlog" will cause it only to write to stdout, and +// "nolog" implements Write as a no-op. +type LogWriter struct { + // RotatorPipe is the write-end pipe for writing to the log rotator. It + // is written to by the Write method of the LogWriter type. This only + // needs to be set if neither the stdlog or nolog builds are set. + RotatorPipe *io.PipeWriter +} + +// NewSubLogger constructs a new subsystem log from the current LogWriter +// implementation. This is primarily intended for use with stdlog, as the actual +// writer is shared amongst all instantiations. +func NewSubLogger(subsystem string, + genSubLogger func(string) btclog.Logger) btclog.Logger { + + switch Deployment { + + // For production builds, generate a new subsystem logger from the + // primary log backend. If no function is provided, logging will be + // disabled. + case Production: + if genSubLogger != nil { + return genSubLogger(subsystem) + } + + // For development builds, we must handle two distinct types of logging: + // unit tests and running the live daemon, e.g. for integration testing. + case Development: + switch LoggingType { + + // Default logging is used when running the standalone daemon. + // We'll use the optional sublogger constructor to mimic the + // production behavior. + case LogTypeDefault: + if genSubLogger != nil { + return genSubLogger(subsystem) + } + + // Logging to stdout is used in unit tests. It is not important + // that they share the same backend, since all output is written + // to std out. + case LogTypeStdOut: + backend := btclog.NewBackend(&LogWriter{}) + logger := backend.Logger(subsystem) + + // Set the logging level of the stdout logger to use the + // configured logging level specified by build flags. + level, _ := btclog.LevelFromString(LogLevel) + logger.SetLevel(level) + + return logger + } + } + + // For any other configurations, we'll disable logging. + return btclog.Disabled +} diff --git a/build/log_default.go b/build/log_default.go new file mode 100644 index 00000000..399a00df --- /dev/null +++ b/build/log_default.go @@ -0,0 +1,18 @@ +// +build !stdlog,!nolog + +package build + +import "os" + +// LoggingType is a log type that writes to both stdout and the log rotator, if +// present. +const LoggingType = LogTypeDefault + +// Write writes the byte slice to both stdout and the log rotator, if present. +func (w *LogWriter) Write(b []byte) (int, error) { + os.Stdout.Write(b) + if w.RotatorPipe != nil { + w.RotatorPipe.Write(b) + } + return len(b), nil +} diff --git a/build/log_nolog.go b/build/log_nolog.go new file mode 100644 index 00000000..0aa15bc4 --- /dev/null +++ b/build/log_nolog.go @@ -0,0 +1,11 @@ +// +build nolog + +package build + +// LoggingType is a log type that writes no logs. +const LoggingType = LogTypeNone + +// Write is a noop. +func (w *LogWriter) Write(b []byte) (int, error) { + return len(b), nil +} diff --git a/build/log_stdlog.go b/build/log_stdlog.go new file mode 100644 index 00000000..a495eaec --- /dev/null +++ b/build/log_stdlog.go @@ -0,0 +1,14 @@ +// +build stdlog + +package build + +import "os" + +// LoggingType is a log type that only writes to stdout. +const LoggingType = LogTypeStdOut + +// Write writes the provided byte slice to stdout. +func (w *LogWriter) Write(b []byte) (int, error) { + os.Stdout.Write(b) + return len(b), nil +} diff --git a/build/loglevel_critical.go b/build/loglevel_critical.go new file mode 100644 index 00000000..2e3a3734 --- /dev/null +++ b/build/loglevel_critical.go @@ -0,0 +1,6 @@ +// +build dev,critical + +package build + +// LogLevel specifies a critical log level. +var LogLevel = "critical" diff --git a/build/loglevel_debug.go b/build/loglevel_debug.go new file mode 100644 index 00000000..7d22c90a --- /dev/null +++ b/build/loglevel_debug.go @@ -0,0 +1,6 @@ +// +build dev,debug + +package build + +// LogLevel specifies a debug log level. +var LogLevel = "debug" diff --git a/build/loglevel_default.go b/build/loglevel_default.go new file mode 100644 index 00000000..5e12ffcc --- /dev/null +++ b/build/loglevel_default.go @@ -0,0 +1,6 @@ +// +build !info,!debug,!trace,!warn,!error,!critical,!off + +package build + +// LogLevel specifies a default log level of info. +var LogLevel = "info" diff --git a/build/loglevel_error.go b/build/loglevel_error.go new file mode 100644 index 00000000..81e3849e --- /dev/null +++ b/build/loglevel_error.go @@ -0,0 +1,6 @@ +// +build dev,error + +package build + +// LogLevel specifies an error log level. +var LogLevel = "error" diff --git a/build/loglevel_info.go b/build/loglevel_info.go new file mode 100644 index 00000000..f0d6dca0 --- /dev/null +++ b/build/loglevel_info.go @@ -0,0 +1,6 @@ +// +build dev,info + +package build + +// LogLevel specifies an info log level. +var LogLevel = "info" diff --git a/build/loglevel_off.go b/build/loglevel_off.go new file mode 100644 index 00000000..d6c65e77 --- /dev/null +++ b/build/loglevel_off.go @@ -0,0 +1,6 @@ +// +build dev,off + +package build + +// LogLevel specifies an off log level. +var LogLevel = "off" diff --git a/build/loglevel_trace.go b/build/loglevel_trace.go new file mode 100644 index 00000000..ec17e9dd --- /dev/null +++ b/build/loglevel_trace.go @@ -0,0 +1,6 @@ +// +build dev,trace + +package build + +// LogLevel specifies a trace log level. +var LogLevel = "trace" diff --git a/build/loglevel_warn.go b/build/loglevel_warn.go new file mode 100644 index 00000000..a8fdfc29 --- /dev/null +++ b/build/loglevel_warn.go @@ -0,0 +1,6 @@ +// +build dev,warn + +package build + +// LogLevel specifies a warning log level. +var LogLevel = "warn" diff --git a/version.go b/build/version.go similarity index 72% rename from version.go rename to build/version.go index 69c6cc43..c535a649 100644 --- a/version.go +++ b/build/version.go @@ -3,7 +3,7 @@ // Heavily inspired by https://github.com/btcsuite/btcd/blob/master/version.go // Copyright (C) 2015-2017 The Lightning Network Developers -package main +package build import ( "bytes" @@ -11,6 +11,10 @@ import ( "strings" ) +// Commit stores the current commit hash of this build, this should be set using +// the -ldflags during compilation. +var Commit string + // semanticAlphabet const semanticAlphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-" @@ -26,14 +30,9 @@ const ( appPreRelease = "beta" ) -// appBuild is defined as a variable so it can be overridden during the build -// process with '-ldflags "-X main.appBuild foo' if needed. It MUST only -// contain characters from semanticAlphabet per the semantic versioning spec. -var appBuild string - -// version returns the application version as a properly formed string per the +// Version returns the application version as a properly formed string per the // semantic versioning 2.0.0 spec (http://semver.org/). -func version() string { +func Version() string { // Start with the major, minor, and patch versions. version := fmt.Sprintf("%d.%d.%d", appMajor, appMinor, appPatch) @@ -46,15 +45,6 @@ func version() string { version = fmt.Sprintf("%s-%s", version, preRelease) } - // Append build metadata if there is any. The plus called for - // by the semantic versioning spec is automatically appended and should - // not be contained in the build metadata string. The build metadata - // string is not appended if it contains invalid characters. - build := normalizeVerString(appBuild) - if build != "" { - version = fmt.Sprintf("%s+%s", version, build) - } - // Append commit hash of current build to version. version = fmt.Sprintf("%s commit=%s", version, Commit) diff --git a/chainntnfs/bitcoindnotify/bitcoind_debug.go b/chainntnfs/bitcoindnotify/bitcoind_dev.go similarity index 99% rename from chainntnfs/bitcoindnotify/bitcoind_debug.go rename to chainntnfs/bitcoindnotify/bitcoind_dev.go index 04cd030c..f45cda09 100644 --- a/chainntnfs/bitcoindnotify/bitcoind_debug.go +++ b/chainntnfs/bitcoindnotify/bitcoind_dev.go @@ -1,4 +1,4 @@ -// +build debug +// +build dev package bitcoindnotify diff --git a/chainntnfs/bitcoindnotify/bitcoind_test.go b/chainntnfs/bitcoindnotify/bitcoind_test.go index e65945b0..8f731f38 100644 --- a/chainntnfs/bitcoindnotify/bitcoind_test.go +++ b/chainntnfs/bitcoindnotify/bitcoind_test.go @@ -1,4 +1,4 @@ -// +build debug +// +build dev package bitcoindnotify diff --git a/chainntnfs/btcdnotify/btcd_debug.go b/chainntnfs/btcdnotify/btcd_dev.go similarity index 99% rename from chainntnfs/btcdnotify/btcd_debug.go rename to chainntnfs/btcdnotify/btcd_dev.go index 1c91d3f6..af64dad8 100644 --- a/chainntnfs/btcdnotify/btcd_debug.go +++ b/chainntnfs/btcdnotify/btcd_dev.go @@ -1,4 +1,4 @@ -// +build debug +// +build dev package btcdnotify diff --git a/chainntnfs/btcdnotify/btcd_test.go b/chainntnfs/btcdnotify/btcd_test.go index 239e0d9d..6ef564d0 100644 --- a/chainntnfs/btcdnotify/btcd_test.go +++ b/chainntnfs/btcdnotify/btcd_test.go @@ -1,4 +1,4 @@ -// +build debug +// +build dev package btcdnotify diff --git a/chainntnfs/interface_debug.go b/chainntnfs/interface_dev.go similarity index 96% rename from chainntnfs/interface_debug.go rename to chainntnfs/interface_dev.go index aeb3461b..ac00fa8d 100644 --- a/chainntnfs/interface_debug.go +++ b/chainntnfs/interface_dev.go @@ -1,4 +1,4 @@ -// +build debug +// +build dev package chainntnfs diff --git a/chainntnfs/interface_test.go b/chainntnfs/interface_test.go index f51d912b..36006dcc 100644 --- a/chainntnfs/interface_test.go +++ b/chainntnfs/interface_test.go @@ -1,4 +1,4 @@ -// +build debug +// +build dev package chainntnfs_test diff --git a/chainntnfs/log.go b/chainntnfs/log.go index b6a3f148..f2124605 100644 --- a/chainntnfs/log.go +++ b/chainntnfs/log.go @@ -1,6 +1,9 @@ package chainntnfs -import "github.com/btcsuite/btclog" +import ( + "github.com/btcsuite/btclog" + "github.com/lightningnetwork/lnd/build" +) // Log is a logger that is initialized with no output filters. This // means the package will not perform any logging by default until the caller @@ -9,13 +12,13 @@ var Log btclog.Logger // The default amount of logging is none. func init() { - DisableLog() + UseLogger(build.NewSubLogger("NTFN", nil)) } // DisableLog disables all library log output. Logging output is disabled // by default until UseLogger is called. func DisableLog() { - Log = btclog.Disabled + UseLogger(btclog.Disabled) } // UseLogger uses a specified Logger to output package logging info. diff --git a/chainntnfs/neutrinonotify/neutrino_debug.go b/chainntnfs/neutrinonotify/neutrino_dev.go similarity index 99% rename from chainntnfs/neutrinonotify/neutrino_debug.go rename to chainntnfs/neutrinonotify/neutrino_dev.go index 19085c38..635f0d95 100644 --- a/chainntnfs/neutrinonotify/neutrino_debug.go +++ b/chainntnfs/neutrinonotify/neutrino_dev.go @@ -1,4 +1,4 @@ -// +build debug +// +build dev package neutrinonotify diff --git a/chainntnfs/test_utils.go b/chainntnfs/test_utils.go index 44c844d0..bc3dae05 100644 --- a/chainntnfs/test_utils.go +++ b/chainntnfs/test_utils.go @@ -1,4 +1,4 @@ -// +build debug +// +build dev package chainntnfs diff --git a/channeldb/log.go b/channeldb/log.go index d44192e7..e0158d45 100644 --- a/channeldb/log.go +++ b/channeldb/log.go @@ -1,21 +1,23 @@ package channeldb -import "github.com/btcsuite/btclog" +import ( + "github.com/btcsuite/btclog" + "github.com/lightningnetwork/lnd/build" +) // log is a logger that is initialized with no output filters. This // means the package will not perform any logging by default until the caller // requests it. var log btclog.Logger -// The default amount of logging is none. func init() { - DisableLog() + UseLogger(build.NewSubLogger("CHDB", nil)) } // DisableLog disables all library log output. Logging output is disabled // by default until UseLogger is called. func DisableLog() { - log = btclog.Disabled + UseLogger(btclog.Disabled) } // UseLogger uses a specified Logger to output package logging info. diff --git a/cmd/lncli/main.go b/cmd/lncli/main.go index 7a698f07..3588ba57 100644 --- a/cmd/lncli/main.go +++ b/cmd/lncli/main.go @@ -15,6 +15,7 @@ import ( macaroon "gopkg.in/macaroon.v2" "github.com/btcsuite/btcutil" + "github.com/lightningnetwork/lnd/build" "github.com/lightningnetwork/lnd/lncfg" "github.com/lightningnetwork/lnd/lnrpc" "github.com/lightningnetwork/lnd/macaroons" @@ -34,10 +35,6 @@ const ( ) var ( - // Commit stores the current commit hash of this build. This should be - // set using -ldflags during compilation. - Commit string - defaultLndDir = btcutil.AppDataDir("lnd", false) defaultTLSCertPath = filepath.Join(defaultLndDir, defaultTLSCertFilename) ) @@ -205,7 +202,7 @@ func extractPathArgs(ctx *cli.Context) (string, string, error) { func main() { app := cli.NewApp() app.Name = "lncli" - app.Version = fmt.Sprintf("%s commit=%s", "0.5", Commit) + app.Version = build.Version() app.Usage = "control plane for your Lightning Network Daemon (lnd)" app.Flags = []cli.Flag{ cli.StringFlag{ diff --git a/config.go b/config.go index f39099e1..26e4c522 100644 --- a/config.go +++ b/config.go @@ -21,6 +21,7 @@ import ( "github.com/btcsuite/btcutil" flags "github.com/jessevdk/go-flags" + "github.com/lightningnetwork/lnd/build" "github.com/lightningnetwork/lnd/htlcswitch/hodl" "github.com/lightningnetwork/lnd/lncfg" "github.com/lightningnetwork/lnd/lnwire" @@ -323,7 +324,7 @@ func loadConfig() (*config, error) { appName = strings.TrimSuffix(appName, filepath.Ext(appName)) usageMessage := fmt.Sprintf("Use %s -h to show usage", appName) if preCfg.ShowVersion { - fmt.Println(appName, "version", version()) + fmt.Println(appName, "version", build.Version()) os.Exit(0) } diff --git a/config_debug.go b/config_debug.go deleted file mode 100644 index 6c9fd714..00000000 --- a/config_debug.go +++ /dev/null @@ -1,6 +0,0 @@ -// +build debug - -package main - -// DebugBuild signals that this is a debug build. -const DebugBuild = true diff --git a/config_production.go b/config_production.go deleted file mode 100644 index 59b986bf..00000000 --- a/config_production.go +++ /dev/null @@ -1,6 +0,0 @@ -// +build !debug - -package main - -// DebugBuild signals that this is a debug build. -const DebugBuild = false diff --git a/contractcourt/log.go b/contractcourt/log.go index 0168177c..e8b70719 100644 --- a/contractcourt/log.go +++ b/contractcourt/log.go @@ -1,6 +1,9 @@ package contractcourt -import "github.com/btcsuite/btclog" +import ( + "github.com/btcsuite/btclog" + "github.com/lightningnetwork/lnd/build" +) // log is a logger that is initialized with no output filters. This // means the package will not perform any logging by default until the caller @@ -9,13 +12,13 @@ var log btclog.Logger // The default amount of logging is none. func init() { - DisableLog() + UseLogger(build.NewSubLogger("CNCT", nil)) } // DisableLog disables all library log output. Logging output is disabled // by default until UseLogger is called. func DisableLog() { - log = btclog.Disabled + UseLogger(btclog.Disabled) } // UseLogger uses a specified Logger to output package logging info. diff --git a/discovery/log.go b/discovery/log.go index 7d38664c..bd2004d3 100644 --- a/discovery/log.go +++ b/discovery/log.go @@ -1,6 +1,9 @@ package discovery -import "github.com/btcsuite/btclog" +import ( + "github.com/btcsuite/btclog" + "github.com/lightningnetwork/lnd/build" +) // log is a logger that is initialized with no output filters. This // means the package will not perform any logging by default until the caller @@ -9,13 +12,13 @@ var log btclog.Logger // The default amount of logging is none. func init() { - DisableLog() + UseLogger(build.NewSubLogger("DISC", nil)) } // DisableLog disables all library log output. Logging output is disabled // by default until UseLogger is called. func DisableLog() { - log = btclog.Disabled + UseLogger(btclog.Disabled) } // UseLogger uses a specified Logger to output package logging info. diff --git a/docs/MAKEFILE.md b/docs/MAKEFILE.md index b7111b93..c7c60ecc 100644 --- a/docs/MAKEFILE.md +++ b/docs/MAKEFILE.md @@ -160,6 +160,8 @@ Arguments: - `pkg=` - `case=` - `timeout=` +- `log="stdlog[ ]"` prints logs to stdout + - `` can be `info` (default), `debug`, `trace`, `warn`, `error`, `critical`, or `off` `unit-cover` ------------ @@ -170,6 +172,8 @@ Arguments: - `pkg=` - `case=` - `timeout=` +- `log="stdlog[ ]"` prints logs to stdout + - `` can be `info` (default), `debug`, `trace`, `warn`, `error`, `critical`, or `off` Related: [`unit`](#unit) @@ -181,6 +185,8 @@ Arguments: - `pkg=` - `case=` - `timeout=` +- `log="stdlog[ ]"` prints logs to stdout + - `` can be `info` (default), `debug`, `trace`, `warn`, `error`, `critical`, or `off` Related: [`unit`](#unit) diff --git a/fundingmanager.go b/fundingmanager.go index 55622e40..0acfadcf 100644 --- a/fundingmanager.go +++ b/fundingmanager.go @@ -15,6 +15,9 @@ import ( "github.com/coreos/bbolt" "github.com/davecgh/go-spew/spew" "github.com/go-errors/errors" + "golang.org/x/crypto/salsa20" + "google.golang.org/grpc" + "github.com/lightningnetwork/lnd/chainntnfs" "github.com/lightningnetwork/lnd/channeldb" "github.com/lightningnetwork/lnd/htlcswitch" @@ -24,8 +27,6 @@ import ( "github.com/lightningnetwork/lnd/lnwallet" "github.com/lightningnetwork/lnd/lnwire" "github.com/lightningnetwork/lnd/routing" - "golang.org/x/crypto/salsa20" - "google.golang.org/grpc" ) const ( diff --git a/fundingmanager_test.go b/fundingmanager_test.go index c1421575..2cfd8097 100644 --- a/fundingmanager_test.go +++ b/fundingmanager_test.go @@ -18,12 +18,10 @@ import ( "github.com/btcsuite/btcd/chaincfg" "github.com/btcsuite/btcd/chaincfg/chainhash" "github.com/btcsuite/btcd/wire" - "github.com/btcsuite/btclog" "github.com/btcsuite/btcutil" - _ "github.com/btcsuite/btcwallet/walletdb/bdb" + "github.com/lightningnetwork/lnd/chainntnfs" "github.com/lightningnetwork/lnd/channeldb" - "github.com/lightningnetwork/lnd/contractcourt" "github.com/lightningnetwork/lnd/htlcswitch" "github.com/lightningnetwork/lnd/keychain" "github.com/lightningnetwork/lnd/lnpeer" @@ -201,13 +199,6 @@ func (n *testNode) AddNewChannel(channel *channeldb.OpenChannel, } } -func init() { - channeldb.UseLogger(btclog.Disabled) - lnwallet.UseLogger(btclog.Disabled) - contractcourt.UseLogger(btclog.Disabled) - fndgLog = btclog.Disabled -} - func createTestWallet(cdb *channeldb.DB, netParams *chaincfg.Params, notifier chainntnfs.ChainNotifier, wc lnwallet.WalletController, signer lnwallet.Signer, keyRing keychain.SecretKeyRing, diff --git a/htlcswitch/hodl/config_debug.go b/htlcswitch/hodl/config_dev.go similarity index 96% rename from htlcswitch/hodl/config_debug.go rename to htlcswitch/hodl/config_dev.go index 957eb50d..ef8389ad 100644 --- a/htlcswitch/hodl/config_debug.go +++ b/htlcswitch/hodl/config_dev.go @@ -1,4 +1,4 @@ -// +build debug +// +build dev package hodl @@ -61,7 +61,7 @@ func (c *Config) Mask() Mask { } // NOTE: The value returned here will only honor the configuration if - // the debug build flag is present. In production, this method always + // the dev build flag is present. In production, this method always // returns hodl.MaskNone and Active(*) always returns false. return MaskFromFlags(flags...) } diff --git a/htlcswitch/hodl/config_production.go b/htlcswitch/hodl/config_prod.go similarity index 92% rename from htlcswitch/hodl/config_production.go rename to htlcswitch/hodl/config_prod.go index 8daeefd8..c5e9e934 100644 --- a/htlcswitch/hodl/config_production.go +++ b/htlcswitch/hodl/config_prod.go @@ -1,4 +1,4 @@ -// +build !debug +// +build !dev package hodl diff --git a/htlcswitch/hodl/mask_debug.go b/htlcswitch/hodl/mask_dev.go similarity index 89% rename from htlcswitch/hodl/mask_debug.go rename to htlcswitch/hodl/mask_dev.go index 5d5fd065..a1d50ff0 100644 --- a/htlcswitch/hodl/mask_debug.go +++ b/htlcswitch/hodl/mask_dev.go @@ -1,4 +1,4 @@ -// +build debug +// +build dev package hodl @@ -7,9 +7,6 @@ import ( "strings" ) -// DebugBuild signals that this is a debug build. -const DebugBuild = true - // MaskFromFlags merges a variadic set of Flags into a single Mask. func MaskFromFlags(flags ...Flag) Mask { var mask Mask diff --git a/htlcswitch/hodl/mask_production.go b/htlcswitch/hodl/mask_prod.go similarity index 78% rename from htlcswitch/hodl/mask_production.go rename to htlcswitch/hodl/mask_prod.go index c2db04ef..1b8a4b70 100644 --- a/htlcswitch/hodl/mask_production.go +++ b/htlcswitch/hodl/mask_prod.go @@ -1,10 +1,7 @@ -// +build !debug +// +build !dev package hodl -// DebugBuild signals that this is a production build. -const DebugBuild = false - // MaskFromFlags in production always returns MaskNone. func MaskFromFlags(_ ...Flag) Mask { return MaskNone diff --git a/htlcswitch/hodl/mask_test.go b/htlcswitch/hodl/mask_test.go index cf29d19b..07ba6e33 100644 --- a/htlcswitch/hodl/mask_test.go +++ b/htlcswitch/hodl/mask_test.go @@ -3,6 +3,7 @@ package hodl_test import ( "testing" + "github.com/lightningnetwork/lnd/build" "github.com/lightningnetwork/lnd/htlcswitch/hodl" ) @@ -87,8 +88,8 @@ var hodlMaskTests = []struct { // correctly reports active for flags in the tests' expected flags, and inactive // for all others. func TestMask(t *testing.T) { - if !hodl.DebugBuild { - t.Fatalf("htlcswitch tests must be run with '-tags debug'") + if !build.IsDevBuild() { + t.Fatalf("htlcswitch tests must be run with '-tags=dev'") } for i, test := range hodlMaskTests { diff --git a/htlcswitch/log.go b/htlcswitch/log.go index bd4593a3..63fe63a1 100644 --- a/htlcswitch/log.go +++ b/htlcswitch/log.go @@ -1,6 +1,9 @@ package htlcswitch -import "github.com/btcsuite/btclog" +import ( + "github.com/btcsuite/btclog" + "github.com/lightningnetwork/lnd/build" +) // log is a logger that is initialized with no output filters. This // means the package will not perform any logging by default until the caller @@ -9,13 +12,13 @@ var log btclog.Logger // The default amount of logging is none. func init() { - DisableLog() + UseLogger(build.NewSubLogger("HSWC", nil)) } // DisableLog disables all library log output. Logging output is disabled // by default until UseLogger is called. func DisableLog() { - log = btclog.Disabled + UseLogger(btclog.Disabled) } // UseLogger uses a specified Logger to output package logging info. diff --git a/lnd.go b/lnd.go index e323e95d..93b58ef1 100644 --- a/lnd.go +++ b/lnd.go @@ -37,6 +37,8 @@ import ( "github.com/btcsuite/btcwallet/wallet" proxy "github.com/grpc-ecosystem/grpc-gateway/runtime" flags "github.com/jessevdk/go-flags" + + "github.com/lightningnetwork/lnd/build" "github.com/lightningnetwork/lnd/channeldb" "github.com/lightningnetwork/lnd/keychain" "github.com/lightningnetwork/lnd/lncfg" @@ -54,10 +56,6 @@ const ( ) var ( - // Commit stores the current commit hash of this build. This should be - // set using -ldflags during compilation. - Commit string - cfg *config registeredChains = newChainRegistry() @@ -93,12 +91,6 @@ var ( // defers created in the top-level scope of a main method aren't executed if // os.Exit() is called. func lndMain() error { - defer func() { - if logRotatorPipe != nil { - ltndLog.Info("Shutdown complete") - } - }() - // Load the configuration, and parse any command line options. This // function will also set up logging properly. loadedConfig, err := loadConfig() @@ -108,12 +100,14 @@ func lndMain() error { cfg = loadedConfig defer func() { if logRotator != nil { + ltndLog.Info("Shutdown complete") logRotator.Close() } }() // Show version at startup. - ltndLog.Infof("Version %s", version()) + ltndLog.Infof("Version: %s, build=%s, logging=%s", + build.Version(), build.Deployment, build.LoggingType) var network string switch { diff --git a/lnwallet/log.go b/lnwallet/log.go index f36aa99f..97f1c21f 100644 --- a/lnwallet/log.go +++ b/lnwallet/log.go @@ -5,6 +5,8 @@ import ( "github.com/btcsuite/btcwallet/chain" btcwallet "github.com/btcsuite/btcwallet/wallet" "github.com/btcsuite/btcwallet/wtxmgr" + + "github.com/lightningnetwork/lnd/build" ) // walletLog is a logger that is initialized with no output filters. This @@ -14,13 +16,13 @@ var walletLog btclog.Logger // The default amount of logging is none. func init() { - DisableLog() + UseLogger(build.NewSubLogger("LNWL", nil)) } // DisableLog disables all library log output. Logging output is disabled // by default until UseLogger is called. func DisableLog() { - walletLog = btclog.Disabled + UseLogger(btclog.Disabled) } // UseLogger uses a specified Logger to output package logging info. diff --git a/log.go b/log.go index 01ba080f..1fe1b658 100644 --- a/log.go +++ b/log.go @@ -12,8 +12,10 @@ import ( "github.com/btcsuite/btclog" "github.com/jrick/logrotate/rotator" "github.com/lightninglabs/neutrino" + "github.com/lightningnetwork/lightning-onion" "github.com/lightningnetwork/lnd/autopilot" + "github.com/lightningnetwork/lnd/build" "github.com/lightningnetwork/lnd/chainntnfs" "github.com/lightningnetwork/lnd/channeldb" "github.com/lightningnetwork/lnd/contractcourt" @@ -24,16 +26,6 @@ import ( "github.com/lightningnetwork/lnd/signal" ) -// 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 subsystem // loggers created from it will write to the backend. When adding new // subsystems, add the subsystem logger variable here and to the @@ -43,38 +35,36 @@ func (logWriter) Write(p []byte) (n int, err error) { // log file. This must be performed early during application startup by // calling initLogRotator. var ( + logWriter = &build.LogWriter{} + // 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{}) + 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 - - ltndLog = backendLog.Logger("LTND") - lnwlLog = backendLog.Logger("LNWL") - peerLog = backendLog.Logger("PEER") - discLog = backendLog.Logger("DISC") - rpcsLog = backendLog.Logger("RPCS") - srvrLog = backendLog.Logger("SRVR") - ntfnLog = backendLog.Logger("NTFN") - chdbLog = backendLog.Logger("CHDB") - fndgLog = backendLog.Logger("FNDG") - hswcLog = backendLog.Logger("HSWC") - utxnLog = backendLog.Logger("UTXN") - brarLog = backendLog.Logger("BRAR") - cmgrLog = backendLog.Logger("CMGR") - crtrLog = backendLog.Logger("CRTR") - btcnLog = backendLog.Logger("BTCN") - atplLog = backendLog.Logger("ATPL") - cnctLog = backendLog.Logger("CNCT") - sphxLog = backendLog.Logger("SPHX") + ltndLog = build.NewSubLogger("LTND", backendLog.Logger) + lnwlLog = build.NewSubLogger("LNWL", backendLog.Logger) + peerLog = build.NewSubLogger("PEER", backendLog.Logger) + discLog = build.NewSubLogger("DISC", backendLog.Logger) + rpcsLog = build.NewSubLogger("RPCS", backendLog.Logger) + srvrLog = build.NewSubLogger("SRVR", backendLog.Logger) + ntfnLog = build.NewSubLogger("NTFN", backendLog.Logger) + chdbLog = build.NewSubLogger("CHDB", backendLog.Logger) + fndgLog = build.NewSubLogger("FNDG", backendLog.Logger) + hswcLog = build.NewSubLogger("HSWC", backendLog.Logger) + utxnLog = build.NewSubLogger("UTXN", backendLog.Logger) + brarLog = build.NewSubLogger("BRAR", backendLog.Logger) + cmgrLog = build.NewSubLogger("CMGR", backendLog.Logger) + crtrLog = build.NewSubLogger("CRTR", backendLog.Logger) + btcnLog = build.NewSubLogger("BTCN", backendLog.Logger) + atplLog = build.NewSubLogger("ATPL", backendLog.Logger) + cnctLog = build.NewSubLogger("CNCT", backendLog.Logger) + sphxLog = build.NewSubLogger("SPHX", backendLog.Logger) ) // Initialize package-global logger variables. @@ -134,8 +124,8 @@ func initLogRotator(logFile string, MaxLogFileSize int, MaxLogFiles int) { pr, pw := io.Pipe() go r.Run(pr) + logWriter.RotatorPipe = pw logRotator = r - logRotatorPipe = pw } // setLogLevel sets the logging level for provided subsystem. Invalid diff --git a/make/testing_flags.mk b/make/testing_flags.mk index 308f83f9..e232b39b 100644 --- a/make/testing_flags.mk +++ b/make/testing_flags.mk @@ -1,4 +1,6 @@ -TEST_TAGS = debug +DEV_TAGS = dev +PROD_TAGS = prod +LOG_TAGS = TEST_FLAGS = # If specific package is being unit tested, construct the full name of the @@ -19,6 +21,14 @@ ifneq ($(icase),) TEST_FLAGS += -test.run=TestLightningNetworkDaemon/$(icase) endif +# Define the log tags that will be applied only when running unit tests. If none +# are provided, we default to "nolog" which will be silent. +ifneq ($(log),) +LOG_TAGS := ${log} +else +LOG_TAGS := nolog +endif + # If a timeout was requested, construct initialize the proper flag for the go # test command. If not, we set 20m (up from the default 10m). ifneq ($(timeout),) @@ -34,16 +44,16 @@ UNIT_TARGETED ?= no # If a specific package/test case was requested, run the unit test for the # targeted case. Otherwise, default to running all tests. ifeq ($(UNIT_TARGETED), yes) -UNIT := $(GOTEST) -tags="$(TEST_TAGS)" $(TEST_FLAGS) $(UNITPKG) -UNIT_RACE := $(GOTEST) -tags="$(TEST_TAGS)" $(TEST_FLAGS) -race $(UNITPKG) +UNIT := $(GOTEST) -tags="$(DEV_TAGS) $(LOG_TAGS)" $(TEST_FLAGS) $(UNITPKG) +UNIT_RACE := $(GOTEST) -tags="$(DEV_TAGS) $(LOG_TAGS)" $(TEST_FLAGS) -race $(UNITPKG) endif ifeq ($(UNIT_TARGETED), no) -UNIT := $(GOLIST) | $(XARGS) $(GOTEST) -tags="$(TEST_TAGS)" $(TEST_FLAGS) +UNIT := $(GOLIST) | $(XARGS) $(GOTEST) -tags="$(DEV_TAGS) $(LOG_TAGS)" $(TEST_FLAGS) UNIT_RACE := $(UNIT) -race endif # Construct the integration test command with the added build flags. -ITEST_TAGS := $(TEST_TAGS) rpctest +ITEST_TAGS := $(DEV_TAGS) rpctest ITEST := rm output*.log; date; $(GOTEST) -tags="$(ITEST_TAGS)" $(TEST_FLAGS) -logoutput diff --git a/nursery_store_test.go b/nursery_store_test.go index 16723abb..4617e5e0 100644 --- a/nursery_store_test.go +++ b/nursery_store_test.go @@ -9,16 +9,9 @@ import ( "testing" "github.com/btcsuite/btcd/wire" - "github.com/btcsuite/btclog" "github.com/lightningnetwork/lnd/channeldb" ) -func init() { - // Disable logging to prevent panics bc. of global state - channeldb.UseLogger(btclog.Disabled) - utxnLog = btclog.Disabled -} - // makeTestDB creates a new instance of the ChannelDB for testing purposes. A // callback which cleans up the created temporary directories is also returned // and intended to be executed after the test completes. diff --git a/peer.go b/peer.go index 2f7f5488..e95f30ee 100644 --- a/peer.go +++ b/peer.go @@ -16,6 +16,7 @@ import ( "github.com/btcsuite/btcd/txscript" "github.com/btcsuite/btcd/wire" "github.com/davecgh/go-spew/spew" + "github.com/lightningnetwork/lnd/brontide" "github.com/lightningnetwork/lnd/chainntnfs" "github.com/lightningnetwork/lnd/channeldb" diff --git a/peer_test.go b/peer_test.go index ffeb5951..86ba3c16 100644 --- a/peer_test.go +++ b/peer_test.go @@ -7,26 +7,14 @@ import ( "time" "github.com/btcsuite/btcd/wire" - "github.com/btcsuite/btclog" "github.com/btcsuite/btcutil" "github.com/lightningnetwork/lnd/chainntnfs" - "github.com/lightningnetwork/lnd/channeldb" - "github.com/lightningnetwork/lnd/contractcourt" "github.com/lightningnetwork/lnd/htlcswitch" "github.com/lightningnetwork/lnd/lnrpc" "github.com/lightningnetwork/lnd/lnwallet" "github.com/lightningnetwork/lnd/lnwire" ) -func init() { - peerLog = btclog.Disabled - srvrLog = btclog.Disabled - lnwallet.UseLogger(btclog.Disabled) - htlcswitch.UseLogger(btclog.Disabled) - channeldb.UseLogger(btclog.Disabled) - contractcourt.UseLogger(btclog.Disabled) -} - // TestPeerChannelClosureAcceptFeeResponder tests the shutdown responder's // behavior if we can agree on the fee immediately. func TestPeerChannelClosureAcceptFeeResponder(t *testing.T) { diff --git a/routing/chainview/log.go b/routing/chainview/log.go index cb3b59a2..b411819d 100644 --- a/routing/chainview/log.go +++ b/routing/chainview/log.go @@ -1,6 +1,9 @@ package chainview -import "github.com/btcsuite/btclog" +import ( + "github.com/btcsuite/btclog" + "github.com/lightningnetwork/lnd/build" +) // log is a logger that is initialized with no output filters. This // means the package will not perform any logging by default until the caller @@ -9,13 +12,13 @@ var log btclog.Logger // The default amount of logging is none. func init() { - DisableLog() + UseLogger(build.NewSubLogger("CRTR", nil)) } // DisableLog disables all library log output. Logging output is disabled // by default until either UseLogger or SetLogWriter are called. func DisableLog() { - log = btclog.Disabled + UseLogger(btclog.Disabled) } // UseLogger uses a specified Logger to output package logging info. diff --git a/routing/log.go b/routing/log.go index b990aa74..ce550812 100644 --- a/routing/log.go +++ b/routing/log.go @@ -2,6 +2,7 @@ package routing import ( "github.com/btcsuite/btclog" + "github.com/lightningnetwork/lnd/build" "github.com/lightningnetwork/lnd/routing/chainview" ) @@ -12,13 +13,13 @@ var log btclog.Logger // The default amount of logging is none. func init() { - DisableLog() + UseLogger(build.NewSubLogger("CRTR", nil)) } // DisableLog disables all library log output. Logging output is disabled by // by default until UseLogger is called. func DisableLog() { - log = btclog.Disabled + UseLogger(btclog.Disabled) } // UseLogger uses a specified Logger to output package logging info. This diff --git a/rpcserver.go b/rpcserver.go index 24984c4f..f3e3abfb 100644 --- a/rpcserver.go +++ b/rpcserver.go @@ -24,6 +24,7 @@ import ( "github.com/btcsuite/btcwallet/waddrmgr" "github.com/coreos/bbolt" "github.com/davecgh/go-spew/spew" + "github.com/lightningnetwork/lnd/build" "github.com/lightningnetwork/lnd/channeldb" "github.com/lightningnetwork/lnd/htlcswitch" "github.com/lightningnetwork/lnd/lnrpc" @@ -1190,12 +1191,12 @@ out: func (r *rpcServer) AbandonChannel(ctx context.Context, in *lnrpc.AbandonChannelRequest) (*lnrpc.AbandonChannelResponse, error) { - // If this isn't the debug build, then we won't allow the RPC to be + // If this isn't the dev build, then we won't allow the RPC to be // executed, as it's an advanced feature and won't be activated in // regular production/release builds. - if !DebugBuild { + if !build.IsDevBuild() { return nil, fmt.Errorf("AbandonChannel RPC call only " + - "available in debug builds") + "available in dev builds") } // We'll parse out the arguments to we can obtain the chanPoint of the @@ -1364,7 +1365,7 @@ func (r *rpcServer) GetInfo(ctx context.Context, Uris: uris, Alias: nodeAnn.Alias.String(), BestHeaderTimestamp: int64(bestHeaderTimestamp), - Version: version(), + Version: build.Version(), }, nil } diff --git a/signal/log.go b/signal/log.go index e7034321..6bde284b 100644 --- a/signal/log.go +++ b/signal/log.go @@ -15,7 +15,7 @@ func init() { // DisableLog disables all library log output. Logging output is disabled // by default until UseLogger is called. func DisableLog() { - log = btclog.Disabled + UseLogger(btclog.Disabled) } // UseLogger uses a specified Logger to output package logging info. diff --git a/utxonursery.go b/utxonursery.go index 71511ab2..8eed7cb5 100644 --- a/utxonursery.go +++ b/utxonursery.go @@ -13,6 +13,7 @@ import ( "github.com/btcsuite/btcd/wire" "github.com/btcsuite/btcutil" "github.com/davecgh/go-spew/spew" + "github.com/lightningnetwork/lnd/chainntnfs" "github.com/lightningnetwork/lnd/channeldb" "github.com/lightningnetwork/lnd/lnwallet"