6#ifndef BITCOIN_LOGGING_H
7#define BITCOIN_LOGGING_H
21#include <unordered_map>
66#ifdef DEBUG_LOCKCONTENTION
89 SystemClock::time_point
now;
124 void FormatLogStrInPlace(std::string& str,
LogFlags category,
Level level, std::string_view source_file,
int source_line, std::string_view logging_function, std::string_view threadname, SystemClock::time_point now, std::chrono::seconds mocktime)
const;
126 std::string
LogTimestampStr(SystemClock::time_point now, std::chrono::seconds mocktime)
const;
129 std::list<std::function<void(
const std::string&)>> m_print_callbacks
GUARDED_BY(
m_cs) {};
165 m_print_callbacks.push_back(std::move(fun));
166 return --m_print_callbacks.end();
173 m_print_callbacks.erase(it);
194 return m_category_log_levels;
199 m_category_log_levels = levels;
251template <
typename... Args>
260 log_msg =
"Error \"" + std::string(fmterr.what()) +
"\" while formatting log message: " + fmt;
266#define LogPrintLevel_(category, level, ...) LogPrintf_(__func__, __FILE__, __LINE__, category, level, __VA_ARGS__)
269#define LogInfo(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Info, __VA_ARGS__)
270#define LogWarning(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Warning, __VA_ARGS__)
271#define LogError(...) LogPrintLevel_(BCLog::LogFlags::ALL, BCLog::Level::Error, __VA_ARGS__)
274#define LogPrintf(...) LogInfo(__VA_ARGS__)
275#define LogPrintfCategory(category, ...) LogPrintLevel_(category, BCLog::Level::Info, __VA_ARGS__)
281#define LogPrintLevel(category, level, ...) \
283 if (LogAcceptCategory((category), (level))) { \
284 LogPrintLevel_(category, level, __VA_ARGS__); \
289#define LogDebug(category, ...) LogPrintLevel(category, BCLog::Level::Debug, __VA_ARGS__)
290#define LogTrace(category, ...) LogPrintLevel(category, BCLog::Level::Trace, __VA_ARGS__)
293#define LogPrint(category, ...) LogDebug(category, __VA_ARGS__)
static std::string LogLevelToStr(BCLog::Level level)
Returns the string representation of a log level.
bool m_always_print_category_level
size_t m_buffer_lines_discarded GUARDED_BY(m_cs)
FILE *m_fileout GUARDED_BY(m_cs)
bool m_buffering GUARDED_BY(m_cs)
Buffer messages before logging can be started.
bool WillLogCategory(LogFlags category) const
std::list< BufferedLog > m_msgs_before_open GUARDED_BY(m_cs)
std::string LogTimestampStr(SystemClock::time_point now, std::chrono::seconds mocktime) const
std::atomic< uint32_t > m_categories
Log categories bitfield.
size_t m_cur_buffer_memusage GUARDED_BY(m_cs)
bool DefaultShrinkDebugFile() const
std::unordered_map< LogFlags, Level > m_category_log_levels GUARDED_BY(m_cs)
Category-specific log level. Overrides m_log_level.
bool m_log_sourcelocations
void SetCategoryLogLevel(const std::unordered_map< LogFlags, Level > &levels) EXCLUSIVE_LOCKS_REQUIRED(!m_cs)
void SetLogLevel(Level level)
std::atomic< Level > m_log_level
If there is no category-specific log level, all logs with a severity level lower than m_log_level wil...
bool WillLogCategoryLevel(LogFlags category, Level level) const EXCLUSIVE_LOCKS_REQUIRED(!m_cs)
std::list< std::function< void(conststd::string &)> >::iterator PushBackCallback(std::function< void(const std::string &)> fun) EXCLUSIVE_LOCKS_REQUIRED(!m_cs)
Connect a slot to the print signal and return the connection.
std::atomic_bool m_started_new_line
m_started_new_line is a state variable that will suppress printing of the timestamp when multiple cal...
void DisableLogging() EXCLUSIVE_LOCKS_REQUIRED(!m_cs)
Disable logging This offers a slight speedup and slightly smaller memory usage compared to leaving th...
std::vector< LogCategory > LogCategoriesList() const
Returns a vector of the log categories in alphabetical order.
bool StartLogging() EXCLUSIVE_LOCKS_REQUIRED(!m_cs)
Start logging (and flush all buffered messages)
void EnableCategory(LogFlags flag)
size_t m_max_buffer_memusage GUARDED_BY(m_cs)
void FormatLogStrInPlace(std::string &str, LogFlags category, Level level, std::string_view source_file, int source_line, std::string_view logging_function, std::string_view threadname, SystemClock::time_point now, std::chrono::seconds mocktime) const
void DeleteCallback(std::list< std::function< void(const std::string &)> >::iterator it) EXCLUSIVE_LOCKS_REQUIRED(!m_cs)
Delete a connection.
std::string GetLogPrefix(LogFlags category, Level level) const
void LogPrintStr(std::string_view str, std::string_view logging_function, std::string_view source_file, int source_line, BCLog::LogFlags category, BCLog::Level level) EXCLUSIVE_LOCKS_REQUIRED(!m_cs)
Send a string to the log output.
void DisconnectTestLogger() EXCLUSIVE_LOCKS_REQUIRED(!m_cs)
Only for testing.
std::string LogLevelsString() const
Returns a string with all user-selectable log levels.
std::atomic< bool > m_reopen_file
uint32_t GetCategoryMask() const
std::unordered_map< LogFlags, Level > CategoryLevels() const EXCLUSIVE_LOCKS_REQUIRED(!m_cs)
std::list< std::function< void(const std::string &)> > m_print_callbacks GUARDED_BY(m_cs)
Slots that connect to the print signal.
bool Enabled() const EXCLUSIVE_LOCKS_REQUIRED(!m_cs)
Returns whether logs will be written to any output.
std::string LogCategoriesString() const
Returns a string with the log categories in alphabetical order.
void DisableCategory(LogFlags flag)
void LogPrintStr_(std::string_view str, std::string_view logging_function, std::string_view source_file, int source_line, BCLog::LogFlags category, BCLog::Level level) EXCLUSIVE_LOCKS_REQUIRED(m_cs)
Send a string to the log output (internal)
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
static const bool DEFAULT_LOGTIMESTAMPS
bool GetLogCategory(BCLog::LogFlags &flag, std::string_view str)
Return true if str parses as a log category and set the flag.
static const bool DEFAULT_LOGIPS
static const bool DEFAULT_LOGTHREADNAMES
static bool LogAcceptCategory(BCLog::LogFlags category, BCLog::Level level)
Return true if log accepts specified category, at the specified level.
BCLog::Logger & LogInstance()
static const bool DEFAULT_LOGSOURCELOCATIONS
static const bool DEFAULT_LOGTIMEMICROS
const char *const DEFAULT_DEBUGLOGFILE
static void LogPrintf_(std::string_view logging_function, std::string_view source_file, const int source_line, const BCLog::LogFlags flag, const BCLog::Level level, const char *fmt, const Args &... args)
static constexpr bool DEFAULT_LOGLEVELALWAYS
constexpr size_t DEFAULT_MAX_LOG_BUFFER
constexpr auto DEFAULT_LOG_LEVEL
auto Join(const C &container, const S &separator, UnaryOp unary_op)
Join all container items.
std::chrono::seconds mocktime
SystemClock::time_point now
std::string logging_function
#define EXCLUSIVE_LOCKS_REQUIRED(...)