From 54b5cb56e7263d543988530aa8e01926dccf18e7 Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Thu, 10 Oct 2013 14:13:54 -0500 Subject: [PATCH] Move logger code into its own file. This cleans up btcd.go a bit and consolidates the logging related functions. It also paves the way for upcoming message summaries. --- btcd.go | 63 ------------------------------- log.go | 100 ++++++++++++++++++++++++++++++++++++++++++++++++++ logclosure.go | 21 ----------- peer.go | 9 ----- 4 files changed, 100 insertions(+), 93 deletions(-) create mode 100644 log.go delete mode 100644 logclosure.go diff --git a/btcd.go b/btcd.go index 5c5217ba8..fc56f0fcb 100644 --- a/btcd.go +++ b/btcd.go @@ -5,11 +5,6 @@ package main import ( - "fmt" - "github.com/conformal/btcchain" - "github.com/conformal/btcdb" - "github.com/conformal/btcscript" - "github.com/conformal/seelog" "net" "net/http" _ "net/http/pprof" @@ -18,67 +13,9 @@ import ( ) var ( - log = seelog.Disabled cfg *config ) -// newLogger creates a new seelog logger using the provided logging level and -// log message prefix. -func newLogger(level string, prefix string) seelog.LoggerInterface { - fmtstring := ` - - - - - - - - ` - config := fmt.Sprintf(fmtstring, level, prefix) - - logger, err := seelog.LoggerFromConfigAsString(config) - if err != nil { - fmt.Fprintf(os.Stderr, "failed to create logger: %v", err) - os.Exit(1) - } - - return logger -} - -// useLogger sets the btcd logger to the passed logger. -func useLogger(logger seelog.LoggerInterface) { - log = logger -} - -// setLogLevel sets the log level for the logging system. It initialises a -// logger for each subsystem at the provided level. -func setLogLevel(logLevel string) []seelog.LoggerInterface { - var loggers []seelog.LoggerInterface - - // Define sub-systems. - subSystems := []struct { - level string - prefix string - useLogger func(seelog.LoggerInterface) - }{ - {logLevel, "BTCD", useLogger}, - {logLevel, "BCDB", btcdb.UseLogger}, - {logLevel, "CHAN", btcchain.UseLogger}, - {logLevel, "SCRP", btcscript.UseLogger}, - } - - // Configure all sub-systems with new loggers while keeping track of - // the created loggers to return so they can be flushed. - for _, s := range subSystems { - newLog := newLogger(s.level, s.prefix) - loggers = append(loggers, newLog) - s.useLogger(newLog) - } - - return loggers -} - // btcdMain is the real main function for btcd. It is necessary to work around // the fact that deferred functions do not run when os.Exit() is called. func btcdMain() error { diff --git a/log.go b/log.go new file mode 100644 index 000000000..9fdbe1fd8 --- /dev/null +++ b/log.go @@ -0,0 +1,100 @@ +// Copyright (c) 2013 Conformal Systems LLC. +// Use of this source code is governed by an ISC +// license that can be found in the LICENSE file. + +package main + +import ( + "fmt" + "github.com/conformal/btcchain" + "github.com/conformal/btcdb" + "github.com/conformal/btcscript" + "github.com/conformal/seelog" + "os" +) + +var ( + log = seelog.Disabled +) + +// 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. +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) +} + +// newLogger creates a new seelog logger using the provided logging level and +// log message prefix. +func newLogger(level string, prefix string) seelog.LoggerInterface { + fmtstring := ` + + + + + + + + ` + config := fmt.Sprintf(fmtstring, level, prefix) + + logger, err := seelog.LoggerFromConfigAsString(config) + if err != nil { + fmt.Fprintf(os.Stderr, "failed to create logger: %v", err) + os.Exit(1) + } + + return logger +} + +// useLogger sets the btcd logger to the passed logger. +func useLogger(logger seelog.LoggerInterface) { + log = logger +} + +// setLogLevel sets the log level for the logging system. It initializes a +// logger for each subsystem at the provided level. +func setLogLevel(logLevel string) []seelog.LoggerInterface { + var loggers []seelog.LoggerInterface + + // Define sub-systems. + subSystems := []struct { + level string + prefix string + useLogger func(seelog.LoggerInterface) + }{ + {logLevel, "BTCD", useLogger}, + {logLevel, "BCDB", btcdb.UseLogger}, + {logLevel, "CHAN", btcchain.UseLogger}, + {logLevel, "SCRP", btcscript.UseLogger}, + } + + // Configure all sub-systems with new loggers while keeping track of + // the created loggers to return so they can be flushed. + for _, s := range subSystems { + newLog := newLogger(s.level, s.prefix) + loggers = append(loggers, newLog) + s.useLogger(newLog) + } + + return loggers +} + +// directionString is a helper function that returns a string that represents +// the direction of a connection (inbound or outbound). +func directionString(inbound bool) string { + if inbound { + return "inbound" + } + return "outbound" +} diff --git a/logclosure.go b/logclosure.go deleted file mode 100644 index 7763357de..000000000 --- a/logclosure.go +++ /dev/null @@ -1,21 +0,0 @@ -// Copyright (c) 2013 Conformal Systems LLC. -// Use of this source code is governed by an ISC -// license that can be found in the LICENSE file. - -package main - -// 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. -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) -} diff --git a/peer.go b/peer.go index 46c987aac..861759f53 100644 --- a/peer.go +++ b/peer.go @@ -42,15 +42,6 @@ var userAgent = fmt.Sprintf("/btcd:%d.%d.%d/", appMajor, appMinor, appPatch) // zeroHash is the zero value hash (all zeros). It is defined as a convenience. var zeroHash btcwire.ShaHash -// directionString is a helper function that returns a string that represents -// the direction of a connection (inbound or outbound). -func directionString(inbound bool) string { - if inbound { - return "inbound" - } - return "outbound" -} - // minUint32 is a helper function to return the minimum of two uint32s. // This avoids a math import and the need to cast to floats. func minUint32(a, b uint32) uint32 {