Skip to content

Commit

Permalink
logging: use std::string_view
Browse files Browse the repository at this point in the history
  • Loading branch information
ajtowns committed Jul 19, 2024
1 parent 558df5c commit b4dd7ab
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 29 deletions.
37 changes: 18 additions & 19 deletions src/logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
#include <optional>

using util::Join;
using util::RemovePrefix;
using util::ToString;
using util::RemovePrefixView;

const char * const DEFAULT_DEBUGLOGFILE = "debug.log";
constexpr auto MAX_USER_SETABLE_SEVERITY_LEVEL{BCLog::Level::Info};
Expand Down Expand Up @@ -44,7 +43,7 @@ BCLog::Logger& LogInstance()

bool fLogIPs = DEFAULT_LOGIPS;

static int FileWriteStr(const std::string &str, FILE *fp)
static int FileWriteStr(std::string_view str, FILE *fp)
{
return fwrite(str.data(), 1, str.size(), fp);
}
Expand Down Expand Up @@ -124,7 +123,7 @@ void BCLog::Logger::EnableCategory(BCLog::LogFlags flag)
m_categories |= flag;
}

bool BCLog::Logger::EnableCategory(const std::string& str)
bool BCLog::Logger::EnableCategory(std::string_view str)
{
BCLog::LogFlags flag;
if (!GetLogCategory(flag, str)) return false;
Expand All @@ -137,7 +136,7 @@ void BCLog::Logger::DisableCategory(BCLog::LogFlags flag)
m_categories &= ~flag;
}

bool BCLog::Logger::DisableCategory(const std::string& str)
bool BCLog::Logger::DisableCategory(std::string_view str)
{
BCLog::LogFlags flag;
if (!GetLogCategory(flag, str)) return false;
Expand Down Expand Up @@ -168,7 +167,7 @@ bool BCLog::Logger::DefaultShrinkDebugFile() const
return m_categories == BCLog::NONE;
}

