15
15
#include < optional>
16
16
17
17
using util::Join;
18
- using util::RemovePrefix;
19
- using util::ToString;
18
+ using util::RemovePrefixView;
20
19
21
20
const char * const DEFAULT_DEBUGLOGFILE = " debug.log" ;
22
21
constexpr auto MAX_USER_SETABLE_SEVERITY_LEVEL{BCLog::Level::Info};
@@ -44,7 +43,7 @@ BCLog::Logger& LogInstance()
44
43
45
44
bool fLogIPs = DEFAULT_LOGIPS;
46
45
47
- static int FileWriteStr (const std::string & str, FILE *fp)
46
+ static int FileWriteStr (std::string_view str, FILE *fp)
48
47
{
49
48
return fwrite (str.data (), 1 , str.size (), fp);
50
49
}
@@ -124,7 +123,7 @@ void BCLog::Logger::EnableCategory(BCLog::LogFlags flag)
124
123
m_categories |= flag;
125
124
}
126
125
127
- bool BCLog::Logger::EnableCategory (const std::string& str)
126
+ bool BCLog::Logger::EnableCategory (std::string_view str)
128
127
{
129
128
BCLog::LogFlags flag;
130
129
if (!GetLogCategory (flag, str)) return false ;
@@ -137,7 +136,7 @@ void BCLog::Logger::DisableCategory(BCLog::LogFlags flag)
137
136
m_categories &= ~flag;
138
137
}
139
138
140
- bool BCLog::Logger::DisableCategory (const std::string& str)
139
+ bool BCLog::Logger::DisableCategory (std::string_view str)
141
140
{
142
141
BCLog::LogFlags flag;
143
142
if (!GetLogCategory (flag, str)) return false ;
@@ -168,7 +167,7 @@ bool BCLog::Logger::DefaultShrinkDebugFile() const
168
167
return m_categories == BCLog::NONE;
169
168
}
170
169
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{
172
171
{" 0" , BCLog::NONE},
173
172
{" " , BCLog::NONE},
174
173
{" net" , BCLog::NET},
@@ -208,7 +207,7 @@ static const std::map<std::string, BCLog::LogFlags> LOG_CATEGORIES_BY_STR{
208
207
209
208
static const std::unordered_map<BCLog::LogFlags, std::string> LOG_CATEGORIES_BY_FLAG{
210
209
// Swap keys and values from LOG_CATEGORIES_BY_STR.
211
- [](const std::map<std::string, BCLog::LogFlags> & in) {
210
+ [](const auto & in) {
212
211
std::unordered_map<BCLog::LogFlags, std::string> out;
213
212
for (const auto & [k, v] : in) {
214
213
switch (v) {
@@ -221,7 +220,7 @@ static const std::unordered_map<BCLog::LogFlags, std::string> LOG_CATEGORIES_BY_
221
220
}(LOG_CATEGORIES_BY_STR)
222
221
};
223
222
224
- bool GetLogCategory (BCLog::LogFlags& flag, const std::string& str)
223
+ bool GetLogCategory (BCLog::LogFlags& flag, std::string_view str)
225
224
{
226
225
if (str.empty ()) {
227
226
flag = BCLog::ALL;
@@ -259,7 +258,7 @@ std::string LogCategoryToStr(BCLog::LogFlags category)
259
258
return it->second ;
260
259
}
261
260
262
- static std::optional<BCLog::Level> GetLogLevel (const std::string& level_str)
261
+ static std::optional<BCLog::Level> GetLogLevel (std::string_view level_str)
263
262
{
264
263
if (level_str == " trace" ) {
265
264
return BCLog::Level::Trace;
@@ -328,7 +327,7 @@ namespace BCLog {
328
327
* It escapes instead of removes them to still allow for troubleshooting
329
328
* issues where they accidentally end up in strings.
330
329
*/
331
- std::string LogEscapeMessage (const std::string& str) {
330
+ std::string LogEscapeMessage (std::string_view str) {
332
331
std::string ret;
333
332
for (char ch_in : str) {
334
333
uint8_t ch = (uint8_t )ch_in;
@@ -373,28 +372,28 @@ static size_t MemUsage(const BCLog::Logger::BufferedLog& buflog)
373
372
return buflog.str .size () + buflog.logging_function .size () + buflog.source_file .size () + buflog.threadname .size () + memusage::MallocUsage (sizeof (memusage::list_node<BCLog::Logger::BufferedLog>));
374
373
}
375
374
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
377
376
{
378
377
str.insert (0 , GetLogPrefix (category, level));
379
378
380
379
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) );
382
381
}
383
382
384
383
if (m_log_threadnames) {
385
- str.insert (0 , " [ " + (threadname.empty () ? " unknown" : threadname) + " ] " );
384
+ str.insert (0 , strprintf ( " [%s] " , (threadname.empty () ? " unknown" : threadname)) );
386
385
}
387
386
388
387
str.insert (0 , LogTimestampStr (now, mocktime));
389
388
}
390
389
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)
392
391
{
393
392
StdLockGuard scoped_lock (m_cs);
394
393
return LogPrintStr_ (str, logging_function, source_file, source_line, category, level);
395
394
}
396
395
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)
398
397
{
399
398
std::string str_prefixed = LogEscapeMessage (str);
400
399
@@ -418,8 +417,8 @@ void BCLog::Logger::LogPrintStr_(const std::string& str, const std::string& logg
418
417
.now =SystemClock::now (),
419
418
.mocktime =GetMockTime (),
420
419
.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) ,
423
422
.threadname =util::ThreadGetInternalName (),
424
423
.source_line =source_line,
425
424
.category =category,
@@ -512,15 +511,15 @@ void BCLog::Logger::ShrinkDebugFile()
512
511
fclose (file);
513
512
}
514
513
515
- bool BCLog::Logger::SetLogLevel (const std::string& level_str)
514
+ bool BCLog::Logger::SetLogLevel (std::string_view level_str)
516
515
{
517
516
const auto level = GetLogLevel (level_str);
518
517
if (!level.has_value () || level.value () > MAX_USER_SETABLE_SEVERITY_LEVEL) return false ;
519
518
m_log_level = level.value ();
520
519
return true ;
521
520
}
522
521
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)
524
523
{
525
524
BCLog::LogFlags flag;
526
525
if (!GetLogCategory (flag, category_str)) return false ;
0 commit comments