2019-01-24 16:28:25 +03:00
|
|
|
package lnd
|
2016-03-23 04:43:10 +03:00
|
|
|
|
|
|
|
import (
|
2019-08-07 03:57:02 +03:00
|
|
|
"context"
|
2017-06-21 18:07:44 +03:00
|
|
|
|
2018-06-05 04:34:16 +03:00
|
|
|
"github.com/btcsuite/btcd/connmgr"
|
2016-03-23 04:43:10 +03:00
|
|
|
"github.com/btcsuite/btclog"
|
2017-05-18 21:48:10 +03:00
|
|
|
"github.com/lightninglabs/neutrino"
|
2019-03-27 13:44:30 +03:00
|
|
|
sphinx "github.com/lightningnetwork/lightning-onion"
|
2017-08-11 07:39:45 +03:00
|
|
|
"github.com/lightningnetwork/lnd/autopilot"
|
2018-09-20 13:28:35 +03:00
|
|
|
"github.com/lightningnetwork/lnd/build"
|
2016-08-04 22:37:50 +03:00
|
|
|
"github.com/lightningnetwork/lnd/chainntnfs"
|
2019-02-09 06:39:38 +03:00
|
|
|
"github.com/lightningnetwork/lnd/chanbackup"
|
2016-03-23 04:43:10 +03:00
|
|
|
"github.com/lightningnetwork/lnd/channeldb"
|
2018-10-22 06:36:56 +03:00
|
|
|
"github.com/lightningnetwork/lnd/channelnotifier"
|
2018-01-17 07:25:34 +03:00
|
|
|
"github.com/lightningnetwork/lnd/contractcourt"
|
2017-03-19 21:40:25 +03:00
|
|
|
"github.com/lightningnetwork/lnd/discovery"
|
2017-05-02 23:04:58 +03:00
|
|
|
"github.com/lightningnetwork/lnd/htlcswitch"
|
2019-01-05 04:34:59 +03:00
|
|
|
"github.com/lightningnetwork/lnd/invoices"
|
2018-12-13 14:26:30 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnrpc/autopilotrpc"
|
2018-12-11 05:34:36 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnrpc/chainrpc"
|
2018-12-20 13:42:28 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnrpc/invoicesrpc"
|
2019-03-27 13:44:30 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnrpc/routerrpc"
|
2018-10-23 03:42:21 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnrpc/signrpc"
|
2018-10-25 05:32:37 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnrpc/walletrpc"
|
2019-06-08 03:46:31 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnrpc/wtclientrpc"
|
2016-03-23 04:43:10 +03:00
|
|
|
"github.com/lightningnetwork/lnd/lnwallet"
|
2019-05-31 10:59:27 +03:00
|
|
|
"github.com/lightningnetwork/lnd/monitoring"
|
2019-01-05 04:34:59 +03:00
|
|
|
"github.com/lightningnetwork/lnd/netann"
|
2019-07-29 17:59:48 +03:00
|
|
|
"github.com/lightningnetwork/lnd/peernotifier"
|
2016-12-27 08:42:23 +03:00
|
|
|
"github.com/lightningnetwork/lnd/routing"
|
2018-06-15 06:15:56 +03:00
|
|
|
"github.com/lightningnetwork/lnd/signal"
|
2018-09-26 08:03:16 +03:00
|
|
|
"github.com/lightningnetwork/lnd/sweep"
|
2019-01-12 05:32:40 +03:00
|
|
|
"github.com/lightningnetwork/lnd/watchtower"
|
2019-06-14 03:29:09 +03:00
|
|
|
"github.com/lightningnetwork/lnd/watchtower/wtclient"
|
2019-08-07 03:57:02 +03:00
|
|
|
"google.golang.org/grpc"
|
2016-03-23 04:43:10 +03:00
|
|
|
)
|
|
|
|
|
2018-02-07 06:11:11 +03:00
|
|
|
// Loggers per subsystem. A single backend logger is created and all subsystem
|
2017-06-21 18:07:44 +03:00
|
|
|
// 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
|
2017-07-31 02:01:44 +03:00
|
|
|
// log file. This must be performed early during application startup by
|
2019-09-23 14:20:14 +03:00
|
|
|
// calling logWriter.InitLogRotator.
|
2016-03-23 04:43:10 +03:00
|
|
|
var (
|
2019-09-23 14:20:14 +03:00
|
|
|
logWriter = build.NewRotatingLogWriter()
|
2018-09-20 13:28:35 +03:00
|
|
|
|
2019-09-23 14:20:14 +03:00
|
|
|
// Loggers that need to be accessible from the lnd package can be placed
|
|
|
|
// here. Loggers that are only used in sub modules can be added directly
|
|
|
|
// by using the addSubLogger method.
|
|
|
|
ltndLog = build.NewSubLogger("LTND", logWriter.GenSubLogger)
|
|
|
|
peerLog = build.NewSubLogger("PEER", logWriter.GenSubLogger)
|
|
|
|
rpcsLog = build.NewSubLogger("RPCS", logWriter.GenSubLogger)
|
|
|
|
srvrLog = build.NewSubLogger("SRVR", logWriter.GenSubLogger)
|
|
|
|
fndgLog = build.NewSubLogger("FNDG", logWriter.GenSubLogger)
|
|
|
|
utxnLog = build.NewSubLogger("UTXN", logWriter.GenSubLogger)
|
|
|
|
brarLog = build.NewSubLogger("BRAR", logWriter.GenSubLogger)
|
|
|
|
atplLog = build.NewSubLogger("ATPL", logWriter.GenSubLogger)
|
2016-03-23 04:43:10 +03:00
|
|
|
)
|
|
|
|
|
2017-06-21 18:07:44 +03:00
|
|
|
// Initialize package-global logger variables.
|
|
|
|
func init() {
|
2019-09-23 14:20:14 +03:00
|
|
|
setSubLogger("LTND", ltndLog, signal.UseLogger)
|
|
|
|
setSubLogger("ATPL", atplLog, autopilot.UseLogger)
|
|
|
|
setSubLogger("PEER", peerLog, nil)
|
|
|
|
setSubLogger("RPCS", rpcsLog, nil)
|
|
|
|
setSubLogger("SRVR", srvrLog, nil)
|
|
|
|
setSubLogger("FNDG", fndgLog, nil)
|
|
|
|
setSubLogger("UTXN", utxnLog, nil)
|
|
|
|
setSubLogger("BRAR", brarLog, nil)
|
|
|
|
|
|
|
|
addSubLogger("LNWL", lnwallet.UseLogger)
|
|
|
|
addSubLogger("DISC", discovery.UseLogger)
|
|
|
|
addSubLogger("NTFN", chainntnfs.UseLogger)
|
|
|
|
addSubLogger("CHDB", channeldb.UseLogger)
|
|
|
|
addSubLogger("HSWC", htlcswitch.UseLogger)
|
|
|
|
addSubLogger("CMGR", connmgr.UseLogger)
|
|
|
|
addSubLogger("CRTR", routing.UseLogger)
|
|
|
|
addSubLogger("BTCN", neutrino.UseLogger)
|
|
|
|
addSubLogger("CNCT", contractcourt.UseLogger)
|
|
|
|
addSubLogger("SPHX", sphinx.UseLogger)
|
|
|
|
addSubLogger("SWPR", sweep.UseLogger)
|
|
|
|
addSubLogger("SGNR", signrpc.UseLogger)
|
|
|
|
addSubLogger("WLKT", walletrpc.UseLogger)
|
|
|
|
addSubLogger("ARPC", autopilotrpc.UseLogger)
|
|
|
|
addSubLogger("INVC", invoices.UseLogger)
|
|
|
|
addSubLogger("NANN", netann.UseLogger)
|
|
|
|
addSubLogger("WTWR", watchtower.UseLogger)
|
|
|
|
addSubLogger("NTFR", chainrpc.UseLogger)
|
|
|
|
addSubLogger("IRPC", invoicesrpc.UseLogger)
|
|
|
|
addSubLogger("CHNF", channelnotifier.UseLogger)
|
|
|
|
addSubLogger("CHBU", chanbackup.UseLogger)
|
|
|
|
addSubLogger("PROM", monitoring.UseLogger)
|
|
|
|
addSubLogger("WTCL", wtclient.UseLogger)
|
|
|
|
addSubLogger("PRNF", peernotifier.UseLogger)
|
2019-03-27 13:44:30 +03:00
|
|
|
|
|
|
|
addSubLogger(routerrpc.Subsystem, routerrpc.UseLogger)
|
2019-06-08 03:46:31 +03:00
|
|
|
addSubLogger(wtclientrpc.Subsystem, wtclientrpc.UseLogger)
|
2019-03-27 13:44:30 +03:00
|
|
|
}
|
|
|
|
|
2019-09-23 14:20:14 +03:00
|
|
|
// addSubLogger is a helper method to conveniently create and register the
|
|
|
|
// logger of a sub system.
|
2019-03-27 13:44:30 +03:00
|
|
|
func addSubLogger(subsystem string, useLogger func(btclog.Logger)) {
|
2019-09-23 14:20:14 +03:00
|
|
|
logger := build.NewSubLogger(subsystem, logWriter.GenSubLogger)
|
|
|
|
setSubLogger(subsystem, logger, useLogger)
|
2017-06-21 18:07:44 +03:00
|
|
|
}
|
|
|
|
|
2019-09-23 14:20:14 +03:00
|
|
|
// setSubLogger is a helper method to conveniently register the logger of a sub
|
|
|
|
// system.
|
|
|
|
func setSubLogger(subsystem string, logger btclog.Logger,
|
|
|
|
useLogger func(btclog.Logger)) {
|
|
|
|
|
|
|
|
logWriter.RegisterSubLogger(subsystem, logger)
|
|
|
|
if useLogger != nil {
|
|
|
|
useLogger(logger)
|
|
|
|
}
|
2016-03-23 04:43:10 +03:00
|
|
|
}
|
|
|
|
|
2017-07-31 02:01:44 +03:00
|
|
|
// logClosure is used to provide a closure over expensive logging operations so
|
|
|
|
// don't have to be performed when the logging level doesn't warrant it.
|
2016-06-21 21:32:07 +03:00
|
|
|
type logClosure func() string
|
|
|
|
|
|
|
|
// String invokes the underlying function and returns the result.
|
|
|
|
func (c logClosure) String() string {
|
|
|
|
return c()
|
|
|
|
}
|
|
|
|
|
|
|
|
// newLogClosure returns a new closure over a function that returns a string
|
|
|
|
// which itself provides a Stringer interface so that it can be used with the
|
|
|
|
// logging system.
|
|
|
|
func newLogClosure(c func() string) logClosure {
|
|
|
|
return logClosure(c)
|
|
|
|
}
|
2019-08-07 03:57:02 +03:00
|
|
|
|
|
|
|
// errorLogUnaryServerInterceptor is a simple UnaryServerInterceptor that will
|
|
|
|
// automatically log any errors that occur when serving a client's unary
|
|
|
|
// request.
|
|
|
|
func errorLogUnaryServerInterceptor(logger btclog.Logger) grpc.UnaryServerInterceptor {
|
|
|
|
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo,
|
|
|
|
handler grpc.UnaryHandler) (interface{}, error) {
|
|
|
|
|
|
|
|
resp, err := handler(ctx, req)
|
|
|
|
if err != nil {
|
|
|
|
// TODO(roasbeef): also log request details?
|
|
|
|
logger.Errorf("[%v]: %v", info.FullMethod, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// errorLogStreamServerInterceptor is a simple StreamServerInterceptor that
|
|
|
|
// will log any errors that occur while processing a client or server streaming
|
|
|
|
// RPC.
|
|
|
|
func errorLogStreamServerInterceptor(logger btclog.Logger) grpc.StreamServerInterceptor {
|
|
|
|
return func(srv interface{}, ss grpc.ServerStream,
|
|
|
|
info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
|
|
|
|
|
|
|
|
err := handler(srv, ss)
|
|
|
|
if err != nil {
|
|
|
|
logger.Errorf("[%v]: %v", info.FullMethod, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|