Skip to content
This repository has been archived by the owner on Oct 28, 2021. It is now read-only.

Commit

Permalink
StructuredLogger's functions all become static and call the singleton…
Browse files Browse the repository at this point in the history
… getter.
  • Loading branch information
LefterisJP committed Mar 2, 2015
1 parent 936cbdb commit f8b4b3c
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 52 deletions.
6 changes: 3 additions & 3 deletions eth/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
52 changes: 26 additions & 26 deletions libdevcore/StructuredLogger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@ 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

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 "";
}
Expand All @@ -54,39 +54,39 @@ 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");
}
}

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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -131,17 +131,17 @@ 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");
}
}

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;
Expand All @@ -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;
Expand All @@ -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");
}
}

Expand Down
30 changes: 13 additions & 17 deletions libdevcore/StructuredLogger.h
Original file line number Diff line number Diff line change
Expand Up @@ -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()

}
4 changes: 2 additions & 2 deletions libethereum/BlockChain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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(),
Expand Down
2 changes: 1 addition & 1 deletion libethereum/Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down
2 changes: 1 addition & 1 deletion libethereum/State.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 ??
Expand Down
2 changes: 1 addition & 1 deletion libp2p/Host.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ void Host::registerPeer(std::shared_ptr<Session> _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,
Expand Down
2 changes: 1 addition & 1 deletion libp2p/Session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit f8b4b3c

Please sign in to comment.