Browse Source

Structured logging should now work for some events

cl-refactor
Lefteris Karapetsas 10 years ago
parent
commit
063a12fd88
  1. 9
      eth/main.cpp
  2. 45
      libdevcore/StructuredLogger.cpp
  3. 14
      libdevcore/StructuredLogger.h

9
eth/main.cpp

@ -282,7 +282,7 @@ int main(int argc, char** argv)
}
else if ((arg == "-s" || arg == "--secret") && i + 1 < argc)
us = KeyPair(h256(fromHex(argv[++i])));
else if ((arg == "--structured-logging") && i + 1 < argc)
else if (arg == "--structured-logging")
structuredLogging = true;
else if ((arg == "-d" || arg == "--path" || arg == "--db-path") && i + 1 < argc)
dbPath = argv[++i];
@ -355,7 +355,7 @@ int main(int argc, char** argv)
cout << credits();
unique_ptr<StructuredLogger> structuredLogger(new StructuredLogger(structuredLogging));
StructuredLogger structuredLogger(structuredLogging);
VMFactory::setKind(jit ? VMKind::JIT : VMKind::Interpreter);
NetworkPreferences netPrefs(listenPort, publicIP, upnp, useLocal);
auto nodesState = contents((dbPath.size() ? dbPath : getDataDir()) + "/network.rlp");
@ -368,12 +368,11 @@ int main(int argc, char** argv)
netPrefs,
&nodesState,
miners,
structuredLogger.get()
structuredLogger
);
web3.setIdealPeerCount(peers);
eth::Client* c = mode == NodeMode::Full ? web3.ethereum() : nullptr;
if (structuredLogging)
structuredLogger->logStarting(clientImplString, dev::Version);
structuredLogger.logStarting(clientImplString, dev::Version);
if (c)
{
c->setForceMining(forceMining);

45
libdevcore/StructuredLogger.cpp

@ -23,6 +23,7 @@
#include "StructuredLogger.h"
#include <ctime>
#include <json/json.h>
using namespace std;
@ -30,34 +31,38 @@ using namespace std;
namespace dev
{
char const* StructuredLogger::timePointToString(chrono::system_clock::time_point const& _ts) const
string StructuredLogger::timePointToString(chrono::system_clock::time_point const& _ts) const
{
// not using C++11 std::put_time due to gcc bug
// http://stackoverflow.com/questions/14136833/stdput-time-implementation-status-in-gcc
// TODO: Format it according to Log event Requirements
char buffer[64];
time_t time = chrono::system_clock::to_time_t(_ts);
return ctime(&time);
tm* ptm = localtime(&time);
if (strftime(buffer, sizeof(buffer), m_timeFormat.c_str(), ptm))
return string(buffer);
return "";
}
void StructuredLogger::outputJson(Json::Value const* _value, std::string const& _name) const
void StructuredLogger::outputJson(Json::Value const& _value, std::string const& _name) const
{
Json::Value event;
event[_name] = _value;
cout << event;
cout << event << endl;
}
void StructuredLogger::logStarting(string const& _clientImpl, const char* _ethVersion)
void StructuredLogger::logStarting(string const& _clientImpl, const char* _ethVersion) const
{
if (m_enabled)
{
Json::Value event;
event["comment"] = "one of the first log events, before any operation is started";
event["client_implt"] = _clientImpl;
event["client_impl"] = _clientImpl;
event["eth_version"] = std::string(_ethVersion);
event["ts"] = string(timePointToString(std::chrono::system_clock::now()));
event["ts"] = timePointToString(std::chrono::system_clock::now());
outputJson(&event, "starting");
outputJson(event, "starting");
}
}
@ -74,9 +79,9 @@ void StructuredLogger::logP2PConnected(string const& _id, bi::tcp::endpoint cons
event["remote_addr"] = addrStream.str();
event["remote_id"] = _id;
event["num_connections"] = Json::Value(_numConnections);
event["ts"] = string(timePointToString(_ts));
event["ts"] = timePointToString(_ts);
outputJson(&event, "p2p.connected");
outputJson(event, "p2p.connected");
}
}
@ -91,9 +96,9 @@ void StructuredLogger::logP2PDisconnected(string const& _id, bi::tcp::endpoint c
event["remote_addr"] = addrStream.str();
event["remote_id"] = _id;
event["num_connections"] = Json::Value(_numConnections);
event["ts"] = string(timePointToString(chrono::system_clock::now()));
event["ts"] = timePointToString(chrono::system_clock::now());
outputJson(&event, "p2p.disconnected");
outputJson(event, "p2p.disconnected");
}
}
@ -107,10 +112,10 @@ void StructuredLogger::logMinedNewBlock(string const& _hash, string const& _bloc
event["block_hash"] = _hash;
event["block_number"] = _blockNumber;
event["chain_head_hash"] = _chainHeadHash;
event["ts"] = string(timePointToString(std::chrono::system_clock::now()));
event["ts"] = timePointToString(std::chrono::system_clock::now());
event["block_prev_hash"] = _prevHash;
outputJson(&event, "eth.miner.new_block");
outputJson(event, "eth.miner.new_block");
}
}
@ -125,10 +130,10 @@ void StructuredLogger::logChainReceivedNewBlock(string const& _hash, string cons
event["block_number"] = _blockNumber;
event["chain_head_hash"] = _chainHeadHash;
event["remote_id"] = _remoteID;
event["ts"] = string(timePointToString(chrono::system_clock::now()));
event["ts"] = timePointToString(chrono::system_clock::now());
event["block_prev_hash"] = _prevHash;
outputJson(&event, "eth.chain.received.new_block");
outputJson(event, "eth.chain.received.new_block");
}
}
@ -142,10 +147,10 @@ void StructuredLogger::logChainNewHead(string const& _hash, string const& _block
event["block_hash"] = _hash;
event["block_number"] = _blockNumber;
event["chain_head_hash"] = _chainHeadHash;
event["ts"] = string(timePointToString(chrono::system_clock::now()));
event["ts"] = timePointToString(chrono::system_clock::now());
event["block_prev_hash"] = _prevHash;
outputJson(&event, "eth.miner.new_block");
outputJson(event, "eth.miner.new_block");
}
}
@ -156,9 +161,9 @@ void StructuredLogger::logTransactionReceived(string const& _hash, string const&
Json::Value event;
event["tx_hash"] = _hash;
event["remote_id"] = _remoteId;
event["ts"] = string(timePointToString(chrono::system_clock::now()));
event["ts"] = timePointToString(chrono::system_clock::now());
outputJson(&event, "eth.tx.received");
outputJson(event, "eth.tx.received");
}
}

