From f8b4b3cbf5ba69242cea6bab30a09913124ced9f Mon Sep 17 00:00:00 2001 From: Lefteris Karapetsas Date: Mon, 2 Mar 2015 17:25:01 +0100 Subject: [PATCH] StructuredLogger's functions all become static and call the singleton getter. --- eth/main.cpp | 6 ++-- libdevcore/StructuredLogger.cpp | 52 ++++++++++++++++----------------- libdevcore/StructuredLogger.h | 30 +++++++++---------- libethereum/BlockChain.cpp | 4 +-- libethereum/Client.cpp | 2 +- libethereum/State.cpp | 2 +- libp2p/Host.cpp | 2 +- libp2p/Session.cpp | 2 +- 8 files changed, 48 insertions(+), 52 deletions(-) diff --git a/eth/main.cpp b/eth/main.cpp index fa78a41909d..8014cc0fa9a 100644 --- a/eth/main.cpp +++ b/eth/main.cpp @@ -359,7 +359,7 @@ int main(int argc, char** argv) cout << credits(); - StructLog.initialize(structuredLogging, structuredLoggingFormat); + StructuredLogger::get().initialize(structuredLogging, structuredLoggingFormat); VMFactory::setKind(jit ? VMKind::JIT : VMKind::Interpreter); NetworkPreferences netPrefs(listenPort, publicIP, upnp, useLocal); auto nodesState = contents((dbPath.size() ? dbPath : getDataDir()) + "/network.rlp"); @@ -375,7 +375,7 @@ int main(int argc, char** argv) ); web3.setIdealPeerCount(peers); eth::Client* c = mode == NodeMode::Full ? web3.ethereum() : nullptr; - StructLog.starting(clientImplString, dev::Version); + StructuredLogger::starting(clientImplString, dev::Version); if (c) { c->setForceMining(forceMining); @@ -909,7 +909,7 @@ int main(int argc, char** argv) while (!g_exit) this_thread::sleep_for(chrono::milliseconds(1000)); - StructLog.stopping(clientImplString, dev::Version); + StructuredLogger::stopping(clientImplString, dev::Version); auto netData = web3.saveNetwork(); if (!netData.empty()) writeFile((dbPath.size() ? dbPath : getDataDir()) + "/network.rlp", netData); diff --git a/libdevcore/StructuredLogger.cpp b/libdevcore/StructuredLogger.cpp index 2e8b279d1b1..cb6acd5016f 100644 --- a/libdevcore/StructuredLogger.cpp +++ b/libdevcore/StructuredLogger.cpp @@ -32,7 +32,7 @@ using namespace std; namespace dev { -string StructuredLogger::timePointToString(chrono::system_clock::time_point const& _ts) const +string StructuredLogger::timePointToString(chrono::system_clock::time_point const& _ts) { // not using C++11 std::put_time due to gcc bug // http://stackoverflow.com/questions/14136833/stdput-time-implementation-status-in-gcc @@ -40,7 +40,7 @@ string StructuredLogger::timePointToString(chrono::system_clock::time_point cons char buffer[64]; time_t time = chrono::system_clock::to_time_t(_ts); tm* ptm = localtime(&time); - if (strftime(buffer, sizeof(buffer), m_timeFormat.c_str(), ptm)) + if (strftime(buffer, sizeof(buffer), get().m_timeFormat.c_str(), ptm)) return string(buffer); return ""; } @@ -54,29 +54,29 @@ void StructuredLogger::outputJson(Json::Value const& _value, std::string const& cout << event << endl << flush; } -void StructuredLogger::starting(string const& _clientImpl, const char* _ethVersion) const +void StructuredLogger::starting(string const& _clientImpl, const char* _ethVersion) { - if (m_enabled) + if (get().m_enabled) { Json::Value event; event["client_impl"] = _clientImpl; event["eth_version"] = std::string(_ethVersion); event["ts"] = timePointToString(std::chrono::system_clock::now()); - outputJson(event, "starting"); + get().outputJson(event, "starting"); } } -void StructuredLogger::stopping(string const& _clientImpl, const char* _ethVersion) const +void StructuredLogger::stopping(string const& _clientImpl, const char* _ethVersion) { - if (m_enabled) + if (get().m_enabled) { Json::Value event; event["client_impl"] = _clientImpl; event["eth_version"] = std::string(_ethVersion); event["ts"] = timePointToString(std::chrono::system_clock::now()); - outputJson(event, "stopping"); + get().outputJson(event, "stopping"); } } @@ -84,9 +84,9 @@ void StructuredLogger::p2pConnected(string const& _id, bi::tcp::endpoint const& _addr, chrono::system_clock::time_point const& _ts, string const& _remoteVersion, - unsigned int _numConnections) const + unsigned int _numConnections) { - if (m_enabled) + if (get().m_enabled) { std::stringstream addrStream; addrStream << _addr; @@ -97,13 +97,13 @@ void StructuredLogger::p2pConnected(string const& _id, event["num_connections"] = Json::Value(_numConnections); event["ts"] = timePointToString(_ts); - outputJson(event, "p2p.connected"); + get().outputJson(event, "p2p.connected"); } } -void StructuredLogger::p2pDisconnected(string const& _id, bi::tcp::endpoint const& _addr, unsigned int _numConnections) const +void StructuredLogger::p2pDisconnected(string const& _id, bi::tcp::endpoint const& _addr, unsigned int _numConnections) { - if (m_enabled) + if (get().m_enabled) { std::stringstream addrStream; addrStream << _addr; @@ -113,16 +113,16 @@ void StructuredLogger::p2pDisconnected(string const& _id, bi::tcp::endpoint cons event["num_connections"] = Json::Value(_numConnections); event["ts"] = timePointToString(chrono::system_clock::now()); - outputJson(event, "p2p.disconnected"); + get().outputJson(event, "p2p.disconnected"); } } void StructuredLogger::minedNewBlock(string const& _hash, string const& _blockNumber, string const& _chainHeadHash, - string const& _prevHash) const + string const& _prevHash) { - if (m_enabled) + if (get().m_enabled) { Json::Value event; event["block_hash"] = _hash; @@ -131,7 +131,7 @@ void StructuredLogger::minedNewBlock(string const& _hash, event["ts"] = timePointToString(std::chrono::system_clock::now()); event["block_prev_hash"] = _prevHash; - outputJson(event, "eth.miner.new_block"); + get().outputJson(event, "eth.miner.new_block"); } } @@ -139,9 +139,9 @@ void StructuredLogger::chainReceivedNewBlock(string const& _hash, string const& _blockNumber, string const& _chainHeadHash, string const& _remoteID, - string const& _prevHash) const + string const& _prevHash) { - if (m_enabled) + if (get().m_enabled) { Json::Value event; event["block_hash"] = _hash; @@ -151,16 +151,16 @@ void StructuredLogger::chainReceivedNewBlock(string const& _hash, event["ts"] = timePointToString(chrono::system_clock::now()); event["block_prev_hash"] = _prevHash; - outputJson(event, "eth.chain.received.new_block"); + get().outputJson(event, "eth.chain.received.new_block"); } } void StructuredLogger::chainNewHead(string const& _hash, string const& _blockNumber, string const& _chainHeadHash, - string const& _prevHash) const + string const& _prevHash) { - if (m_enabled) + if (get().m_enabled) { Json::Value event; event["block_hash"] = _hash; @@ -169,20 +169,20 @@ void StructuredLogger::chainNewHead(string const& _hash, event["ts"] = timePointToString(chrono::system_clock::now()); event["block_prev_hash"] = _prevHash; - outputJson(event, "eth.miner.new_block"); + get().outputJson(event, "eth.miner.new_block"); } } -void StructuredLogger::transactionReceived(string const& _hash, string const& _remoteId) const +void StructuredLogger::transactionReceived(string const& _hash, string const& _remoteId) { - if (m_enabled) + if (get().m_enabled) { Json::Value event; event["tx_hash"] = _hash; event["remote_id"] = _remoteId; event["ts"] = timePointToString(chrono::system_clock::now()); - outputJson(event, "eth.tx.received"); + get().outputJson(event, "eth.tx.received"); } } diff --git a/libdevcore/StructuredLogger.h b/libdevcore/StructuredLogger.h index ebceecdffd5..522a5115d73 100644 --- a/libdevcore/StructuredLogger.h +++ b/libdevcore/StructuredLogger.h @@ -57,43 +57,39 @@ class StructuredLogger return instance; } - void starting(std::string const& _clientImpl, const char* _ethVersion) const; - void stopping(std::string const& _clientImpl, const char* _ethVersion) const; - void p2pConnected(std::string const& _id, + static void starting(std::string const& _clientImpl, const char* _ethVersion); + static void stopping(std::string const& _clientImpl, const char* _ethVersion); + static void p2pConnected(std::string const& _id, bi::tcp::endpoint const& _addr, std::chrono::system_clock::time_point const& _ts, std::string const& _remoteVersion, - unsigned int _numConnections) const; - void p2pDisconnected(std::string const& _id, bi::tcp::endpoint const& _addr, unsigned int _numConnections) const; - void minedNewBlock(std::string const& _hash, + unsigned int _numConnections); + static void p2pDisconnected(std::string const& _id, bi::tcp::endpoint const& _addr, unsigned int _numConnections); + static void minedNewBlock(std::string const& _hash, std::string const& _blockNumber, std::string const& _chainHeadHash, - std::string const& _prevHash) const; - void chainReceivedNewBlock(std::string const& _hash, + std::string const& _prevHash); + static void chainReceivedNewBlock(std::string const& _hash, std::string const& _blockNumber, std::string const& _chainHeadHash, std::string const& _remoteID, - std::string const& _prevHash) const; - void chainNewHead(std::string const& _hash, + std::string const& _prevHash); + static void chainNewHead(std::string const& _hash, std::string const& _blockNumber, std::string const& _chainHeadHash, - std::string const& _prevHash) const; - void transactionReceived(std::string const& _hash, std::string const& _remoteId) const; + std::string const& _prevHash); + static void transactionReceived(std::string const& _hash, std::string const& _remoteId); private: // Singleton class, no copying StructuredLogger() {} StructuredLogger(StructuredLogger const&) = delete; void operator=(StructuredLogger const&) = delete; /// @returns a string representation of a timepoint - std::string timePointToString(std::chrono::system_clock::time_point const& _ts) const; + static std::string timePointToString(std::chrono::system_clock::time_point const& _ts); void outputJson(Json::Value const& _value, std::string const& _name) const; bool m_enabled; std::string m_timeFormat = "%Y-%m-%dT%H:%M:%S"; }; -/// Convenience macro to get the singleton instance -/// Calling the logging functions becomes as simple as: StructLog.transactionReceived(...) -#define StructLog StructuredLogger::get() - } diff --git a/libethereum/BlockChain.cpp b/libethereum/BlockChain.cpp index 690a8976ba2..784621a694c 100644 --- a/libethereum/BlockChain.cpp +++ b/libethereum/BlockChain.cpp @@ -318,7 +318,7 @@ h256s BlockChain::import(bytes const& _block, OverlayDB const& _db) } #endif - StructLog.chainReceivedNewBlock( + StructuredLogger::chainReceivedNewBlock( bi.headerHash(WithoutNonce).abridged(), bi.nonce.abridged(), currentHash().abridged(), @@ -338,7 +338,7 @@ h256s BlockChain::import(bytes const& _block, OverlayDB const& _db) } m_extrasDB->Put(m_writeOptions, ldb::Slice("best"), ldb::Slice((char const*)&newHash, 32)); clog(BlockChainNote) << " Imported and best" << td << ". Has" << (details(bi.parentHash).children.size() - 1) << "siblings. Route:" << toString(ret); - StructLog.chainNewHead( + StructuredLogger::chainNewHead( bi.headerHash(WithoutNonce).abridged(), bi.nonce.abridged(), currentHash().abridged(), diff --git a/libethereum/Client.cpp b/libethereum/Client.cpp index 38901643a92..910f3650e15 100644 --- a/libethereum/Client.cpp +++ b/libethereum/Client.cpp @@ -416,7 +416,7 @@ void Client::transact(Secret _secret, u256 _value, Address _dest, bytes const& _ } Transaction t(_value, _gasPrice, _gas, _dest, _data, n, _secret); // cdebug << "Nonce at " << toAddress(_secret) << " pre:" << m_preMine.transactionsFrom(toAddress(_secret)) << " post:" << m_postMine.transactionsFrom(toAddress(_secret)); - StructLog.transactionReceived(t.sha3().abridged(), t.sender().abridged()); + StructuredLogger::transactionReceived(t.sha3().abridged(), t.sender().abridged()); cnote << "New transaction " << t; m_tq.attemptImport(t.rlp()); } diff --git a/libethereum/State.cpp b/libethereum/State.cpp index 0fd94175cc6..fad9112db7a 100644 --- a/libethereum/State.cpp +++ b/libethereum/State.cpp @@ -821,7 +821,7 @@ void State::completeMine() ret.swapOut(m_currentBytes); m_currentBlock.hash = sha3(RLP(m_currentBytes)[0].data()); cnote << "Mined " << m_currentBlock.hash.abridged() << "(parent: " << m_currentBlock.parentHash.abridged() << ")"; - StructLog.minedNewBlock( + StructuredLogger::minedNewBlock( m_currentBlock.hash.abridged(), m_currentBlock.nonce.abridged(), "", //TODO: chain head hash here ?? diff --git a/libp2p/Host.cpp b/libp2p/Host.cpp index 5bee1dbc9be..bb97f06e4d5 100644 --- a/libp2p/Host.cpp +++ b/libp2p/Host.cpp @@ -159,7 +159,7 @@ void Host::registerPeer(std::shared_ptr _s, CapDescs const& _caps) { { clog(NetNote) << "p2p.host.peer.register" << _s->m_peer->id.abridged(); - StructLog.p2pConnected( + StructuredLogger::p2pConnected( _s->m_peer->id.abridged(), _s->m_peer->peerEndpoint(), _s->m_peer->m_lastConnected, diff --git a/libp2p/Session.cpp b/libp2p/Session.cpp index ffd2dbc90a2..ec0a2d0d7f6 100644 --- a/libp2p/Session.cpp +++ b/libp2p/Session.cpp @@ -468,7 +468,7 @@ void Session::drop(DisconnectReason _reason) void Session::disconnect(DisconnectReason _reason) { clogS(NetConnect) << "Disconnecting (our reason:" << reasonOf(_reason) << ")"; - StructLog.p2pDisconnected( + StructuredLogger::p2pDisconnected( m_info.id.abridged(), m_peer->peerEndpoint(), m_server->peerCount()