static const std::map<std::string, BCLog::LogFlags> LOG_CATEGORIES_BY_STR{
static const std::map<std::string, BCLog::LogFlags, std::less<>> LOG_CATEGORIES_BY_STR{
{"0", BCLog::NONE},
{"", BCLog::NONE},
{"net", BCLog::NET},
Expand Down Expand Up @@ -208,7 +207,7 @@ static const std::map<std::string, BCLog::LogFlags> LOG_CATEGORIES_BY_STR{

static const std::unordered_map<BCLog::LogFlags, std::string> LOG_CATEGORIES_BY_FLAG{
// Swap keys and values from LOG_CATEGORIES_BY_STR.
[](const std::map<std::string, BCLog::LogFlags>& in) {
[](const auto& in) {
std::unordered_map<BCLog::LogFlags, std::string> out;
for (const auto& [k, v] : in) {
switch (v) {
Expand All @@ -221,7 +220,7 @@ static const std::unordered_map<BCLog::LogFlags, std::string> LOG_CATEGORIES_BY_
}(LOG_CATEGORIES_BY_STR)
};

bool GetLogCategory(BCLog::LogFlags& flag, const std::string& str)
bool GetLogCategory(BCLog::LogFlags& flag, std::string_view str)
{
if (str.empty()) {
flag = BCLog::ALL;
Expand Down Expand Up @@ -259,7 +258,7 @@ std::string LogCategoryToStr(BCLog::LogFlags category)
return it->second;
}

static std::optional<BCLog::Level> GetLogLevel(const std::string& level_str)
static std::optional<BCLog::Level> GetLogLevel(std::string_view level_str)
{
if (level_str == "trace") {
return BCLog::Level::Trace;
Expand Down Expand Up @@ -328,7 +327,7 @@ namespace BCLog {
* It escapes instead of removes them to still allow for troubleshooting
* issues where they accidentally end up in strings.
*/
std::string LogEscapeMessage(const std::string& str) {
std::string LogEscapeMessage(std::string_view str) {
std::string ret;
for (char ch_in : str) {
uint8_t ch = (uint8_t)ch_in;
Expand Down Expand Up @@ -373,28 +372,28 @@ static size_t MemUsage(const BCLog::Logger::BufferedLog& buflog)
return buflog.str.size() + buflog.logging_function.size() + buflog.source_file.size() + buflog.threadname.size() + memusage::MallocUsage(sizeof(memusage::list_node<BCLog::Logger::BufferedLog>));
}

void BCLog::Logger::FormatLogStrInPlace(std::string& str, BCLog::LogFlags category, BCLog::Level level, const std::string& source_file, int source_line, const std::string& logging_function, const std::string& threadname, SystemClock::time_point now, std::chrono::seconds mocktime) const
void BCLog::Logger::FormatLogStrInPlace(std::string& str, BCLog::LogFlags category, BCLog::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
{
str.insert(0, GetLogPrefix(category, level));

if (m_log_sourcelocations) {
str.insert(0, "[" + RemovePrefix(source_file, "./") + ":" + ToString(source_line) + "] [" + logging_function + "] ");
str.insert(0, strprintf("[%s:%d] [%s] ", RemovePrefixView(source_file, "./"), source_line, logging_function));
}

if (m_log_threadnames) {
str.insert(0, "[" + (threadname.empty() ? "unknown" : threadname) + "] ");
str.insert(0, strprintf("[%s] ", (threadname.empty() ? "unknown" : threadname)));
}

str.insert(0, LogTimestampStr(now, mocktime));
}

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)
void BCLog::Logger::LogPrintStr(std::string_view str, std::string_view logging_function, std::string_view source_file, int source_line, BCLog::LogFlags category, BCLog::Level level)
{
StdLockGuard scoped_lock(m_cs);
return LogPrintStr_(str, logging_function, source_file, source_line, category, level);
}

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)
void BCLog::Logger::LogPrintStr_(std::string_view str, std::string_view logging_function, std::string_view source_file, int source_line, BCLog::LogFlags category, BCLog::Level level)
{
std::string str_prefixed = LogEscapeMessage(str);

Expand All @@ -418,8 +417,8 @@ void BCLog::Logger::LogPrintStr_(const std::string& str, const std::string& logg
.now=SystemClock::now(),
.mocktime=GetMockTime(),
.str=str_prefixed,
.logging_function=logging_function,
.source_file=source_file,
.logging_function=std::string(logging_function),
.source_file=std::string(source_file),
.threadname=util::ThreadGetInternalName(),
.source_line=source_line,
.category=category,
Expand Down Expand Up @@ -512,15 +511,15 @@ void BCLog::Logger::ShrinkDebugFile()
fclose(file);
}

bool BCLog::Logger::SetLogLevel(const std::string& level_str)
bool BCLog::Logger::SetLogLevel(std::string_view level_str)
{
const auto level = GetLogLevel(level_str);
if (!level.has_value() || level.value() > MAX_USER_SETABLE_SEVERITY_LEVEL) return false;
m_log_level = level.value();
return true;
}

bool BCLog::Logger::SetCategoryLogLevel(const std::string& category_str, const std::string& level_str)
bool BCLog::Logger::SetCategoryLogLevel(std::string_view category_str, std::string_view level_str)
{
BCLog::LogFlags flag;
if (!GetLogCategory(flag, category_str)) return false;
Expand Down
18 changes: 9 additions & 9 deletions src/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,15 @@ namespace BCLog {
/** Log categories bitfield. */
std::atomic<uint32_t> m_categories{0};

void FormatLogStrInPlace(std::string& str, LogFlags category, Level level, const std::string& source_file, int source_line, const std::string& logging_function, const std::string& threadname, SystemClock::time_point now, std::chrono::seconds mocktime) const;
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;

std::string LogTimestampStr(SystemClock::time_point now, std::chrono::seconds mocktime) const;

/** Slots that connect to the print signal */
std::list<std::function<void(const std::string&)>> m_print_callbacks GUARDED_BY(m_cs) {};

/** Send a string to the log output (internal) */
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)
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);

public:
Expand All @@ -148,7 +148,7 @@ namespace BCLog {
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)
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);

/** Returns whether logs will be written to any output */
Expand Down Expand Up @@ -198,18 +198,18 @@ namespace BCLog {
StdLockGuard scoped_lock(m_cs);
m_category_log_levels = levels;
}
bool SetCategoryLogLevel(const std::string& category_str, const std::string& level_str) EXCLUSIVE_LOCKS_REQUIRED(!m_cs);
bool SetCategoryLogLevel(std::string_view category_str, std::string_view level_str) EXCLUSIVE_LOCKS_REQUIRED(!m_cs);

Level LogLevel() const { return m_log_level.load(); }
void SetLogLevel(Level level) { m_log_level = level; }
bool SetLogLevel(const std::string& level);
bool SetLogLevel(std::string_view level);

uint32_t GetCategoryMask() const { return m_categories.load(); }

void EnableCategory(LogFlags flag);
bool EnableCategory(const std::string& str);
bool EnableCategory(std::string_view str);
void DisableCategory(LogFlags flag);
bool DisableCategory(const std::string& str);
bool DisableCategory(std::string_view str);

bool WillLogCategory(LogFlags category) const;
bool WillLogCategoryLevel(LogFlags category, Level level) const EXCLUSIVE_LOCKS_REQUIRED(!m_cs);
Expand Down Expand Up @@ -242,14 +242,14 @@ static inline bool LogAcceptCategory(BCLog::LogFlags category, BCLog::Level leve
}

/** Return true if str parses as a log category and set the flag */
bool GetLogCategory(BCLog::LogFlags& flag, const std::string& str);
bool GetLogCategory(BCLog::LogFlags& flag, std::string_view str);

// Be conservative when using functions that
// unconditionally log to debug.log! It should not be the case that an inbound
// peer can fill up a user's disk with debug.log entries.

template <typename... Args>
static inline void LogPrintf_(const std::string& logging_function, const std::string& source_file, const int source_line, const BCLog::LogFlags flag, const BCLog::Level level, const char* fmt, const Args&... args)
static inline 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)
{
if (LogInstance().Enabled()) {
std::string log_msg;
Expand Down
2 changes: 1 addition & 1 deletion src/test/util_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ static const std::string STRING_WITH_EMBEDDED_NULL_CHAR{"1"s "\0" "1"s};

/* defined in logging.cpp */
namespace BCLog {
std::string LogEscapeMessage(const std::string& str);
std::string LogEscapeMessage(std::string_view str);
}

BOOST_FIXTURE_TEST_SUITE(util_tests, BasicTestingSetup)
Expand Down

0 comments on commit b4dd7ab

Please sign in to comment.