aboutsummaryrefslogtreecommitdiff
path: root/src/logging.h
diff options
context:
space:
mode:
authorJim Posen <jimpo@coinbase.com>2018-04-11 10:03:21 -0700
committerJim Posen <jimpo@coinbase.com>2018-04-27 16:09:59 -0700
commitf55f4fcf05a53fdf618b4c69ddcf4c43b14e84c2 (patch)
treed5e549d544757a7d0aa1bd2fbcef5be376af8823 /src/logging.h
parent17266a13067c061783e0428c6200985ed8872b16 (diff)
downloadbitcoin-f55f4fcf05a53fdf618b4c69ddcf4c43b14e84c2.tar.xz
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.
Diffstat (limited to 'src/logging.h')
-rw-r--r--src/logging.h47
1 files changed, 35 insertions, 12 deletions
diff --git a/src/logging.h b/src/logging.h
index 4053f75acf..1ccf9136fd 100644
--- a/src/logging.h
+++ b/src/logging.h
@@ -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)