Skip to content

Commit b4dd7ab

Browse files
committed
logging: use std::string_view
1 parent 558df5c commit b4dd7ab

File tree

3 files changed

+28
-29
lines changed

3 files changed

+28
-29
lines changed

src/logging.cpp

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
#include <optional>
1616

1717
using util::Join;
18-
using util::RemovePrefix;
19-
using util::ToString;
18+
using util::RemovePrefixView;
2019

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

4544
bool fLogIPs = DEFAULT_LOGIPS;
4645

47-
static int FileWriteStr(const std::string &str, FILE *fp)
46+
static int FileWriteStr(std::string_view str, FILE *fp)
4847
{
4948
return fwrite(str.data(), 1, str.size(), fp);
5049
}
@@ -124,7 +123,7 @@ void BCLog::Logger::EnableCategory(BCLog::LogFlags flag)
124123
m_categories |= flag;
125124
}
126125

127-
bool BCLog::Logger::EnableCategory(const std::string& str)
126+
bool BCLog::Logger::EnableCategory(std::string_view str)
128127
{
129128
BCLog::LogFlags flag;
130129
if (!GetLogCategory(flag, str)) return false;
@@ -137,7 +136,7 @@ void BCLog::Logger::DisableCategory(BCLog::LogFlags flag)
137136
m_categories &= ~flag;
138137
}
139138

140-
bool BCLog::Logger::DisableCategory(const std::string& str)
139+
bool BCLog::Logger::DisableCategory(std::string_view str)
141140
{
142141
BCLog::LogFlags flag;
143142
if (!GetLogCategory(flag, str)) return false;
@@ -168,7 +167,7 @@ bool BCLog::Logger::DefaultShrinkDebugFile() const
168167
return m_categories == BCLog::NONE;
169168
}
170169

171-
static const std::map<std::string, BCLog::LogFlags> LOG_CATEGORIES_BY_STR{
170+
static const std::map<std::string, BCLog::LogFlags, std::less<>> LOG_CATEGORIES_BY_STR{
172171
{"0", BCLog::NONE},
173172
{"", BCLog::NONE},
174173
{"net", BCLog::NET},
@@ -208,7 +207,7 @@ static const std::map<std::string, BCLog::LogFlags> LOG_CATEGORIES_BY_STR{
208207

209208
static const std::unordered_map<BCLog::LogFlags, std::string> LOG_CATEGORIES_BY_FLAG{
210209
// Swap keys and values from LOG_CATEGORIES_BY_STR.
211-
[](const std::map<std::string, BCLog::LogFlags>& in) {
210+
[](const auto& in) {
212211
std::unordered_map<BCLog::LogFlags, std::string> out;
213212
for (const auto& [k, v] : in) {
214213
switch (v) {
@@ -221,7 +220,7 @@ static const std::unordered_map<BCLog::LogFlags, std::string> LOG_CATEGORIES_BY_
221220
}(LOG_CATEGORIES_BY_STR)
222221
};
223222

224-
bool GetLogCategory(BCLog::LogFlags& flag, const std::string& str)
223+
bool GetLogCategory(BCLog::LogFlags& flag, std::string_view str)
225224
{
226225
if (str.empty()) {
227226
flag = BCLog::ALL;
@@ -259,7 +258,7 @@ std::string LogCategoryToStr(BCLog::LogFlags category)
259258
return it->second;
260259
}
261260

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

376-
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
375+
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
377376
{
378377
str.insert(0, GetLogPrefix(category, level));
379378

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

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

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

391-
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)
390+
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)
392391
{
393392
StdLockGuard scoped_lock(m_cs);
394393
return LogPrintStr_(str, logging_function, source_file, source_line, category, level);
395394
}
396395

397-
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)
396+
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)
398397
{
399398
std::string str_prefixed = LogEscapeMessage(str);
400399

@@ -418,8 +417,8 @@ void BCLog::Logger::LogPrintStr_(const std::string& str, const std::string& logg
418417
.now=SystemClock::now(),
419418
.mocktime=GetMockTime(),
420419
.str=str_prefixed,
421-
.logging_function=logging_function,
422-
.source_file=source_file,
420+
.logging_function=std::string(logging_function),
421+
.source_file=std::string(source_file),
423422
.threadname=util::ThreadGetInternalName(),
424423
.source_line=source_line,
425424
.category=category,
@@ -512,15 +511,15 @@ void BCLog::Logger::ShrinkDebugFile()
512511
fclose(file);
513512
}
514513

515-
bool BCLog::Logger::SetLogLevel(const std::string& level_str)
514+
bool BCLog::Logger::SetLogLevel(std::string_view level_str)
516515
{
517516
const auto level = GetLogLevel(level_str);
518517
if (!level.has_value() || level.value() > MAX_USER_SETABLE_SEVERITY_LEVEL) return false;
519518
m_log_level = level.value();
520519
return true;
521520
}
522521

523-
bool BCLog::Logger::SetCategoryLogLevel(const std::string& category_str, const std::string& level_str)
522+
bool BCLog::Logger::SetCategoryLogLevel(std::string_view category_str, std::string_view level_str)
524523
{
525524
BCLog::LogFlags flag;
526525
if (!GetLogCategory(flag, category_str)) return false;

src/logging.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -121,15 +121,15 @@ namespace BCLog {
121121
/** Log categories bitfield. */
122122
std::atomic<uint32_t> m_categories{0};
123123

124-
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;
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;
125125

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

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

131131
/** Send a string to the log output (internal) */
132-
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)
132+
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)
133133
EXCLUSIVE_LOCKS_REQUIRED(m_cs);
134134

135135
public:
@@ -148,7 +148,7 @@ namespace BCLog {
148148
std::string GetLogPrefix(LogFlags category, Level level) const;
149149

150150
/** Send a string to the log output */
151-
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)
151+
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)
152152
EXCLUSIVE_LOCKS_REQUIRED(!m_cs);
153153

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

203203
Level LogLevel() const { return m_log_level.load(); }
204204
void SetLogLevel(Level level) { m_log_level = level; }
205-
bool SetLogLevel(const std::string& level);
205+
bool SetLogLevel(std::string_view level);
206206

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

209209
void EnableCategory(LogFlags flag);
210-
bool EnableCategory(const std::string& str);
210+
bool EnableCategory(std::string_view str);
211211
void DisableCategory(LogFlags flag);
212-
bool DisableCategory(const std::string& str);
212+
bool DisableCategory(std::string_view str);
213213

214214
bool WillLogCategory(LogFlags category) const;
215215
bool WillLogCategoryLevel(LogFlags category, Level level) const EXCLUSIVE_LOCKS_REQUIRED(!m_cs);
@@ -242,14 +242,14 @@ static inline bool LogAcceptCategory(BCLog::LogFlags category, BCLog::Level leve
242242
}
243243

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

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

251251
template <typename... Args>
252-
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)
252+
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)
253253
{
254254
if (LogInstance().Enabled()) {
255255
std::string log_msg;

src/test/util_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ static const std::string STRING_WITH_EMBEDDED_NULL_CHAR{"1"s "\0" "1"s};
5858

5959
/* defined in logging.cpp */
6060
namespace BCLog {
61-
std::string LogEscapeMessage(const std::string& str);
61+
std::string LogEscapeMessage(std::string_view str);
6262
}
6363

6464
BOOST_FIXTURE_TEST_SUITE(util_tests, BasicTestingSetup)

0 commit comments

Comments
 (0)