loop: unify top level logger, use lnd's build pacakge for logging

pull/30/head
Olaoluwa Osuntokun 5 years ago
parent 769c69e512
commit b45956b84f
No known key found for this signature in database
GPG Key ID: CE58F7F8E20FD9A2

@ -134,7 +134,7 @@ func (s *Client) Run(ctx context.Context,
if err != nil {
return fmt.Errorf("GetInfo error: %v", err)
}
logger.Infof("Connected to lnd node %v with pubkey %v",
log.Infof("Connected to lnd node %v with pubkey %v",
info.Alias, hex.EncodeToString(info.IdentityPubkey[:]),
)
@ -172,22 +172,22 @@ func (s *Client) Run(ctx context.Context,
}
if err != nil {
logger.Errorf("Swap client terminating: %v", err)
log.Errorf("Swap client terminating: %v", err)
} else {
logger.Info("Swap client terminating")
log.Info("Swap client terminating")
}
// Cancel all remaining active goroutines.
mainCancel()
// Wait for all to finish.
logger.Debug("Wait for executor to finish")
log.Debug("Wait for executor to finish")
s.executor.waitFinished()
logger.Debug("Wait for goroutines to finish")
log.Debug("Wait for goroutines to finish")
s.wg.Wait()
logger.Info("Swap client terminated")
log.Info("Swap client terminated")
return err
}
@ -206,7 +206,7 @@ func (s *Client) resumeSwaps(ctx context.Context,
}
swap, err := resumeLoopOutSwap(ctx, swapCfg, pend)
if err != nil {
logger.Errorf("resuming swap: %v", err)
log.Errorf("resuming swap: %v", err)
continue
}
@ -226,7 +226,7 @@ func (s *Client) resumeSwaps(ctx context.Context,
func (s *Client) LoopOut(globalCtx context.Context,
request *OutRequest) (*lntypes.Hash, error) {
logger.Infof("LoopOut %v to %v (channel: %v)",
log.Infof("LoopOut %v to %v (channel: %v)",
request.Amount, request.DestAddr,
request.LoopOutChannel,
)
@ -276,7 +276,7 @@ func (s *Client) LoopOutQuote(ctx context.Context,
return nil, ErrSwapAmountTooHigh
}
logger.Infof("Offchain swap destination: %x", terms.SwapPaymentDest)
log.Infof("Offchain swap destination: %x", terms.SwapPaymentDest)
swapFee := swap.CalcFee(
request.Amount, terms.SwapFeeBase, terms.SwapFeeRate,

@ -59,7 +59,7 @@ func (s *executor) run(mainCtx context.Context,
// Before starting, make sure we have an up to date block height.
// Otherwise we might reveal a preimage for a swap that is already
// expired.
logger.Infof("Wait for first block ntfn")
log.Infof("Wait for first block ntfn")
var height int32
setHeight := func(h int32) {
@ -77,7 +77,7 @@ func (s *executor) run(mainCtx context.Context,
}
// Start main event loop.
logger.Infof("Starting event loop at height %v", height)
log.Infof("Starting event loop at height %v", height)
// Signal that executor being ready with an up to date block height.
close(s.ready)

@ -9,6 +9,7 @@ require (
github.com/golang/protobuf v1.3.1
github.com/grpc-ecosystem/grpc-gateway v1.8.5
github.com/jessevdk/go-flags v1.4.0
github.com/jrick/logrotate v1.0.0
github.com/lightninglabs/neutrino v0.0.0-20190314214430-643615b8c132 // indirect
github.com/lightningnetwork/lnd v0.0.0-20190314214430-b4a1024ac74fc576c65c8074288a5ffaf6bd1ec4
github.com/lightningnetwork/lnd/queue v1.0.1

@ -2,28 +2,33 @@ package loop
import (
"fmt"
"os"
"github.com/btcsuite/btclog"
"github.com/lightningnetwork/lnd/build"
"github.com/lightningnetwork/lnd/lntypes"
)
// 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 (
backendLog = btclog.NewBackend(logWriter{})
logger = backendLog.Logger("CLIENT")
servicesLogger = backendLog.Logger("SERVICES")
)
// 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() {
UseLogger(build.NewSubLogger("LOOP", nil))
}
// logWriter implements an io.Writer that outputs to both standard output and
// the write-end pipe of an initialized log rotator.
type logWriter struct{}
// DisableLog disables all library log output. Logging output is disabled by
// default until UseLogger is called.
func DisableLog() {
UseLogger(btclog.Disabled)
}
func (logWriter) Write(p []byte) (n int, err error) {
os.Stdout.Write(p)
return len(p), nil
// 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
}
// SwapLog logs with a short swap hash prefix.

@ -50,7 +50,7 @@ func newLoopOutSwap(globalCtx context.Context, cfg *swapConfig,
// Generate random preimage.
var swapPreimage [32]byte
if _, err := rand.Read(swapPreimage[:]); err != nil {
logger.Error("Cannot generate preimage")
log.Error("Cannot generate preimage")
}
swapHash := lntypes.Hash(sha256.Sum256(swapPreimage[:]))
@ -66,7 +66,7 @@ func newLoopOutSwap(globalCtx context.Context, cfg *swapConfig,
// Post the swap parameters to the swap server. The response contains
// the server revocation key and the swap and prepay invoices.
logger.Infof("Initiating swap request at height %v", currentHeight)
log.Infof("Initiating swap request at height %v", currentHeight)
swapResp, err := cfg.server.NewLoopOutSwap(globalCtx, swapHash,
request.Amount, receiverKey,
@ -135,7 +135,7 @@ func resumeLoopOutSwap(reqContext context.Context, cfg *swapConfig,
hash := lntypes.Hash(sha256.Sum256(pend.Contract.Preimage[:]))
logger.Infof("Resuming swap %v", hash)
log.Infof("Resuming swap %v", hash)
swapKit, err := newSwapKit(
hash, TypeOut, cfg, &pend.Contract.SwapContract,
@ -291,7 +291,7 @@ func (s *loopOutSwap) executeSwap(globalCtx context.Context) error {
// Verify amount if preimage hasn't been revealed yet.
if s.state != loopdb.StatePreimageRevealed && htlcValue < s.AmountRequested {
logger.Warnf("Swap amount too low, expected %v but received %v",
log.Warnf("Swap amount too low, expected %v but received %v",
s.AmountRequested, htlcValue)
s.state = loopdb.StateFailInsufficientValue
@ -468,7 +468,7 @@ func (s *loopOutSwap) waitForConfirmedHtlc(globalCtx context.Context) (
case notification := <-s.blockEpochChan:
s.height = notification.(int32)
logger.Infof("Received block %v", s.height)
log.Infof("Received block %v", s.height)
if checkMaxRevealHeightExceeded() {
return nil, nil
@ -653,21 +653,21 @@ func validateLoopOutContract(lnd *lndclient.LndServices,
swapFee := swapInvoiceAmt + prepayInvoiceAmt - request.Amount
if swapFee > request.MaxSwapFee {
logger.Warnf("Swap fee %v exceeding maximum of %v",
log.Warnf("Swap fee %v exceeding maximum of %v",
swapFee, request.MaxSwapFee)
return ErrSwapFeeTooHigh
}
if prepayInvoiceAmt > request.MaxPrepayAmount {
logger.Warnf("Prepay amount %v exceeding maximum of %v",
log.Warnf("Prepay amount %v exceeding maximum of %v",
prepayInvoiceAmt, request.MaxPrepayAmount)
return ErrPrepayAmountTooHigh
}
if response.expiry-height < MinLoopOutPreimageRevealDelta {
logger.Warnf("Proposed expiry %v (delta %v) too soon",
log.Warnf("Proposed expiry %v (delta %v) too soon",
response.expiry, response.expiry-height)
return ErrExpiryTooSoon

@ -48,7 +48,7 @@ func newSwapKit(hash lntypes.Hash, swapType Type, cfg *swapConfig,
log := &SwapLog{
Hash: hash,
Logger: logger,
Logger: log,
}
log.Infof("Htlc address: %v", htlcAddress)

Loading…
Cancel
Save