mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-03-05 14:06:27 -05:00
logging: refactor: pull prefix code out
This commit is contained in:
parent
8f7b9eb871
commit
c5c76dc615
3 changed files with 21 additions and 9 deletions
|
@ -202,7 +202,7 @@ bool GetLogCategory(BCLog::LogFlags& flag, const std::string& str)
|
|||
return false;
|
||||
}
|
||||
|
||||
std::string BCLog::Logger::LogLevelToStr(BCLog::Level level) const
|
||||
std::string BCLog::Logger::LogLevelToStr(BCLog::Level level)
|
||||
{
|
||||
switch (level) {
|
||||
case BCLog::Level::Trace:
|
||||
|
@ -341,7 +341,7 @@ static constexpr std::array<BCLog::Level, 3> LogLevelsList()
|
|||
std::string BCLog::Logger::LogLevelsString() const
|
||||
{
|
||||
const auto& levels = LogLevelsList();
|
||||
return Join(std::vector<BCLog::Level>{levels.begin(), levels.end()}, ", ", [this](BCLog::Level level) { return LogLevelToStr(level); });
|
||||
return Join(std::vector<BCLog::Level>{levels.begin(), levels.end()}, ", ", [](BCLog::Level level) { return LogLevelToStr(level); });
|
||||
}
|
||||
|
||||
std::string BCLog::Logger::LogTimestampStr(const std::string& str)
|
||||
|
@ -392,12 +392,9 @@ namespace BCLog {
|
|||
}
|
||||
} // namespace BCLog
|
||||
|
||||
void BCLog::Logger::LogPrintStr(const std::string& str, const std::string& logging_function, const std::string& source_file, int source_line, BCLog::LogFlags category, BCLog::Level level)
|
||||
std::string BCLog::Logger::GetLogPrefix(BCLog::LogFlags category, BCLog::Level level) const
|
||||
{
|
||||
StdLockGuard scoped_lock(m_cs);
|
||||
std::string str_prefixed = LogEscapeMessage(str);
|
||||
|
||||
if ((category != LogFlags::NONE || level != Level::None) && m_started_new_line) {
|
||||
if (category != LogFlags::NONE || level != Level::None) {
|
||||
std::string s{"["};
|
||||
|
||||
if (category != LogFlags::NONE) {
|
||||
|
@ -414,7 +411,18 @@ void BCLog::Logger::LogPrintStr(const std::string& str, const std::string& loggi
|
|||
}
|
||||
|
||||
s += "] ";
|
||||
str_prefixed.insert(0, s);
|
||||
return s;
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
void BCLog::Logger::LogPrintStr(const std::string& str, const std::string& logging_function, const std::string& source_file, int source_line, BCLog::LogFlags category, BCLog::Level level)
|
||||
{
|
||||
StdLockGuard scoped_lock(m_cs);
|
||||
std::string str_prefixed = LogEscapeMessage(str);
|
||||
|
||||
if (m_started_new_line) {
|
||||
str_prefixed.insert(0, GetLogPrefix(category, level));
|
||||
}
|
||||
|
||||
if (m_log_sourcelocations && m_started_new_line) {
|
||||
|
|
|
@ -124,6 +124,8 @@ namespace BCLog {
|
|||
fs::path m_file_path;
|
||||
std::atomic<bool> m_reopen_file{false};
|
||||
|
||||
std::string GetLogPrefix(LogFlags category, Level level) const;
|
||||
|
||||
/** Send a string to the log output */
|
||||
void LogPrintStr(const std::string& str, const std::string& logging_function, const std::string& source_file, int source_line, BCLog::LogFlags category, BCLog::Level level);
|
||||
|
||||
|
@ -194,7 +196,7 @@ namespace BCLog {
|
|||
std::string LogLevelsString() const;
|
||||
|
||||
//! Returns the string representation of a log level.
|
||||
std::string LogLevelToStr(BCLog::Level level) const;
|
||||
static std::string LogLevelToStr(BCLog::Level level);
|
||||
|
||||
bool DefaultShrinkDebugFile() const;
|
||||
};
|
||||
|
|
|
@ -87,6 +87,7 @@ BOOST_FIXTURE_TEST_CASE(logging_LogPrintf_, LogSetup)
|
|||
LogPrintf_("fn2", "src2", 2, BCLog::LogFlags::NET, BCLog::Level::None, "foo2: %s\n", "bar2");
|
||||
LogPrintf_("fn3", "src3", 3, BCLog::LogFlags::NONE, BCLog::Level::Debug, "foo3: %s\n", "bar3");
|
||||
LogPrintf_("fn4", "src4", 4, BCLog::LogFlags::NONE, BCLog::Level::None, "foo4: %s\n", "bar4");
|
||||
LogPrintf_("fn5", "src5", 5, BCLog::LogFlags::NONE, BCLog::Level::Info, "foo5: %s\n", "bar5");
|
||||
std::ifstream file{tmp_log_path};
|
||||
std::vector<std::string> log_lines;
|
||||
for (std::string log; std::getline(file, log);) {
|
||||
|
@ -97,6 +98,7 @@ BOOST_FIXTURE_TEST_CASE(logging_LogPrintf_, LogSetup)
|
|||
"[src2:2] [fn2] [net] foo2: bar2",
|
||||
"[src3:3] [fn3] [debug] foo3: bar3",
|
||||
"[src4:4] [fn4] foo4: bar4",
|
||||
"[src5:5] [fn5] [info] foo5: bar5",
|
||||
};
|
||||
BOOST_CHECK_EQUAL_COLLECTIONS(log_lines.begin(), log_lines.end(), expected.begin(), expected.end());
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue