Changeset View
Changeset View
Standalone View
Standalone View
src/logging.h
Show All 9 Lines | |||||
#include <atomic> | #include <atomic> | ||||
#include <cstdint> | #include <cstdint> | ||||
#include <string> | #include <string> | ||||
static const bool DEFAULT_LOGTIMEMICROS = false; | static const bool DEFAULT_LOGTIMEMICROS = false; | ||||
static const bool DEFAULT_LOGIPS = false; | static const bool DEFAULT_LOGIPS = false; | ||||
static const bool DEFAULT_LOGTIMESTAMPS = true; | static const bool DEFAULT_LOGTIMESTAMPS = true; | ||||
extern bool fPrintToConsole; | |||||
extern bool fPrintToDebugLog; | |||||
extern bool fLogTimestamps; | |||||
extern bool fLogTimeMicros; | |||||
extern bool fLogIPs; | extern bool fLogIPs; | ||||
extern std::atomic<bool> fReopenDebugLog; | extern std::atomic<bool> fReopenDebugLog; | ||||
extern std::atomic<uint32_t> logCategories; | extern std::atomic<uint32_t> logCategories; | ||||
namespace BCLog { | namespace BCLog { | ||||
enum LogFlags : uint32_t { | enum LogFlags : uint32_t { | ||||
NONE = 0, | NONE = 0, | ||||
NET = (1 << 0), | NET = (1 << 0), | ||||
TOR = (1 << 1), | TOR = (1 << 1), | ||||
MEMPOOL = (1 << 2), | MEMPOOL = (1 << 2), | ||||
HTTP = (1 << 3), | HTTP = (1 << 3), | ||||
BENCH = (1 << 4), | BENCH = (1 << 4), | ||||
ZMQ = (1 << 5), | ZMQ = (1 << 5), | ||||
Show All 9 Lines | enum LogFlags : uint32_t { | ||||
PROXY = (1 << 15), | PROXY = (1 << 15), | ||||
MEMPOOLREJ = (1 << 16), | MEMPOOLREJ = (1 << 16), | ||||
LIBEVENT = (1 << 17), | LIBEVENT = (1 << 17), | ||||
COINDB = (1 << 18), | COINDB = (1 << 18), | ||||
QT = (1 << 19), | QT = (1 << 19), | ||||
LEVELDB = (1 << 20), | LEVELDB = (1 << 20), | ||||
ALL = ~uint32_t(0), | ALL = ~uint32_t(0), | ||||
}; | }; | ||||
} | |||||
class Logger { | |||||
private: | |||||
/** | |||||
* fStartedNewLine is a state variable that will suppress printing of the | |||||
* timestamp when multiple calls are made that don't end in a newline. | |||||
*/ | |||||
std::atomic_bool fStartedNewLine{true}; | |||||
std::string LogTimestampStr(const std::string &str); | |||||
public: | |||||
bool fPrintToConsole = false; | |||||
bool fPrintToDebugLog = true; | |||||
bool fLogTimestamps = DEFAULT_LOGTIMESTAMPS; | |||||
bool fLogTimeMicros = DEFAULT_LOGTIMEMICROS; | |||||
std::atomic<bool> fReopenDebugLog{false}; | |||||
/** Send a string to the log output */ | |||||
int LogPrintStr(const std::string &str); | |||||
}; | |||||
} // namespace BCLog | |||||
BCLog::Logger &GetLogger(); | |||||
/** Return true if log accepts specified category */ | /** Return true if log accepts specified category */ | ||||
static inline bool LogAcceptCategory(uint32_t category) { | static inline bool LogAcceptCategory(uint32_t category) { | ||||
return (logCategories.load(std::memory_order_relaxed) & category) != 0; | return (logCategories.load(std::memory_order_relaxed) & category) != 0; | ||||
} | } | ||||
/** Returns a string with the supported log categories */ | /** Returns a string with the supported log categories */ | ||||
std::string ListLogCategories(); | std::string ListLogCategories(); | ||||
/** Return true if str parses as a log category and set the flags in f */ | /** Return true if str parses as a log category and set the flags in f */ | ||||
bool GetLogCategory(uint32_t *f, const std::string *str); | bool GetLogCategory(uint32_t *f, const std::string *str); | ||||
/** Send a string to the log output */ | |||||
int LogPrintStr(const std::string &str); | |||||
#define LogPrint(category, ...) \ | #define LogPrint(category, ...) \ | ||||
do { \ | do { \ | ||||
if (LogAcceptCategory((category))) { \ | if (LogAcceptCategory((category))) { \ | ||||
LogPrintStr(tfm::format(__VA_ARGS__)); \ | GetLogger().LogPrintStr(tfm::format(__VA_ARGS__)); \ | ||||
} \ | } \ | ||||
} while (0) | } while (0) | ||||
#define LogPrintf(...) \ | #define LogPrintf(...) \ | ||||
do { \ | do { \ | ||||
LogPrintStr(tfm::format(__VA_ARGS__)); \ | GetLogger().LogPrintStr(tfm::format(__VA_ARGS__)); \ | ||||
} while (0) | } while (0) | ||||
void OpenDebugLog(); | void OpenDebugLog(); | ||||
void ShrinkDebugFile(); | void ShrinkDebugFile(); | ||||
#endif // BITCOIN_LOGGING_H | #endif // BITCOIN_LOGGING_H |