1515#include < optional>
1616
1717using util::Join;
18- using util::RemovePrefix;
19- using util::ToString;
18+ using util::RemovePrefixView;
2019
2120const char * const DEFAULT_DEBUGLOGFILE = " debug.log" ;
2221constexpr auto MAX_USER_SETABLE_SEVERITY_LEVEL{BCLog::Level::Info};
@@ -44,7 +43,7 @@ BCLog::Logger& LogInstance()
4443
4544bool 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
209208static 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 ;
0 commit comments