github.com/mavryk-network/mvgo@v1.19.9/contract/log.go (about) 1 // Copyright (c) 2020-2021 Blockwatch Data Inc. 2 // Author: alex@blockwatch.cc 3 4 //nolint:unused,deadcode 5 package contract 6 7 import logpkg "github.com/echa/log" 8 9 // log is a logger that is initialized with no output filters. This 10 // means the package will not perform any logging by default until the caller 11 // requests it. 12 var log logpkg.Logger = logpkg.Log 13 14 // The default amount of logging is none. 15 func init() { 16 DisableLog() 17 } 18 19 // DisableLog disables all library log output. Logging output is disabled 20 // by default until either UseLogger or SetLogWriter are called. 21 func DisableLog() { 22 log = logpkg.Disabled 23 } 24 25 // UseLogger uses a specified Logger to output package logging info. 26 // This should be used in preference to SetLogWriter if the caller is also 27 // using logpkg. 28 func UseLogger(logger logpkg.Logger) { 29 log = logger 30 } 31 32 // LogClosure is a closure that can be printed with %v to be used to 33 // generate expensive-to-create data for a detailed log level and avoid doing 34 // the work if the data isn't printed. 35 type logClosure func() string 36 37 // String invokes the log closure and returns the results string. 38 func (c logClosure) String() string { 39 return c() 40 } 41 42 // newLogClosure returns a new closure over the passed function which allows 43 // it to be used as a parameter in a logging function that is only invoked when 44 // the logging level is such that the message will actually be logged. 45 func newLogClosure(c func() string) logClosure { 46 return logClosure(c) 47 } 48 49 func logDebug(c func()) { 50 if log.Level() <= logpkg.LevelDebug { 51 c() 52 } 53 }