Changeset View
Changeset View
Standalone View
Standalone View
src/logging.cpp
- This file was added.
// Copyright (c) 2018 The Bitcoin ABC developers | |||||
// Distributed under the MIT software license, see the accompanying | |||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | |||||
#include "logging.h" | |||||
#include <iostream> | |||||
#include <sstream> | |||||
#include <thread> | |||||
static std::string FormatTimestamp(bool include_micros) { | |||||
int64_t time_micros = GetLogTimeMicros(); | |||||
std::string formatted = | |||||
DateTimeStrFormat("%Y-%m-%d %H:%M:%S", time_micros / 1000000); | |||||
if (include_micros) { | |||||
formatted += strprintf(".%06d", time_micros % 1000000); | |||||
} | |||||
return formatted; | |||||
} | |||||
static std::string FormatThreadID() { | |||||
std::stringstream ss; | |||||
ss << std::this_thread::get_id(); | |||||
return ss.str(); | |||||
} | |||||
static std::string FormatLogLevel(BCLog::Level level) { | |||||
switch (level) { | |||||
case BCLog::DEBUG: | |||||
return "debug"; | |||||
case BCLog::INFO: | |||||
return "info"; | |||||
case BCLog::WARN: | |||||
return "warn"; | |||||
case BCLog::ERROR: | |||||
return "error"; | |||||
case BCLog::FATAL: | |||||
return "fatal"; | |||||
} | |||||
return "unknown"; | |||||
} | |||||
static std::string FormatLogCategory(BCLog::LogFlags category) { | |||||
switch (category) { | |||||
case BCLog::NET: | |||||
return "net"; | |||||
case BCLog::TOR: | |||||
return "tor"; | |||||
case BCLog::MEMPOOL: | |||||
return "mempool"; | |||||
case BCLog::HTTP: | |||||
return "http"; | |||||
case BCLog::BENCH: | |||||
return "bench"; | |||||
case BCLog::ZMQ: | |||||
return "zmq"; | |||||
case BCLog::DB: | |||||
return "db"; | |||||
case BCLog::RPC: | |||||
return "rpc"; | |||||
case BCLog::ESTIMATEFEE: | |||||
return "estimatefee"; | |||||
case BCLog::ADDRMAN: | |||||
return "addrman"; | |||||
case BCLog::SELECTCOINS: | |||||
return "selectcoins"; | |||||
case BCLog::REINDEX: | |||||
return "reindex"; | |||||
case BCLog::CMPCTBLOCK: | |||||
return "cmpctblock"; | |||||
case BCLog::RAND: | |||||
return "rand"; | |||||
case BCLog::PRUNE: | |||||
return "prune"; | |||||
case BCLog::PROXY: | |||||
return "proxy"; | |||||
case BCLog::MEMPOOLREJ: | |||||
return "mempoolrej"; | |||||
case BCLog::LIBEVENT: | |||||
return "libevent"; | |||||
case BCLog::COINDB: | |||||
return "coindb"; | |||||
case BCLog::QT: | |||||
return "qt"; | |||||
case BCLog::LEVELDB: | |||||
return "leveldb"; | |||||
case BCLog::ALL:; | |||||
case BCLog::NONE:; | |||||
} | |||||
return ""; | |||||
} | |||||
BCLog::LogLine::LogLine(BCLog::Logger *logger, BCLog::Level level, | |||||
BCLog::LogFlags category) | |||||
: m_logger(logger), m_value(UniValue::VOBJ) { | |||||
if (!m_logger) return; | |||||
if (m_logger->log_timestamps) { | |||||
With("timestamp", FormatTimestamp(m_logger->log_time_micros)); | |||||
} | |||||
With("level", FormatLogLevel(level)); | |||||
if (category != BCLog::ALL) { | |||||
With("category", FormatLogCategory(category)); | |||||
} | |||||
if (m_logger->log_thread_id) { | |||||
With("thread_id", FormatThreadID()); | |||||
} | |||||
} | |||||
BCLog::LogLine::~LogLine() { | |||||
if (m_logger) m_logger->Write(*this); | |||||
} | |||||
BCLog::LogLine &BCLog::LogLine::Message(const std::string &message) { | |||||
return With("msg", message); | |||||
} | |||||
BCLog::LogLine &BCLog::LogLine::With(const std::string &key, | |||||
const UniValue &val) { | |||||
m_value.pushKV(key, val); | |||||
return *this; | |||||
} | |||||
bool BCLog::Logger::WillWriteLevel(Level level) const { | |||||
return level >= threshold_level; | |||||
} | |||||
bool BCLog::Logger::WillWriteCategory(LogFlags category) const { | |||||
return category == BCLog::ALL || (categories & category) != 0; | |||||
} | |||||
void BCLog::Logger::Write(const LogLine &log) { | |||||
std::cout << log.m_value.write() << std::endl; | |||||
} | |||||
std::unique_ptr<BCLog::Logger> g_logger; |