github.com/BlockABC/godash@v0.0.0-20191112120524-f4aa3a32c566/blockchain/log.go (about)

     1  // Copyright (c) 2013-2014 The btcsuite developers
     2  // Copyright (c) 2016 The Dash developers
     3  // Use of this source code is governed by an ISC
     4  // license that can be found in the LICENSE file.
     5  
     6  package blockchain
     7  
     8  import (
     9  	"errors"
    10  	"io"
    11  
    12  	"github.com/btcsuite/btclog"
    13  )
    14  
    15  // log is a logger that is initialized with no output filters.  This
    16  // means the package will not perform any logging by default until the caller
    17  // requests it.
    18  var log btclog.Logger
    19  
    20  // The default amount of logging is none.
    21  func init() {
    22  	DisableLog()
    23  }
    24  
    25  // DisableLog disables all library log output.  Logging output is disabled
    26  // by default until either UseLogger or SetLogWriter are called.
    27  func DisableLog() {
    28  	log = btclog.Disabled
    29  }
    30  
    31  // UseLogger uses a specified Logger to output package logging info.
    32  // This should be used in preference to SetLogWriter if the caller is also
    33  // using btclog.
    34  func UseLogger(logger btclog.Logger) {
    35  	log = logger
    36  }
    37  
    38  // SetLogWriter uses a specified io.Writer to output package logging info.
    39  // This allows a caller to direct package logging output without needing a
    40  // dependency on seelog.  If the caller is also using btclog, UseLogger should
    41  // be used instead.
    42  func SetLogWriter(w io.Writer, level string) error {
    43  	if w == nil {
    44  		return errors.New("nil writer")
    45  	}
    46  
    47  	lvl, ok := btclog.LogLevelFromString(level)
    48  	if !ok {
    49  		return errors.New("invalid log level")
    50  	}
    51  
    52  	l, err := btclog.NewLoggerFromWriter(w, lvl)
    53  	if err != nil {
    54  		return err
    55  	}
    56  
    57  	UseLogger(l)
    58  	return nil
    59  }
    60  
    61  // LogClosure is a closure that can be printed with %v to be used to
    62  // generate expensive-to-create data for a detailed log level and avoid doing
    63  // the work if the data isn't printed.
    64  type logClosure func() string
    65  
    66  func (c logClosure) String() string {
    67  	return c()
    68  }
    69  
    70  func newLogClosure(c func() string) logClosure {
    71  	return logClosure(c)
    72  }