mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-06-02 09:15:04 +02:00
util: Establish global logger object.
The object encapsulates logging configuration, and in a later commit, set up routines will also be moved into the class.
This commit is contained in:
@@ -19,13 +19,7 @@ static const bool DEFAULT_LOGIPS = false;
|
||||
static const bool DEFAULT_LOGTIMESTAMPS = true;
|
||||
extern const char * const DEFAULT_DEBUGLOGFILE;
|
||||
|
||||
extern bool fPrintToConsole;
|
||||
extern bool fPrintToDebugLog;
|
||||
|
||||
extern bool fLogTimestamps;
|
||||
extern bool fLogTimeMicros;
|
||||
extern bool fLogIPs;
|
||||
extern std::atomic<bool> fReopenDebugLog;
|
||||
|
||||
extern std::atomic<uint32_t> logCategories;
|
||||
|
||||
@@ -61,7 +55,39 @@ namespace BCLog {
|
||||
LEVELDB = (1 << 20),
|
||||
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);
|
||||
|
||||
/** Returns whether logs will be written to any output */
|
||||
bool Enabled() const { return fPrintToConsole || fPrintToDebugLog; }
|
||||
};
|
||||
|
||||
} // namespace BCLog
|
||||
|
||||
extern BCLog::Logger* const g_logger;
|
||||
|
||||
/** Return true if log accepts specified category */
|
||||
static inline bool LogAcceptCategory(uint32_t category)
|
||||
{
|
||||
@@ -77,9 +103,6 @@ std::vector<CLogCategoryActive> ListActiveLogCategories();
|
||||
/** Return true if str parses as a log category and set the flags in f */
|
||||
bool GetLogCategory(uint32_t *f, const std::string *str);
|
||||
|
||||
/** Send a string to the log output */
|
||||
int LogPrintStr(const std::string &str);
|
||||
|
||||
/** Get format string from VA_ARGS for error reporting */
|
||||
template<typename... Args> std::string FormatStringFromLogArgs(const char *fmt, const Args&... args) { return fmt; }
|
||||
|
||||
@@ -99,7 +122,7 @@ template<typename T, typename... Args> static inline void MarkUsed(const T& t, c
|
||||
#define LogPrint(category, ...) do { MarkUsed(__VA_ARGS__); } while(0)
|
||||
#else
|
||||
#define LogPrintf(...) do { \
|
||||
if (fPrintToConsole || fPrintToDebugLog) { \
|
||||
if (g_logger->Enabled()) { \
|
||||
std::string _log_msg_; /* Unlikely name to avoid shadowing variables */ \
|
||||
try { \
|
||||
_log_msg_ = tfm::format(__VA_ARGS__); \
|
||||
@@ -107,7 +130,7 @@ template<typename T, typename... Args> static inline void MarkUsed(const T& t, c
|
||||
/* Original format string will have newline so don't add one here */ \
|
||||
_log_msg_ = "Error \"" + std::string(fmterr.what()) + "\" while formatting log message: " + FormatStringFromLogArgs(__VA_ARGS__); \
|
||||
} \
|
||||
LogPrintStr(_log_msg_); \
|
||||
g_logger->LogPrintStr(_log_msg_); \
|
||||
} \
|
||||
} while(0)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user