14
libdevcore/StructuredLogger.h

@ -37,10 +37,11 @@ namespace dev
class StructuredLogger
{
public:
StructuredLogger(): m_enabled(false) {}
StructuredLogger(bool _enabled): m_enabled(_enabled) {}
StructuredLogger(): m_enabled(false){}
StructuredLogger(bool _enabled, std::string const& _timeFormat = "%Y-%m-%dT%H:%M:%S"):
m_enabled(_enabled), m_timeFormat(_timeFormat) {}
void logStarting(std::string const& _clientImpl, const char* _ethVersion);
void logStarting(std::string const& _clientImpl, const char* _ethVersion) const;
void logP2PConnected(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 logP2PDisconnected(std::string const& _id, bi::tcp::endpoint const& _addr, unsigned int _numConnections) const;
@ -53,10 +54,11 @@ public:
void logTransactionReceived(std::string const& _hash, std::string const& _remoteId) const;
private:
/// @returns a string representation of a timepoint
char const* timePointToString(std::chrono::system_clock::time_point const& _ts) const;
void outputJson(Json::Value const* _value, std::string const& _name) const;
bool m_enabled;
std::string timePointToString(std::chrono::system_clock::time_point const& _ts) const;
void outputJson(Json::Value const& _value, std::string const& _name) const;
bool m_enabled;
std::string m_timeFormat = "%Y-%m-%dT%H:%M:%S";
};
}

Loading…
Cancel
Save