From b1c247231b11f313ca0eedff75ea563926d23f68 Mon Sep 17 00:00:00 2001 From: obscuren Date: Fri, 31 Oct 2014 12:56:05 +0100 Subject: ethlog => logger --- block_pool.go | 4 +- chain/chain_manager.go | 4 +- chain/dagger.go | 4 +- chain/state_manager.go | 4 +- chain/transaction_pool.go | 4 +- cmd/ethereum/cmd.go | 4 +- cmd/ethereum/flags.go | 4 +- cmd/ethereum/main.go | 5 +- cmd/ethereum/repl/repl.go | 8 +- cmd/mist/bindings.go | 16 +-- cmd/mist/ext_app.go | 2 +- cmd/mist/flags.go | 3 +- cmd/mist/gui.go | 34 +++--- cmd/mist/html_container.go | 8 +- cmd/mist/main.go | 4 +- cmd/mist/qml_container.go | 2 +- cmd/mist/ui_lib.go | 4 +- ethereum.go | 38 +++--- ethlog/example_test.go | 21 ---- ethlog/loggers.go | 248 --------------------------------------- ethlog/loggers_test.go | 174 --------------------------- ethminer/miner.go | 24 ++-- ethpipe/js_pipe.go | 2 +- ethpipe/pipe.go | 8 +- ethstate/state.go | 4 +- javascript/javascript_runtime.go | 4 +- logger/example_test.go | 21 ++++ logger/loggers.go | 248 +++++++++++++++++++++++++++++++++++++++ logger/loggers_test.go | 174 +++++++++++++++++++++++++++ peer.go | 16 +-- rpc/packages.go | 2 +- rpc/server.go | 12 +- utils/cmd.go | 34 +++--- vm/common.go | 4 +- websocket/client.go | 8 +- websocket/server.go | 14 +-- 36 files changed, 585 insertions(+), 585 deletions(-) delete mode 100644 ethlog/example_test.go delete mode 100644 ethlog/loggers.go delete mode 100644 ethlog/loggers_test.go create mode 100644 logger/example_test.go create mode 100644 logger/loggers.go create mode 100644 logger/loggers_test.go diff --git a/block_pool.go b/block_pool.go index 49fa07eb1..1cf3ab907 100644 --- a/block_pool.go +++ b/block_pool.go @@ -10,12 +10,12 @@ import ( "time" "github.com/ethereum/go-ethereum/chain" - "github.com/ethereum/go-ethereum/ethlog" "github.com/ethereum/go-ethereum/ethutil" "github.com/ethereum/go-ethereum/ethwire" + "github.com/ethereum/go-ethereum/logger" ) -var poollogger = ethlog.NewLogger("BPOOL") +var poollogger = logger.NewLogger("BPOOL") type block struct { from *Peer diff --git a/chain/chain_manager.go b/chain/chain_manager.go index 83ae21dcc..8ee7a85cc 100644 --- a/chain/chain_manager.go +++ b/chain/chain_manager.go @@ -5,11 +5,11 @@ import ( "fmt" "math/big" - "github.com/ethereum/go-ethereum/ethlog" "github.com/ethereum/go-ethereum/ethutil" + "github.com/ethereum/go-ethereum/logger" ) -var chainlogger = ethlog.NewLogger("CHAIN") +var chainlogger = logger.NewLogger("CHAIN") type ChainManager struct { Ethereum EthManager diff --git a/chain/dagger.go b/chain/dagger.go index 66fbbcde4..3333e002d 100644 --- a/chain/dagger.go +++ b/chain/dagger.go @@ -7,12 +7,12 @@ import ( "time" "github.com/ethereum/go-ethereum/crypto" - "github.com/ethereum/go-ethereum/ethlog" "github.com/ethereum/go-ethereum/ethutil" + "github.com/ethereum/go-ethereum/logger" "github.com/obscuren/sha3" ) -var powlogger = ethlog.NewLogger("POW") +var powlogger = logger.NewLogger("POW") type PoW interface { Search(block *Block, stop <-chan struct{}) []byte diff --git a/chain/state_manager.go b/chain/state_manager.go index 9f7a4a0fd..8266726f5 100644 --- a/chain/state_manager.go +++ b/chain/state_manager.go @@ -10,14 +10,14 @@ import ( "time" "github.com/ethereum/go-ethereum/crypto" - "github.com/ethereum/go-ethereum/ethlog" "github.com/ethereum/go-ethereum/ethstate" "github.com/ethereum/go-ethereum/ethutil" "github.com/ethereum/go-ethereum/ethwire" "github.com/ethereum/go-ethereum/event" + "github.com/ethereum/go-ethereum/logger" ) -var statelogger = ethlog.NewLogger("BLOCK") +var statelogger = logger.NewLogger("BLOCK") type Peer interface { Inbound() bool diff --git a/chain/transaction_pool.go b/chain/transaction_pool.go index 861ebdf00..21d501dfc 100644 --- a/chain/transaction_pool.go +++ b/chain/transaction_pool.go @@ -7,12 +7,12 @@ import ( "math/big" "sync" - "github.com/ethereum/go-ethereum/ethlog" "github.com/ethereum/go-ethereum/ethstate" "github.com/ethereum/go-ethereum/ethwire" + "github.com/ethereum/go-ethereum/logger" ) -var txplogger = ethlog.NewLogger("TXP") +var txplogger = logger.NewLogger("TXP") const txPoolQueueSize = 50 diff --git a/cmd/ethereum/cmd.go b/cmd/ethereum/cmd.go index a0225b652..d8cb19eaf 100644 --- a/cmd/ethereum/cmd.go +++ b/cmd/ethereum/cmd.go @@ -38,11 +38,11 @@ func InitJsConsole(ethereum *eth.Ethereum) { func ExecJsFile(ethereum *eth.Ethereum, InputFile string) { file, err := os.Open(InputFile) if err != nil { - logger.Fatalln(err) + clilogger.Fatalln(err) } content, err := ioutil.ReadAll(file) if err != nil { - logger.Fatalln(err) + clilogger.Fatalln(err) } re := javascript.NewJSRE(ethereum) utils.RegisterInterrupt(func(os.Signal) { diff --git a/cmd/ethereum/flags.go b/cmd/ethereum/flags.go index 892ea0036..783944cf2 100644 --- a/cmd/ethereum/flags.go +++ b/cmd/ethereum/flags.go @@ -25,7 +25,7 @@ import ( "os/user" "path" - "github.com/ethereum/go-ethereum/ethlog" + "github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/vm" ) @@ -98,7 +98,7 @@ func Init() { flag.StringVar(&Datadir, "datadir", defaultDataDir(), "specifies the datadir to use") flag.StringVar(&ConfigFile, "conf", defaultConfigFile, "config file") flag.StringVar(&DebugFile, "debug", "", "debug file (no debugging if not set)") - flag.IntVar(&LogLevel, "loglevel", int(ethlog.InfoLevel), "loglevel: 0-5: silent,error,warn,info,debug,debug detail)") + flag.IntVar(&LogLevel, "loglevel", int(logger.InfoLevel), "loglevel: 0-5: silent,error,warn,info,debug,debug detail)") flag.BoolVar(&DiffTool, "difftool", false, "creates output for diff'ing. Sets LogLevel=0") flag.StringVar(&DiffType, "diff", "all", "sets the level of diff output [vm, all]. Has no effect if difftool=false") flag.BoolVar(&ShowGenesis, "genesis", false, "Dump the genesis block") diff --git a/cmd/ethereum/main.go b/cmd/ethereum/main.go index 71b9b72f1..fa85865fc 100644 --- a/cmd/ethereum/main.go +++ b/cmd/ethereum/main.go @@ -23,9 +23,10 @@ import ( "runtime" "github.com/ethereum/go-ethereum/chain" - "github.com/ethereum/go-ethereum/ethlog" "github.com/ethereum/go-ethereum/ethutil" + "github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/utils" + "github.com/ethgo.old/ethlog" ) const ( @@ -33,7 +34,7 @@ const ( Version = "0.7.0" ) -var logger = ethlog.NewLogger("CLI") +var clilogger = logger.NewLogger("CLI") func main() { runtime.GOMAXPROCS(runtime.NumCPU()) diff --git a/cmd/ethereum/repl/repl.go b/cmd/ethereum/repl/repl.go index 101bb058b..a5146fecd 100644 --- a/cmd/ethereum/repl/repl.go +++ b/cmd/ethereum/repl/repl.go @@ -25,12 +25,12 @@ import ( "path" "github.com/ethereum/go-ethereum" - "github.com/ethereum/go-ethereum/ethlog" "github.com/ethereum/go-ethereum/ethutil" "github.com/ethereum/go-ethereum/javascript" + "github.com/ethereum/go-ethereum/logger" ) -var logger = ethlog.NewLogger("REPL") +var repllogger = logger.NewLogger("REPL") type Repl interface { Start() @@ -59,7 +59,7 @@ func NewJSRepl(ethereum *eth.Ethereum) *JSRepl { func (self *JSRepl) Start() { if !self.running { self.running = true - logger.Infoln("init JS Console") + repllogger.Infoln("init JS Console") reader := bufio.NewReader(self.history) for { line, err := reader.ReadString('\n') @@ -80,7 +80,7 @@ func (self *JSRepl) Stop() { if self.running { self.running = false self.re.Stop() - logger.Infoln("exit JS Console") + repllogger.Infoln("exit JS Console") self.history.Close() } } diff --git a/cmd/mist/bindings.go b/cmd/mist/bindings.go index 03d35a574..8b038587b 100644 --- a/cmd/mist/bindings.go +++ b/cmd/mist/bindings.go @@ -23,9 +23,9 @@ import ( "strconv" "github.com/ethereum/go-ethereum/chain" - "github.com/ethereum/go-ethereum/ethlog" "github.com/ethereum/go-ethereum/ethpipe" "github.com/ethereum/go-ethereum/ethutil" + "github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/utils" ) @@ -35,7 +35,7 @@ type plugin struct { } // LogPrint writes to the GUI log. -func (gui *Gui) LogPrint(level ethlog.LogLevel, msg string) { +func (gui *Gui) LogPrint(level logger.LogLevel, msg string) { /* str := strings.TrimRight(s, "\n") lines := strings.Split(str, "\n") @@ -74,14 +74,14 @@ func (gui *Gui) ToggleTurboMining() { gui.miner.ToggleTurbo() } -// functions that allow Gui to implement interface ethlog.LogSystem -func (gui *Gui) SetLogLevel(level ethlog.LogLevel) { +// functions that allow Gui to implement interface guilogger.LogSystem +func (gui *Gui) SetLogLevel(level logger.LogLevel) { gui.logLevel = level gui.stdLog.SetLogLevel(level) gui.config.Save("loglevel", level) } -func (gui *Gui) GetLogLevel() ethlog.LogLevel { +func (gui *Gui) GetLogLevel() logger.LogLevel { return gui.logLevel } @@ -119,7 +119,7 @@ func (self *Gui) DumpState(hash, path string) { } if block == nil { - logger.Infof("block err: not found %s\n", hash) + guilogger.Infof("block err: not found %s\n", hash) return } @@ -128,12 +128,12 @@ func (self *Gui) DumpState(hash, path string) { file, err := os.OpenFile(path[7:], os.O_CREATE|os.O_RDWR, os.ModePerm) if err != nil { - logger.Infoln("dump err: ", err) + guilogger.Infoln("dump err: ", err) return } defer file.Close() - logger.Infof("dumped state (%s) to %s\n", hash, path) + guilogger.Infof("dumped state (%s) to %s\n", hash, path) file.Write(stateDump) } diff --git a/cmd/mist/ext_app.go b/cmd/mist/ext_app.go index cb014aec4..8af9778bc 100644 --- a/cmd/mist/ext_app.go +++ b/cmd/mist/ext_app.go @@ -74,7 +74,7 @@ func (app *ExtApplication) run() { err := app.container.Create() if err != nil { - logger.Errorln(err) + guilogger.Errorln(err) return } diff --git a/cmd/mist/flags.go b/cmd/mist/flags.go index 3aa2e21c8..7de2a881d 100644 --- a/cmd/mist/flags.go +++ b/cmd/mist/flags.go @@ -28,7 +28,6 @@ import ( "runtime" "bitbucket.org/kardianos/osext" - "github.com/ethereum/go-ethereum/ethlog" "github.com/ethereum/go-ethereum/vm" ) @@ -117,7 +116,7 @@ func Init() { flag.StringVar(&Datadir, "datadir", defaultDataDir(), "specifies the datadir to use") flag.StringVar(&ConfigFile, "conf", defaultConfigFile, "config file") flag.StringVar(&DebugFile, "debug", "", "debug file (no debugging if not set)") - flag.IntVar(&LogLevel, "loglevel", int(ethlog.InfoLevel), "loglevel: 0-5: silent,error,warn,info,debug,debug detail)") + flag.IntVar(&LogLevel, "loglevel", int(repllogger.InfoLevel), "loglevel: 0-5: silent,error,warn,info,debug,debug detail)") flag.StringVar(&AssetPath, "asset_path", defaultAssetPath(), "absolute path to GUI assets directory") diff --git a/cmd/mist/gui.go b/cmd/mist/gui.go index c917ad06e..4ae92a340 100644 --- a/cmd/mist/gui.go +++ b/cmd/mist/gui.go @@ -33,11 +33,11 @@ import ( "github.com/ethereum/go-ethereum" "github.com/ethereum/go-ethereum/chain" "github.com/ethereum/go-ethereum/ethdb" - "github.com/ethereum/go-ethereum/ethlog" "github.com/ethereum/go-ethereum/ethminer" "github.com/ethereum/go-ethereum/ethpipe" "github.com/ethereum/go-ethereum/ethutil" "github.com/ethereum/go-ethereum/ethwire" + "github.com/ethereum/go-ethereum/logger" "gopkg.in/qml.v1" ) @@ -64,7 +64,7 @@ func LoadExtension(path string) (uintptr, error) { } */ -var logger = ethlog.NewLogger("GUI") +var guilogger = logger.NewLogger("GUI") type Gui struct { // The main application window @@ -81,7 +81,7 @@ type Gui struct { txDb *ethdb.LDBDatabase - logLevel ethlog.LogLevel + logLevel logger.LogLevel open bool pipe *ethpipe.JSPipe @@ -93,7 +93,7 @@ type Gui struct { plugins map[string]plugin miner *ethminer.Miner - stdLog ethlog.LogSystem + stdLog logger.LogSystem } // Create GUI, but doesn't start it @@ -104,7 +104,7 @@ func NewWindow(ethereum *eth.Ethereum, config *ethutil.ConfigManager, clientIden } pipe := ethpipe.NewJSPipe(ethereum) - gui := &Gui{eth: ethereum, txDb: db, pipe: pipe, logLevel: ethlog.LogLevel(logLevel), Session: session, open: false, clientIdentity: clientIdentity, config: config, plugins: make(map[string]plugin)} + gui := &Gui{eth: ethereum, txDb: db, pipe: pipe, logLevel: logger.LogLevel(logLevel), Session: session, open: false, clientIdentity: clientIdentity, config: config, plugins: make(map[string]plugin)} data, _ := ethutil.ReadAllFile(path.Join(ethutil.Config.ExecPath, "plugins.json")) json.Unmarshal([]byte(data), &gui.plugins) @@ -155,36 +155,36 @@ func (gui *Gui) Start(assetPath string) { addlog = true } if err != nil { - logger.Errorln("asset not found: you can set an alternative asset path on the command line using option 'asset_path'", err) + guilogger.Errorln("asset not found: you can set an alternative asset path on the command line using option 'asset_path'", err) panic(err) } - logger.Infoln("Starting GUI") + guilogger.Infoln("Starting GUI") gui.open = true win.Show() - // only add the gui logger after window is shown otherwise slider wont be shown + // only add the gui guilogger after window is shown otherwise slider wont be shown if addlog { - ethlog.AddLogSystem(gui) + logger.AddLogSystem(gui) } win.Wait() - // need to silence gui logger after window closed otherwise logsystem hangs (but do not save loglevel) - gui.logLevel = ethlog.Silence + // need to silence gui guilogger after window closed otherwise logsystem hangs (but do not save loglevel) + gui.logLevel = logger.Silence gui.open = false } func (gui *Gui) Stop() { if gui.open { - gui.logLevel = ethlog.Silence + gui.logLevel = logger.Silence gui.open = false gui.win.Hide() } gui.uiLib.jsEngine.Stop() - logger.Infoln("Stopped") + guilogger.Infoln("Stopped") } func (gui *Gui) showWallet(context *qml.Context) (*qml.Window, error) { @@ -229,17 +229,17 @@ func (gui *Gui) createWindow(comp qml.Object) *qml.Window { func (gui *Gui) ImportAndSetPrivKey(secret string) bool { err := gui.eth.KeyManager().InitFromString(gui.Session, 0, secret) if err != nil { - logger.Errorln("unable to import: ", err) + guilogger.Errorln("unable to import: ", err) return false } - logger.Errorln("successfully imported: ", err) + guilogger.Errorln("successfully imported: ", err) return true } func (gui *Gui) CreateAndSetPrivKey() (string, string, string, string) { err := gui.eth.KeyManager().Init(gui.Session, 0, true) if err != nil { - logger.Errorln("unable to create key: ", err) + guilogger.Errorln("unable to create key: ", err) return "", "", "", "" } return gui.eth.KeyManager().KeyPair().AsStrings() @@ -387,7 +387,7 @@ func (gui *Gui) update() { }() for _, plugin := range gui.plugins { - logger.Infoln("Loading plugin ", plugin.Name) + guilogger.Infoln("Loading plugin ", plugin.Name) gui.win.Root().Call("addPlugin", plugin.Path, "") } diff --git a/cmd/mist/html_container.go b/cmd/mist/html_container.go index 96bae1a9a..755d5ea6e 100644 --- a/cmd/mist/html_container.go +++ b/cmd/mist/html_container.go @@ -98,12 +98,12 @@ func (app *HtmlApplication) NewWatcher(quitChan chan bool) { app.watcher, err = fsnotify.NewWatcher() if err != nil { - logger.Infoln("Could not create new auto-reload watcher:", err) + guilogger.Infoln("Could not create new auto-reload watcher:", err) return } err = app.watcher.Watch(app.RootFolder()) if err != nil { - logger.Infoln("Could not start auto-reload watcher:", err) + guilogger.Infoln("Could not start auto-reload watcher:", err) return } for _, folder := range app.RecursiveFolders() { @@ -119,11 +119,11 @@ func (app *HtmlApplication) NewWatcher(quitChan chan bool) { app.watcher.Close() break out case <-app.watcher.Event: - //logger.Debugln("Got event:", ev) + //guilogger.Debugln("Got event:", ev) app.webView.Call("reload") case err := <-app.watcher.Error: // TODO: Do something here - logger.Infoln("Watcher error:", err) + guilogger.Infoln("Watcher error:", err) } } }() diff --git a/cmd/mist/main.go b/cmd/mist/main.go index e739bbff5..b7282d9b5 100644 --- a/cmd/mist/main.go +++ b/cmd/mist/main.go @@ -22,7 +22,7 @@ import ( "runtime" "github.com/ethereum/go-ethereum" - "github.com/ethereum/go-ethereum/ethlog" + "github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/utils" "gopkg.in/qml.v1" ) @@ -108,5 +108,5 @@ func main() { } // this blocks the thread ethereum.WaitForShutdown() - ethlog.Flush() + logger.Flush() } diff --git a/cmd/mist/qml_container.go b/cmd/mist/qml_container.go index 3318786e7..13a50d988 100644 --- a/cmd/mist/qml_container.go +++ b/cmd/mist/qml_container.go @@ -50,7 +50,7 @@ func (app *QmlApplication) Create() error { component, err := app.engine.LoadFile(path) if err != nil { - logger.Warnln(err) + guilogger.Warnln(err) } app.win = component.CreateWindow(nil) diff --git a/cmd/mist/ui_lib.go b/cmd/mist/ui_lib.go index b12fab603..6fffa845f 100644 --- a/cmd/mist/ui_lib.go +++ b/cmd/mist/ui_lib.go @@ -62,7 +62,7 @@ func NewUiLib(engine *qml.Engine, eth *eth.Ethereum, assetPath string) *UiLib { } func (self *UiLib) Notef(args []interface{}) { - logger.Infoln(args...) + guilogger.Infoln(args...) } func (self *UiLib) LookupDomain(domain string) string { @@ -158,7 +158,7 @@ func (ui *UiLib) OpenBrowser() { func (ui *UiLib) Muted(content string) { component, err := ui.engine.LoadFile(ui.AssetPath("qml/muted.qml")) if err != nil { - logger.Debugln(err) + guilogger.Debugln(err) return } diff --git a/ethereum.go b/ethereum.go index 4f2ca85e8..9b456ad7c 100644 --- a/ethereum.go +++ b/ethereum.go @@ -16,11 +16,11 @@ import ( "github.com/ethereum/go-ethereum/chain" "github.com/ethereum/go-ethereum/crypto" - "github.com/ethereum/go-ethereum/ethlog" "github.com/ethereum/go-ethereum/ethstate" "github.com/ethereum/go-ethereum/ethutil" "github.com/ethereum/go-ethereum/ethwire" "github.com/ethereum/go-ethereum/event" + "github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/rpc" ) @@ -29,7 +29,7 @@ const ( seedNodeAddress = "poc-7.ethdev.com:30303" ) -var ethlogger = ethlog.NewLogger("SERV") +var loggerger = logger.NewLogger("SERV") func eachPeer(peers *list.List, callback func(*Peer, *list.Element)) { // Loop thru the peers and close them (if we had them) @@ -104,7 +104,7 @@ func New(db ethutil.Database, clientIdentity ethwire.ClientIdentity, keyManager if usePnp { nat, err = Discover() if err != nil { - ethlogger.Debugln("UPnP failed", err) + loggerger.Debugln("UPnP failed", err) } } @@ -217,7 +217,7 @@ func (s *Ethereum) AddPeer(conn net.Conn) { if s.peers.Len() < s.MaxPeers { peer.Start() } else { - ethlogger.Debugf("Max connected peers reached. Not adding incoming peer.") + loggerger.Debugf("Max connected peers reached. Not adding incoming peer.") } } } @@ -277,7 +277,7 @@ func (s *Ethereum) ConnectToPeer(addr string) error { if phost == chost { alreadyConnected = true - //ethlogger.Debugf("Peer %s already added.\n", chost) + //loggerger.Debugf("Peer %s already added.\n", chost) return } }) @@ -397,12 +397,12 @@ func (s *Ethereum) Start(seed bool) { // Bind to addr and port ln, err := net.Listen("tcp", ":"+s.Port) if err != nil { - ethlogger.Warnf("Port %s in use. Connection listening disabled. Acting as client", s.Port) + loggerger.Warnf("Port %s in use. Connection listening disabled. Acting as client", s.Port) s.listening = false } else { s.listening = true // Starting accepting connections - ethlogger.Infoln("Ready and accepting connections") + loggerger.Infoln("Ready and accepting connections") // Start the peer handler go s.peerHandler(ln) } @@ -419,7 +419,7 @@ func (s *Ethereum) Start(seed bool) { if seed { s.Seed() } - ethlogger.Infoln("Server started") + loggerger.Infoln("Server started") } func (s *Ethereum) Seed() { @@ -428,11 +428,11 @@ func (s *Ethereum) Seed() { ips := PastPeers() if len(ips) > 0 { for _, ip := range ips { - ethlogger.Infoln("Connecting to previous peer ", ip) + loggerger.Infoln("Connecting to previous peer ", ip) s.ConnectToPeer(ip) } } else { - ethlogger.Debugln("Retrieving seed nodes") + loggerger.Debugln("Retrieving seed nodes") // Eth-Go Bootstrapping ips, er := net.LookupIP("seed.bysh.me") @@ -440,7 +440,7 @@ func (s *Ethereum) Seed() { peers := []string{} for _, ip := range ips { node := fmt.Sprintf("%s:%d", ip.String(), 30303) - ethlogger.Debugln("Found DNS Go Peer:", node) + loggerger.Debugln("Found DNS Go Peer:", node) peers = append(peers, node) } s.ProcessPeerList(peers) @@ -460,11 +460,11 @@ func (s *Ethereum) Seed() { for _, a := range addr { // Build string out of SRV port and Resolved IP peer := net.JoinHostPort(a, port) - ethlogger.Debugln("Found DNS Bootstrap Peer:", peer) + loggerger.Debugln("Found DNS Bootstrap Peer:", peer) peers = append(peers, peer) } } else { - ethlogger.Debugln("Couldn't resolve :", target) + loggerger.Debugln("Couldn't resolve :", target) } } // Connect to Peer list @@ -480,7 +480,7 @@ func (s *Ethereum) peerHandler(listener net.Listener) { for { conn, err := listener.Accept() if err != nil { - ethlogger.Debugln(err) + loggerger.Debugln(err) continue } @@ -519,7 +519,7 @@ func (s *Ethereum) Stop() { s.stateManager.Stop() s.blockPool.Stop() - ethlogger.Infoln("Server stopped") + loggerger.Infoln("Server stopped") close(s.shutdownChan) } @@ -541,13 +541,13 @@ out: var err error _, err = s.nat.AddPortMapping("TCP", int(lport), int(lport), "eth listen port", 20*60) if err != nil { - ethlogger.Debugln("can't add UPnP port mapping:", err) + loggerger.Debugln("can't add UPnP port mapping:", err) break out } if first && err == nil { _, err = s.nat.GetExternalAddress() if err != nil { - ethlogger.Debugln("UPnP can't get external address:", err) + loggerger.Debugln("UPnP can't get external address:", err) continue out } first = false @@ -561,9 +561,9 @@ out: timer.Stop() if err := s.nat.DeletePortMapping("TCP", int(lport), int(lport)); err != nil { - ethlogger.Debugln("unable to remove UPnP port mapping:", err) + loggerger.Debugln("unable to remove UPnP port mapping:", err) } else { - ethlogger.Debugln("succesfully disestablished UPnP port mapping") + loggerger.Debugln("succesfully disestablished UPnP port mapping") } } diff --git a/ethlog/example_test.go b/ethlog/example_test.go deleted file mode 100644 index 2532f36c1..000000000 --- a/ethlog/example_test.go +++ /dev/null @@ -1,21 +0,0 @@ -package ethlog - -import "os" - -func ExampleLogger() { - logger := NewLogger("TAG") - logger.Infoln("so awesome") // prints [TAG] so awesome - logger.Infof("this %q is raw", "coin") // prints [TAG] this "coin" is raw -} - -func ExampleLogSystem() { - filename := "test.log" - file, _ := os.OpenFile(filename, os.O_RDWR|os.O_CREATE, os.ModePerm) - fileLog := NewStdLogSystem(file, 0, WarnLevel) - AddLogSystem(fileLog) - - stdoutLog := NewStdLogSystem(os.Stdout, 0, WarnLevel) - AddLogSystem(stdoutLog) - - NewLogger("TAG").Warnln("reactor meltdown") // writes to both logs -} diff --git a/ethlog/loggers.go b/ethlog/loggers.go deleted file mode 100644 index 986bb1759..000000000 --- a/ethlog/loggers.go +++ /dev/null @@ -1,248 +0,0 @@ -/* -Package ethlog implements a multi-output leveled logger. - -Other packages use tagged logger to send log messages to shared -(process-wide) logging engine. The shared logging engine dispatches to -multiple log systems. The log level can be set separately per log -system. - -Logging is asynchronous and does not block the caller. Message -formatting is performed by the caller goroutine to avoid incorrect -logging of mutable state. -*/ -package ethlog - -import ( - "fmt" - "io" - "log" - "os" - "sync" - "sync/atomic" -) - -// LogSystem is implemented by log output devices. -// All methods can be called concurrently from multiple goroutines. -type LogSystem interface { - GetLogLevel() LogLevel - SetLogLevel(i LogLevel) - LogPrint(LogLevel, string) -} - -type message struct { - level LogLevel - msg string -} - -type LogLevel uint8 - -const ( - // Standard log levels - Silence LogLevel = iota - ErrorLevel - WarnLevel - InfoLevel - DebugLevel - DebugDetailLevel -) - -var ( - logMessageC = make(chan message) - addSystemC = make(chan LogSystem) - flushC = make(chan chan struct{}) - resetC = make(chan chan struct{}) -) - -func init() { - go dispatchLoop() -} - -// each system can buffer this many messages before -// blocking incoming log messages. -const sysBufferSize = 500 - -func dispatchLoop() { - var ( - systems []LogSystem - systemIn []chan message - systemWG sync.WaitGroup - ) - bootSystem := func(sys LogSystem) { - in := make(chan message, sysBufferSize) - systemIn = append(systemIn, in) - systemWG.Add(1) - go sysLoop(sys, in, &systemWG) - } - - for { - select { - case msg := <-logMessageC: - for _, c := range systemIn { - c <- msg - } - - case sys := <-addSystemC: - systems = append(systems, sys) - bootSystem(sys) - - case waiter := <-resetC: - // reset means terminate all systems - for _, c := range systemIn { - close(c) - } - systems = nil - systemIn = nil - systemWG.Wait() - close(waiter) - - case waiter := <-flushC: - // flush means reboot all systems - for _, c := range systemIn { - close(c) - } - systemIn = nil - systemWG.Wait() - for _, sys := range systems { - bootSystem(sys) - } - close(waiter) - } - } -} - -func sysLoop(sys LogSystem, in <-chan message, wg *sync.WaitGroup) { - for msg := range in { - if sys.GetLogLevel() >= msg.level { - sys.LogPrint(msg.level, msg.msg) - } - } - wg.Done() -} - -// Reset removes all active log systems. -// It blocks until all current messages have been delivered. -func Reset() { - waiter := make(chan struct{}) - resetC <- waiter - <-waiter -} - -// Flush waits until all current log messages have been dispatched to -// the active log systems. -func Flush() { - waiter := make(chan struct{}) - flushC <- waiter - <-waiter -} - -// AddLogSystem starts printing messages to the given LogSystem. -func AddLogSystem(sys LogSystem) { - addSystemC <- sys -} - -// A Logger prints messages prefixed by a given tag. It provides named -// Printf and Println style methods for all loglevels. Each ethereum -// component should have its own logger with a unique prefix. -type Logger struct { - tag string -} - -func NewLogger(tag string) *Logger { - return &Logger{"[" + tag + "] "} -} - -func (logger *Logger) sendln(level LogLevel, v ...interface{}) { - logMessageC <- message{level, logger.tag + fmt.Sprintln(v...)} -} - -func (logger *Logger) sendf(level LogLevel, format string, v ...interface{}) { - logMessageC <- message{level, logger.tag + fmt.Sprintf(format, v...)} -} - -// Errorln writes a message with ErrorLevel. -func (logger *Logger) Errorln(v ...interface{}) { - logger.sendln(ErrorLevel, v...) -} - -// Warnln writes a message with WarnLevel. -func (logger *Logger) Warnln(v ...interface{}) { - logger.sendln(WarnLevel, v...) -} - -// Infoln writes a message with InfoLevel. -func (logger *Logger) Infoln(v ...interface{}) { - logger.sendln(InfoLevel, v...) -} - -// Debugln writes a message with DebugLevel. -func (logger *Logger) Debugln(v ...interface{}) { - logger.sendln(DebugLevel, v...) -} - -// DebugDetailln writes a message with DebugDetailLevel. -func (logger *Logger) DebugDetailln(v ...interface{}) { - logger.sendln(DebugDetailLevel, v...) -} - -// Errorf writes a message with ErrorLevel. -func (logger *Logger) Errorf(format string, v ...interface{}) { - logger.sendf(ErrorLevel, format, v...) -} - -// Warnf writes a message with WarnLevel. -func (logger *Logger) Warnf(format string, v ...interface{}) { - logger.sendf(WarnLevel, format, v...) -} - -// Infof writes a message with InfoLevel. -func (logger *Logger) Infof(format string, v ...interface{}) { - logger.sendf(InfoLevel, format, v...) -} - -// Debugf writes a message with DebugLevel. -func (logger *Logger) Debugf(format string, v ...interface{}) { - logger.sendf(DebugLevel, format, v...) -} - -// DebugDetailf writes a message with DebugDetailLevel. -func (logger *Logger) DebugDetailf(format string, v ...interface{}) { - logger.sendf(DebugDetailLevel, format, v...) -} - -// Fatalln writes a message with ErrorLevel and exits the program. -func (logger *Logger) Fatalln(v ...interface{}) { - logger.sendln(ErrorLevel, v...) - Flush() - os.Exit(0) -} - -// Fatalf writes a message with ErrorLevel and exits the program. -func (logger *Logger) Fatalf(format string, v ...interface{}) { - logger.sendf(ErrorLevel, format, v...) - Flush() - os.Exit(0) -} - -// NewStdLogSystem creates a LogSystem that prints to the given writer. -// The flag values are defined package log. -func NewStdLogSystem(writer io.Writer, flags int, level LogLevel) LogSystem { - logger := log.New(writer, "", flags) - return &stdLogSystem{logger, uint32(level)} -} - -type stdLogSystem struct { - logger *log.Logger - level uint32 -} - -func (t *stdLogSystem) LogPrint(level LogLevel, msg string) { - t.logger.Print(msg) -} - -func (t *stdLogSystem) SetLogLevel(i LogLevel) { - atomic.StoreUint32(&t.level, uint32(i)) -} - -func (t *stdLogSystem) GetLogLevel() LogLevel { - return LogLevel(atomic.LoadUint32(&t.level)) -} diff --git a/ethlog/loggers_test.go b/ethlog/loggers_test.go deleted file mode 100644 index cf92e3cc6..000000000 --- a/ethlog/loggers_test.go +++ /dev/null @@ -1,174 +0,0 @@ -package ethlog - -import ( - "io/ioutil" - "math/rand" - "os" - "sync" - "testing" - "time" -) - -type TestLogSystem struct { - mutex sync.Mutex - output string - level LogLevel -} - -func (ls *TestLogSystem) LogPrint(level LogLevel, msg string) { - ls.mutex.Lock() - ls.output += msg - ls.mutex.Unlock() -} - -func (ls *TestLogSystem) SetLogLevel(i LogLevel) { - ls.mutex.Lock() - ls.level = i - ls.mutex.Unlock() -} - -func (ls *TestLogSystem) GetLogLevel() LogLevel { - ls.mutex.Lock() - defer ls.mutex.Unlock() - return ls.level -} - -func (ls *TestLogSystem) CheckOutput(t *testing.T, expected string) { - ls.mutex.Lock() - output := ls.output - ls.mutex.Unlock() - if output != expected { - t.Errorf("log output mismatch:\n got: %q\n want: %q\n", output, expected) - } -} - -type blockedLogSystem struct { - LogSystem - unblock chan struct{} -} - -func (ls blockedLogSystem) LogPrint(level LogLevel, msg string) { - <-ls.unblock - ls.LogSystem.LogPrint(level, msg) -} - -func TestLoggerFlush(t *testing.T) { - Reset() - - logger := NewLogger("TEST") - ls := blockedLogSystem{&TestLogSystem{level: WarnLevel}, make(chan struct{})} - AddLogSystem(ls) - for i := 0; i < 5; i++ { - // these writes shouldn't hang even though ls is blocked - logger.Errorf(".") - } - - beforeFlush := time.Now() - time.AfterFunc(80*time.Millisecond, func() { close(ls.unblock) }) - Flush() // this should hang for approx. 80ms - if blockd := time.Now().Sub(beforeFlush); blockd < 80*time.Millisecond { - t.Errorf("Flush didn't block long enough, blocked for %v, should've been >= 80ms", blockd) - } - - ls.LogSystem.(*TestLogSystem).CheckOutput(t, "[TEST] .[TEST] .[TEST] .[TEST] .[TEST] .") -} - -func TestLoggerPrintln(t *testing.T) { - Reset() - - logger := NewLogger("TEST") - testLogSystem := &TestLogSystem{level: WarnLevel} - AddLogSystem(testLogSystem) - logger.Errorln("error") - logger.Warnln("warn") - logger.Infoln("info") - logger.Debugln("debug") - Flush() - - testLogSystem.CheckOutput(t, "[TEST] error\n[TEST] warn\n") -} - -func TestLoggerPrintf(t *testing.T) { - Reset() - - logger := NewLogger("TEST") - testLogSystem := &TestLogSystem{level: WarnLevel} - AddLogSystem(testLogSystem) - logger.Errorf("error to %v\n", []int{1, 2, 3}) - logger.Warnf("warn %%d %d", 5) - logger.Infof("info") - logger.Debugf("debug") - Flush() - testLogSystem.CheckOutput(t, "[TEST] error to [1 2 3]\n[TEST] warn %d 5") -} - -func TestMultipleLogSystems(t *testing.T) { - Reset() - - logger := NewLogger("TEST") - testLogSystem0 := &TestLogSystem{level: ErrorLevel} - testLogSystem1 := &TestLogSystem{level: WarnLevel} - AddLogSystem(testLogSystem0) - AddLogSystem(testLogSystem1) - logger.Errorln("error") - logger.Warnln("warn") - Flush() - - testLogSystem0.CheckOutput(t, "[TEST] error\n") - testLogSystem1.CheckOutput(t, "[TEST] error\n[TEST] warn\n") -} - -func TestFileLogSystem(t *testing.T) { - Reset() - - logger := NewLogger("TEST") - filename := "test.log" - file, _ := os.OpenFile(filename, os.O_RDWR|os.O_CREATE, os.ModePerm) - testLogSystem := NewStdLogSystem(file, 0, WarnLevel) - AddLogSystem(testLogSystem) - logger.Errorf("error to %s\n", filename) - logger.Warnln("warn") - Flush() - contents, _ := ioutil.ReadFile(filename) - output := string(contents) - if output != "[TEST] error to test.log\n[TEST] warn\n" { - t.Error("Expected contents of file 'test.log': '[TEST] error to test.log\\n[TEST] warn\\n', got ", output) - } else { - os.Remove(filename) - } -} - -func TestNoLogSystem(t *testing.T) { - Reset() - - logger := NewLogger("TEST") - logger.Warnln("warn") - Flush() -} - -func TestConcurrentAddSystem(t *testing.T) { - rand.Seed(time.Now().Unix()) - Reset() - - logger := NewLogger("TEST") - stop := make(chan struct{}) - writer := func() { - select { - case <-stop: - return - default: - logger.Infoln("foo") - Flush() - } - } - - go writer() - go writer() - - stopTime := time.Now().Add(100 * time.Millisecond) - for time.Now().Before(stopTime) { - time.Sleep(time.Duration(rand.Intn(20)) * time.Millisecond) - AddLogSystem(NewStdLogSystem(ioutil.Discard, 0, InfoLevel)) - } - close(stop) -} diff --git a/ethminer/miner.go b/ethminer/miner.go index c2e973f32..42c989eee 100644 --- a/ethminer/miner.go +++ b/ethminer/miner.go @@ -5,12 +5,12 @@ import ( "sort" "github.com/ethereum/go-ethereum/chain" - "github.com/ethereum/go-ethereum/ethlog" "github.com/ethereum/go-ethereum/ethwire" "github.com/ethereum/go-ethereum/event" + "github.com/ethereum/go-ethereum/logger" ) -var logger = ethlog.NewLogger("MINER") +var minerlogger = logger.NewLogger("MINER") type Miner struct { pow chain.PoW @@ -70,12 +70,12 @@ func (miner *Miner) Start() { //miner.ethereum.StateManager().Prepare(miner.block.State(), miner.block.State()) go miner.listener() - logger.Infoln("Started") + minerlogger.Infoln("Started") mux.Post(Event{Started, miner}) } func (miner *Miner) Stop() { - logger.Infoln("Stopping...") + minerlogger.Infoln("Stopping...") miner.events.Unsubscribe() miner.ethereum.EventMux().Post(Event{Stopped, miner}) } @@ -91,10 +91,10 @@ func (miner *Miner) listener() { miner.stopMining() block := event.Block - //logger.Infoln("Got new block via Reactor") + //minerlogger.Infoln("Got new block via Reactor") if bytes.Compare(miner.ethereum.ChainManager().CurrentBlock.Hash(), block.Hash()) == 0 { // TODO: Perhaps continue mining to get some uncle rewards - //logger.Infoln("New top block found resetting state") + //minerlogger.Infoln("New top block found resetting state") // Filter out which Transactions we have that were not in this block var newtxs []*chain.Transaction @@ -112,7 +112,7 @@ func (miner *Miner) listener() { miner.txs = newtxs } else { if bytes.Compare(block.PrevHash, miner.ethereum.ChainManager().CurrentBlock.PrevHash) == 0 { - logger.Infoln("Adding uncle block") + minerlogger.Infoln("Adding uncle block") miner.uncles = append(miner.uncles, block) } } @@ -180,7 +180,7 @@ func (self *Miner) mineNewBlock() { coinbase.SetGasPool(self.block.CalcGasLimit(parent)) receipts, txs, unhandledTxs, erroneous, err := stateManager.ProcessTransactions(coinbase, self.block.State(), self.block, self.block, self.txs) if err != nil { - logger.Debugln(err) + minerlogger.Debugln(err) } self.ethereum.TxPool().RemoveSet(erroneous) self.txs = append(txs, unhandledTxs...) @@ -193,7 +193,7 @@ func (self *Miner) mineNewBlock() { self.block.State().Update() - logger.Infof("Mining on block. Includes %v transactions", len(self.txs)) + minerlogger.Infof("Mining on block. Includes %v transactions", len(self.txs)) // Find a valid nonce nonce := self.pow.Search(self.block, self.powQuitChan) @@ -201,11 +201,11 @@ func (self *Miner) mineNewBlock() { self.block.Nonce = nonce err := self.ethereum.StateManager().Process(self.block) if err != nil { - logger.Infoln(err) + minerlogger.Infoln(err) } else { self.ethereum.Broadcast(ethwire.MsgBlockTy, []interface{}{self.block.Value().Val}) - logger.Infof("🔨 Mined block %x\n", self.block.Hash()) - logger.Infoln(self.block) + minerlogger.Infof("🔨 Mined block %x\n", self.block.Hash()) + minerlogger.Infoln(self.block) // Gather the new batch of transactions currently in the tx pool self.txs = self.ethereum.TxPool().CurrentTransactions() self.ethereum.EventMux().Post(chain.NewBlockEvent{self.block}) diff --git a/ethpipe/js_pipe.go b/ethpipe/js_pipe.go index deb3cf091..eb365f007 100644 --- a/ethpipe/js_pipe.go +++ b/ethpipe/js_pipe.go @@ -233,7 +233,7 @@ func (self *JSPipe) Transact(key, toStr, valueStr, gasStr, gasPriceStr, codeStr self.obj.TxPool().QueueTransaction(tx) if contractCreation { - logger.Infof("Contract addr %x", tx.CreationAddress(self.World().State())) + pipelogger.Infof("Contract addr %x", tx.CreationAddress(self.World().State())) } return NewJSReciept(contractCreation, tx.CreationAddress(self.World().State()), tx.Hash(), keyPair.Address()), nil diff --git a/ethpipe/pipe.go b/ethpipe/pipe.go index 7663a1984..f6fee3923 100644 --- a/ethpipe/pipe.go +++ b/ethpipe/pipe.go @@ -6,13 +6,13 @@ import ( "github.com/ethereum/go-ethereum/chain" "github.com/ethereum/go-ethereum/crypto" - "github.com/ethereum/go-ethereum/ethlog" "github.com/ethereum/go-ethereum/ethstate" "github.com/ethereum/go-ethereum/ethutil" + "github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/vm" ) -var logger = ethlog.NewLogger("PIPE") +var pipelogger = logger.NewLogger("PIPE") type VmVars struct { State *ethstate.State @@ -143,7 +143,7 @@ func (self *Pipe) Transact(key *crypto.KeyPair, rec []byte, value, gas, price *e if contractCreation { addr := tx.CreationAddress(self.World().State()) - logger.Infof("Contract addr %x\n", addr) + pipelogger.Infof("Contract addr %x\n", addr) return addr, nil } @@ -155,7 +155,7 @@ func (self *Pipe) PushTx(tx *chain.Transaction) ([]byte, error) { self.obj.TxPool().QueueTransaction(tx) if tx.Recipient == nil { addr := tx.CreationAddress(self.World().State()) - logger.Infof("Contract addr %x\n", addr) + pipelogger.Infof("Contract addr %x\n", addr) return addr, nil } return tx.Hash(), nil diff --git a/ethstate/state.go b/ethstate/state.go index 48efeae46..9025194e5 100644 --- a/ethstate/state.go +++ b/ethstate/state.go @@ -3,12 +3,12 @@ package ethstate import ( "math/big" - "github.com/ethereum/go-ethereum/ethlog" "github.com/ethereum/go-ethereum/ethtrie" "github.com/ethereum/go-ethereum/ethutil" + "github.com/ethereum/go-ethereum/logger" ) -var statelogger = ethlog.NewLogger("STATE") +var statelogger = logger.NewLogger("STATE") // States within the ethereum protocol are used to store anything // within the merkle trie. States take care of caching and storing diff --git a/javascript/javascript_runtime.go b/javascript/javascript_runtime.go index 36850021d..c539c4348 100644 --- a/javascript/javascript_runtime.go +++ b/javascript/javascript_runtime.go @@ -9,16 +9,16 @@ import ( "github.com/ethereum/go-ethereum" "github.com/ethereum/go-ethereum/chain" - "github.com/ethereum/go-ethereum/ethlog" "github.com/ethereum/go-ethereum/ethpipe" "github.com/ethereum/go-ethereum/ethstate" "github.com/ethereum/go-ethereum/ethutil" "github.com/ethereum/go-ethereum/event" + "github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/utils" "github.com/obscuren/otto" ) -var jsrelogger = ethlog.NewLogger("JSRE") +var jsrelogger = logger.NewLogger("JSRE") type JSRE struct { ethereum *eth.Ethereum diff --git a/logger/example_test.go b/logger/example_test.go new file mode 100644 index 000000000..c624252b8 --- /dev/null +++ b/logger/example_test.go @@ -0,0 +1,21 @@ +package logger + +import "os" + +func ExampleLogger() { + logger := NewLogger("TAG") + logger.Infoln("so awesome") // prints [TAG] so awesome + logger.Infof("this %q is raw", "coin") // prints [TAG] this "coin" is raw +} + +func ExampleLogSystem() { + filename := "test.log" + file, _ := os.OpenFile(filename, os.O_RDWR|os.O_CREATE, os.ModePerm) + fileLog := NewStdLogSystem(file, 0, WarnLevel) + AddLogSystem(fileLog) + + stdoutLog := NewStdLogSystem(os.Stdout, 0, WarnLevel) + AddLogSystem(stdoutLog) + + NewLogger("TAG").Warnln("reactor meltdown") // writes to both logs +} diff --git a/logger/loggers.go b/logger/loggers.go new file mode 100644 index 000000000..1bf7bfa0e --- /dev/null +++ b/logger/loggers.go @@ -0,0 +1,248 @@ +/* +Package logger implements a multi-output leveled logger. + +Other packages use tagged logger to send log messages to shared +(process-wide) logging engine. The shared logging engine dispatches to +multiple log systems. The log level can be set separately per log +system. + +Logging is asynchronous and does not block the caller. Message +formatting is performed by the caller goroutine to avoid incorrect +logging of mutable state. +*/ +package logger + +import ( + "fmt" + "io" + "log" + "os" + "sync" + "sync/atomic" +) + +// LogSystem is implemented by log output devices. +// All methods can be called concurrently from multiple goroutines. +type LogSystem interface { + GetLogLevel() LogLevel + SetLogLevel(i LogLevel) + LogPrint(LogLevel, string) +} + +type message struct { + level LogLevel + msg string +} + +type LogLevel uint8 + +const ( + // Standard log levels + Silence LogLevel = iota + ErrorLevel + WarnLevel + InfoLevel + DebugLevel + DebugDetailLevel +) + +var ( + logMessageC = make(chan message) + addSystemC = make(chan LogSystem) + flushC = make(chan chan struct{}) + resetC = make(chan chan struct{}) +) + +func init() { + go dispatchLoop() +} + +// each system can buffer this many messages before +// blocking incoming log messages. +const sysBufferSize = 500 + +func dispatchLoop() { + var ( + systems []LogSystem + systemIn []chan message + systemWG sync.WaitGroup + ) + bootSystem := func(sys LogSystem) { + in := make(chan message, sysBufferSize) + systemIn = append(systemIn, in) + systemWG.Add(1) + go sysLoop(sys, in, &systemWG) + } + + for { + select { + case msg := <-logMessageC: + for _, c := range systemIn { + c <- msg + } + + case sys := <-addSystemC: + systems = append(systems, sys) + bootSystem(sys) + + case waiter := <-resetC: + // reset means terminate all systems + for _, c := range systemIn { + close(c) + } + systems = nil + systemIn = nil + systemWG.Wait() + close(waiter) + + case waiter := <-flushC: + // flush means reboot all systems + for _, c := range systemIn { + close(c) + } + systemIn = nil + systemWG.Wait() + for _, sys := range systems { + bootSystem(sys) + } + close(waiter) + } + } +} + +func sysLoop(sys LogSystem, in <-chan message, wg *sync.WaitGroup) { + for msg := range in { + if sys.GetLogLevel() >= msg.level { + sys.LogPrint(msg.level, msg.msg) + } + } + wg.Done() +} + +// Reset removes all active log systems. +// It blocks until all current messages have been delivered. +func Reset() { + waiter := make(chan struct{}) + resetC <- waiter + <-waiter +} + +// Flush waits until all current log messages have been dispatched to +// the active log systems. +func Flush() { + waiter := make(chan struct{}) + flushC <- waiter + <-waiter +} + +// AddLogSystem starts printing messages to the given LogSystem. +func AddLogSystem(sys LogSystem) { + addSystemC <- sys +} + +// A Logger prints messages prefixed by a given tag. It provides named +// Printf and Println style methods for all loglevels. Each ethereum +// component should have its own logger with a unique prefix. +type Logger struct { + tag string +} + +func NewLogger(tag string) *Logger { + return &Logger{"[" + tag + "] "} +} + +func (logger *Logger) sendln(level LogLevel, v ...interface{}) { + logMessageC <- message{level, logger.tag + fmt.Sprintln(v...)} +} + +func (logger *Logger) sendf(level LogLevel, format string, v ...interface{}) { + logMessageC <- message{level, logger.tag + fmt.Sprintf(format, v...)} +} + +// Errorln writes a message with ErrorLevel. +func (logger *Logger) Errorln(v ...interface{}) { + logger.sendln(ErrorLevel, v...) +} + +// Warnln writes a message with WarnLevel. +func (logger *Logger) Warnln(v ...interface{}) { + logger.sendln(WarnLevel, v...) +} + +// Infoln writes a message with InfoLevel. +func (logger *Logger) Infoln(v ...interface{}) { + logger.sendln(InfoLevel, v...) +} + +// Debugln writes a message with DebugLevel. +func (logger *Logger) Debugln(v ...interface{}) { + logger.sendln(DebugLevel, v...) +} + +// DebugDetailln writes a message with DebugDetailLevel. +func (logger *Logger) DebugDetailln(v ...interface{}) { + logger.sendln(DebugDetailLevel, v...) +} + +// Errorf writes a message with ErrorLevel. +func (logger *Logger) Errorf(format string, v ...interface{}) { + logger.sendf(ErrorLevel, format, v...) +} + +// Warnf writes a message with WarnLevel. +func (logger *Logger) Warnf(format string, v ...interface{}) { + logger.sendf(WarnLevel, format, v...) +} + +// Infof writes a message with InfoLevel. +func (logger *Logger) Infof(format string, v ...interface{}) { + logger.sendf(InfoLevel, format, v...) +} + +// Debugf writes a message with DebugLevel. +func (logger *Logger) Debugf(format string, v ...interface{}) { + logger.sendf(DebugLevel, format, v...) +} + +// DebugDetailf writes a message with DebugDetailLevel. +func (logger *Logger) DebugDetailf(format string, v ...interface{}) { + logger.sendf(DebugDetailLevel, format, v...) +} + +// Fatalln writes a message with ErrorLevel and exits the program. +func (logger *Logger) Fatalln(v ...interface{}) { + logger.sendln(ErrorLevel, v...) + Flush() + os.Exit(0) +} + +// Fatalf writes a message with ErrorLevel and exits the program. +func (logger *Logger) Fatalf(format string, v ...interface{}) { + logger.sendf(ErrorLevel, format, v...) + Flush() + os.Exit(0) +} + +// NewStdLogSystem creates a LogSystem that prints to the given writer. +// The flag values are defined package log. +func NewStdLogSystem(writer io.Writer, flags int, level LogLevel) LogSystem { + logger := log.New(writer, "", flags) + return &stdLogSystem{logger, uint32(level)} +} + +type stdLogSystem struct { + logger *log.Logger + level uint32 +} + +func (t *stdLogSystem) LogPrint(level LogLevel, msg string) { + t.logger.Print(msg) +} + +func (t *stdLogSystem) SetLogLevel(i LogLevel) { + atomic.StoreUint32(&t.level, uint32(i)) +} + +func (t *stdLogSystem) GetLogLevel() LogLevel { + return LogLevel(atomic.LoadUint32(&t.level)) +} diff --git a/logger/loggers_test.go b/logger/loggers_test.go new file mode 100644 index 000000000..adc4df016 --- /dev/null +++ b/logger/loggers_test.go @@ -0,0 +1,174 @@ +package logger + +import ( + "io/ioutil" + "math/rand" + "os" + "sync" + "testing" + "time" +) + +type TestLogSystem struct { + mutex sync.Mutex + output string + level LogLevel +} + +func (ls *TestLogSystem) LogPrint(level LogLevel, msg string) { + ls.mutex.Lock() + ls.output += msg + ls.mutex.Unlock() +} + +func (ls *TestLogSystem) SetLogLevel(i LogLevel) { + ls.mutex.Lock() + ls.level = i + ls.mutex.Unlock() +} + +func (ls *TestLogSystem) GetLogLevel() LogLevel { + ls.mutex.Lock() + defer ls.mutex.Unlock() + return ls.level +} + +func (ls *TestLogSystem) CheckOutput(t *testing.T, expected string) { + ls.mutex.Lock() + output := ls.output + ls.mutex.Unlock() + if output != expected { + t.Errorf("log output mismatch:\n got: %q\n want: %q\n", output, expected) + } +} + +type blockedLogSystem struct { + LogSystem + unblock chan struct{} +} + +func (ls blockedLogSystem) LogPrint(level LogLevel, msg string) { + <-ls.unblock + ls.LogSystem.LogPrint(level, msg) +} + +func TestLoggerFlush(t *testing.T) { + Reset() + + logger := NewLogger("TEST") + ls := blockedLogSystem{&TestLogSystem{level: WarnLevel}, make(chan struct{})} + AddLogSystem(ls) + for i := 0; i < 5; i++ { + // these writes shouldn't hang even though ls is blocked + logger.Errorf(".") + } + + beforeFlush := time.Now() + time.AfterFunc(80*time.Millisecond, func() { close(ls.unblock) }) + Flush() // this should hang for approx. 80ms + if blockd := time.Now().Sub(beforeFlush); blockd < 80*time.Millisecond { + t.Errorf("Flush didn't block long enough, blocked for %v, should've been >= 80ms", blockd) + } + + ls.LogSystem.(*TestLogSystem).CheckOutput(t, "[TEST] .[TEST] .[TEST] .[TEST] .[TEST] .") +} + +func TestLoggerPrintln(t *testing.T) { + Reset() + + logger := NewLogger("TEST") + testLogSystem := &TestLogSystem{level: WarnLevel} + AddLogSystem(testLogSystem) + logger.Errorln("error") + logger.Warnln("warn") + logger.Infoln("info") + logger.Debugln("debug") + Flush() + + testLogSystem.CheckOutput(t, "[TEST] error\n[TEST] warn\n") +} + +func TestLoggerPrintf(t *testing.T) { + Reset() + + logger := NewLogger("TEST") + testLogSystem := &TestLogSystem{level: WarnLevel} + AddLogSystem(testLogSystem) + logger.Errorf("error to %v\n", []int{1, 2, 3}) + logger.Warnf("warn %%d %d", 5) + logger.Infof("info") + logger.Debugf("debug") + Flush() + testLogSystem.CheckOutput(t, "[TEST] error to [1 2 3]\n[TEST] warn %d 5") +} + +func TestMultipleLogSystems(t *testing.T) { + Reset() + + logger := NewLogger("TEST") + testLogSystem0 := &TestLogSystem{level: ErrorLevel} + testLogSystem1 := &TestLogSystem{level: WarnLevel} + AddLogSystem(testLogSystem0) + AddLogSystem(testLogSystem1) + logger.Errorln("error") + logger.Warnln("warn") + Flush() + + testLogSystem0.CheckOutput(t, "[TEST] error\n") + testLogSystem1.CheckOutput(t, "[TEST] error\n[TEST] warn\n") +} + +func TestFileLogSystem(t *testing.T) { + Reset() + + logger := NewLogger("TEST") + filename := "test.log" + file, _ := os.OpenFile(filename, os.O_RDWR|os.O_CREATE, os.ModePerm) + testLogSystem := NewStdLogSystem(file, 0, WarnLevel) + AddLogSystem(testLogSystem) + logger.Errorf("error to %s\n", filename) + logger.Warnln("warn") + Flush() + contents, _ := ioutil.ReadFile(filename) + output := string(contents) + if output != "[TEST] error to test.log\n[TEST] warn\n" { + t.Error("Expected contents of file 'test.log': '[TEST] error to test.log\\n[TEST] warn\\n', got ", output) + } else { + os.Remove(filename) + } +} + +func TestNoLogSystem(t *testing.T) { + Reset() + + logger := NewLogger("TEST") + logger.Warnln("warn") + Flush() +} + +func TestConcurrentAddSystem(t *testing.T) { + rand.Seed(time.Now().Unix()) + Reset() + + logger := NewLogger("TEST") + stop := make(chan struct{}) + writer := func() { + select { + case <-stop: + return + default: + logger.Infoln("foo") + Flush() + } + } + + go writer() + go writer() + + stopTime := time.Now().Add(100 * time.Millisecond) + for time.Now().Before(stopTime) { + time.Sleep(time.Duration(rand.Intn(20)) * time.Millisecond) + AddLogSystem(NewStdLogSystem(ioutil.Discard, 0, InfoLevel)) + } + close(stop) +} diff --git a/peer.go b/peer.go index ab25e5709..36db68023 100644 --- a/peer.go +++ b/peer.go @@ -13,12 +13,12 @@ import ( "time" "github.com/ethereum/go-ethereum/chain" - "github.com/ethereum/go-ethereum/ethlog" "github.com/ethereum/go-ethereum/ethutil" "github.com/ethereum/go-ethereum/ethwire" + "github.com/ethereum/go-ethereum/logger" ) -var peerlogger = ethlog.NewLogger("PEER") +var peerlogger = logger.NewLogger("PEER") const ( // The size of the output buffer for writing messages @@ -696,18 +696,18 @@ func (self *Peer) handleStatus(msg *ethwire.Msg) { ) if bytes.Compare(self.ethereum.ChainManager().Genesis().Hash(), genesis) != 0 { - ethlogger.Warnf("Invalid genisis hash %x. Disabling [eth]\n", genesis) + loggerger.Warnf("Invalid genisis hash %x. Disabling [eth]\n", genesis) return } if netVersion != NetVersion { - ethlogger.Warnf("Invalid network version %d. Disabling [eth]\n", netVersion) + loggerger.Warnf("Invalid network version %d. Disabling [eth]\n", netVersion) return } /* if protoVersion != ProtocolVersion { - ethlogger.Warnf("Invalid protocol version %d. Disabling [eth]\n", protoVersion) + loggerger.Warnf("Invalid protocol version %d. Disabling [eth]\n", protoVersion) return } */ @@ -723,7 +723,7 @@ func (self *Peer) handleStatus(msg *ethwire.Msg) { // fetch hashes from highest TD node. self.FetchHashes() - ethlogger.Infof("Peer is [eth] capable. (TD = %v ~ %x)", self.td, self.bestHash) + loggerger.Infof("Peer is [eth] capable. (TD = %v ~ %x)", self.td, self.bestHash) } @@ -819,7 +819,7 @@ func (p *Peer) handleHandshake(msg *ethwire.Msg) { switch cap { case "eth": if ver != ProtocolVersion { - ethlogger.Warnf("Invalid protocol version %d. Disabling [eth]\n", ver) + loggerger.Warnf("Invalid protocol version %d. Disabling [eth]\n", ver) continue } p.pushStatus() @@ -828,7 +828,7 @@ func (p *Peer) handleHandshake(msg *ethwire.Msg) { capsStrs = append(capsStrs, cap) } - ethlogger.Infof("Added peer (%s) %d / %d (%v)\n", p.conn.RemoteAddr(), p.ethereum.Peers().Len(), p.ethereum.MaxPeers, capsStrs) + peerlogger.Infof("Added peer (%s) %d / %d (%v)\n", p.conn.RemoteAddr(), p.ethereum.Peers().Len(), p.ethereum.MaxPeers, capsStrs) peerlogger.Debugln(p) } diff --git a/rpc/packages.go b/rpc/packages.go index 07d3ddc9a..31500867c 100644 --- a/rpc/packages.go +++ b/rpc/packages.go @@ -208,7 +208,7 @@ func (p *EthereumApi) GetStorageAt(args *GetStorageArgs, reply *string) error { i, _ := new(big.Int).SetString(args.Key, 10) hx = ethutil.Bytes2Hex(i.Bytes()) } - logger.Debugf("GetStorageAt(%s, %s)\n", args.Address, hx) + jsonlogger.Debugf("GetStorageAt(%s, %s)\n", args.Address, hx) value := state.Storage(ethutil.Hex2Bytes(hx)) *reply = NewSuccessRes(GetStorageAtRes{Address: args.Address, Key: args.Key, Value: value.Str()}) return nil diff --git a/rpc/server.go b/rpc/server.go index ce53266f0..20f75ce67 100644 --- a/rpc/server.go +++ b/rpc/server.go @@ -6,11 +6,11 @@ import ( "net/rpc" "net/rpc/jsonrpc" - "github.com/ethereum/go-ethereum/ethlog" "github.com/ethereum/go-ethereum/ethpipe" + "github.com/ethereum/go-ethereum/logger" ) -var logger = ethlog.NewLogger("JSON") +var jsonlogger = logger.NewLogger("JSON") type JsonRpcServer struct { quit chan bool @@ -28,7 +28,7 @@ out: } } - logger.Infoln("Shutdown JSON-RPC server") + jsonlogger.Infoln("Shutdown JSON-RPC server") } func (s *JsonRpcServer) Stop() { @@ -36,7 +36,7 @@ func (s *JsonRpcServer) Stop() { } func (s *JsonRpcServer) Start() { - logger.Infoln("Starting JSON-RPC server") + jsonlogger.Infoln("Starting JSON-RPC server") go s.exitHandler() rpc.Register(&EthereumApi{pipe: s.pipe}) rpc.HandleHTTP() @@ -44,10 +44,10 @@ func (s *JsonRpcServer) Start() { for { conn, err := s.listener.Accept() if err != nil { - logger.Infoln("Error starting JSON-RPC:", err) + jsonlogger.Infoln("Error starting JSON-RPC:", err) break } - logger.Debugln("Incoming request.") + jsonlogger.Debugln("Incoming request.") go jsonrpc.ServeConn(conn) } } diff --git a/utils/cmd.go b/utils/cmd.go index fd684640b..7ee41d042 100644 --- a/utils/cmd.go +++ b/utils/cmd.go @@ -16,15 +16,15 @@ import ( "github.com/ethereum/go-ethereum" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethdb" - "github.com/ethereum/go-ethereum/ethlog" "github.com/ethereum/go-ethereum/ethminer" "github.com/ethereum/go-ethereum/ethpipe" "github.com/ethereum/go-ethereum/ethutil" "github.com/ethereum/go-ethereum/ethwire" + "github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/rpc" ) -var logger = ethlog.NewLogger("CLI") +var clilogger = logger.NewLogger("CLI") var interruptCallbacks = []func(os.Signal){} // Register interrupt handlers callbacks @@ -38,7 +38,7 @@ func HandleInterrupt() { go func() { signal.Notify(c, os.Interrupt) for sig := range c { - logger.Errorf("Shutting down (%v) ... \n", sig) + clilogger.Errorf("Shutting down (%v) ... \n", sig) RunInterruptCallbacks(sig) } }() @@ -100,7 +100,7 @@ func InitDataDir(Datadir string) { } } -func InitLogging(Datadir string, LogFile string, LogLevel int, DebugFile string) ethlog.LogSystem { +func InitLogging(Datadir string, LogFile string, LogLevel int, DebugFile string) logger.LogSystem { var writer io.Writer if LogFile == "" { writer = os.Stdout @@ -108,11 +108,11 @@ func InitLogging(Datadir string, LogFile string, LogLevel int, DebugFile string) writer = openLogFile(Datadir, LogFile) } - sys := ethlog.NewStdLogSystem(writer, log.LstdFlags, ethlog.LogLevel(LogLevel)) - ethlog.AddLogSystem(sys) + sys := logger.NewStdLogSystem(writer, log.LstdFlags, logger.LogLevel(LogLevel)) + logger.AddLogSystem(sys) if DebugFile != "" { writer = openLogFile(Datadir, DebugFile) - ethlog.AddLogSystem(ethlog.NewStdLogSystem(writer, log.LstdFlags, ethlog.DebugLevel)) + logger.AddLogSystem(logger.NewStdLogSystem(writer, log.LstdFlags, logger.DebugLevel)) } return sys @@ -129,10 +129,10 @@ func InitConfig(vmType int, ConfigFile string, Datadir string, EnvPrefix string) func exit(err error) { status := 0 if err != nil { - logger.Errorln("Fatal: ", err) + clilogger.Errorln("Fatal: ", err) status = 1 } - ethlog.Flush() + logger.Flush() os.Exit(status) } @@ -145,14 +145,14 @@ func NewDatabase() ethutil.Database { } func NewClientIdentity(clientIdentifier, version, customIdentifier string) *ethwire.SimpleClientIdentity { - logger.Infoln("identity created") + clilogger.Infoln("identity created") return ethwire.NewSimpleClientIdentity(clientIdentifier, version, customIdentifier) } func NewEthereum(db ethutil.Database, clientIdentity ethwire.ClientIdentity, keyManager *crypto.KeyManager, usePnp bool, OutboundPort string, MaxPeer int) *eth.Ethereum { ethereum, err := eth.New(db, clientIdentity, keyManager, eth.CapDefault, usePnp) if err != nil { - logger.Fatalln("eth start err:", err) + clilogger.Fatalln("eth start err:", err) } ethereum.Port = OutboundPort ethereum.MaxPeers = MaxPeer @@ -160,16 +160,16 @@ func NewEthereum(db ethutil.Database, clientIdentity ethwire.ClientIdentity, key } func StartEthereum(ethereum *eth.Ethereum, UseSeed bool) { - logger.Infof("Starting %s", ethereum.ClientIdentity()) + clilogger.Infof("Starting %s", ethereum.ClientIdentity()) ethereum.Start(UseSeed) RegisterInterrupt(func(sig os.Signal) { ethereum.Stop() - ethlog.Flush() + logger.Flush() }) } func ShowGenesis(ethereum *eth.Ethereum) { - logger.Infoln(ethereum.ChainManager().Genesis()) + clilogger.Infoln(ethereum.ChainManager().Genesis()) exit(nil) } @@ -246,7 +246,7 @@ func StartRpc(ethereum *eth.Ethereum, RpcPort int) { var err error ethereum.RpcServer, err = rpc.NewJsonRpcServer(ethpipe.NewJSPipe(ethereum), RpcPort) if err != nil { - logger.Errorf("Could not start RPC interface (port %v): %v", RpcPort, err) + clilogger.Errorf("Could not start RPC interface (port %v): %v", RpcPort, err) } else { go ethereum.RpcServer.Start() } @@ -264,7 +264,7 @@ func StartMining(ethereum *eth.Ethereum) bool { addr := ethereum.KeyManager().Address() go func() { - logger.Infoln("Start mining") + clilogger.Infoln("Start mining") if miner == nil { miner = ethminer.NewDefaultMiner(addr, ethereum) } @@ -299,7 +299,7 @@ func FormatTransactionData(data string) []byte { func StopMining(ethereum *eth.Ethereum) bool { if ethereum.Mining && miner != nil { miner.Stop() - logger.Infoln("Stopped mining") + clilogger.Infoln("Stopped mining") ethereum.Mining = false return true diff --git a/vm/common.go b/vm/common.go index 16a0b5523..9514ff6d3 100644 --- a/vm/common.go +++ b/vm/common.go @@ -3,11 +3,11 @@ package vm import ( "math/big" - "github.com/ethereum/go-ethereum/ethlog" "github.com/ethereum/go-ethereum/ethutil" + "github.com/ethereum/go-ethereum/logger" ) -var vmlogger = ethlog.NewLogger("VM") +var vmlogger = logger.NewLogger("VM") type Type int diff --git a/websocket/client.go b/websocket/client.go index 1ff0d3f64..d961816e8 100644 --- a/websocket/client.go +++ b/websocket/client.go @@ -73,13 +73,13 @@ func (c *Client) Listen() { // Listen write request via chanel func (c *Client) listenWrite() { - logger.Debugln("Listening write to client") + wslogger.Debugln("Listening write to client") for { select { // send message to the client case msg := <-c.ch: - logger.Debugln("Send:", msg) + wslogger.Debugln("Send:", msg) ws.JSON.Send(c.ws, msg) // receive done request @@ -93,7 +93,7 @@ func (c *Client) listenWrite() { // Listen read request via chanel func (c *Client) listenRead() { - logger.Debugln("Listening read from client") + wslogger.Debugln("Listening read from client") for { select { @@ -112,7 +112,7 @@ func (c *Client) listenRead() { } else if err != nil { c.server.Err(err) } else { - logger.Debugln(&msg) + wslogger.Debugln(&msg) if c.onMessage != nil { c.onMessage(c, &msg) } diff --git a/websocket/server.go b/websocket/server.go index 1b4c00433..5fd923a0c 100644 --- a/websocket/server.go +++ b/websocket/server.go @@ -3,12 +3,12 @@ package websocket import ( "net/http" - "github.com/ethereum/go-ethereum/ethlog" + "github.com/ethereum/go-ethereum/logger" ws "code.google.com/p/go.net/websocket" ) -var logger = ethlog.NewLogger("WS") +var wslogger = logger.NewLogger("WS") // Chat server. type Server struct { @@ -68,10 +68,10 @@ func (s *Server) Err(err error) { } func (s *Server) servHTTP() { - logger.Debugln("Serving http", s.httpServ) + wslogger.Debugln("Serving http", s.httpServ) err := http.ListenAndServe(s.httpServ, nil) - logger.Warnln(err) + wslogger.Warnln(err) } func (s *Server) MessageFunc(f MsgFunc) { @@ -81,7 +81,7 @@ func (s *Server) MessageFunc(f MsgFunc) { // Listen and serve. // It serves client connection and broadcast request. func (s *Server) Listen() { - logger.Debugln("Listening server...") + wslogger.Debugln("Listening server...") // ws handler onConnected := func(ws *ws.Conn) { @@ -102,7 +102,7 @@ func (s *Server) Listen() { s := ws.Server{Handler: ws.Handler(onConnected)} s.ServeHTTP(w, req) }) - logger.Debugln("Created handler") + wslogger.Debugln("Created handler") go s.servHTTP() @@ -118,7 +118,7 @@ func (s *Server) Listen() { delete(s.clients, c.id) case err := <-s.errCh: - logger.Debugln("Error:", err.Error()) + wslogger.Debugln("Error:", err.Error()) case <-s.doneCh: return -- cgit v1.2.3