| 340 | } // namespace BCLog |
| 341 | |
| 342 | void BCLog::Logger::LogPrintStr(const std::string& str, const std::string& logging_function, |
| 343 | const SourceLocation& source_location, const BCLog::LogFlags category, |
| 344 | const BCLog::Level level) |
| 345 | { |
| 346 | StdLockGuard scoped_lock(m_cs); |
| 347 | std::string str_prefixed = LogEscapeMessage(str); |
| 348 | |
| 349 | const bool print_category{category != LogFlags::NONE && category != LogFlags::UNCONDITIONAL_ALWAYS && category != LogFlags::UNCONDITIONAL_RATE_LIMITED}; |
| 350 | if ((print_category || level != Level::None) && m_started_new_line) { |
| 351 | std::string s{"["}; |
| 352 | |
| 353 | if (print_category) { |
| 354 | s += LogCategoryToStr(category); |
| 355 | } |
| 356 | |
| 357 | if (print_category && level != Level::None) { |
| 358 | // Only add separator if both flag and level are not NONE |
| 359 | s += ":"; |
| 360 | } |
| 361 | |
| 362 | if (level != Level::None) { |
| 363 | s += LogLevelToStr(level); |
| 364 | } |
| 365 | |
| 366 | s += "] "; |
| 367 | str_prefixed.insert(0, s); |
| 368 | } |
| 369 | |
| 370 | if (m_log_sourcelocations && m_started_new_line) { |
| 371 | str_prefixed.insert(0, "[" + RemovePrefix(source_location.m_file, "./") + ":" + ToString(source_location.m_line) + "] [" + logging_function + "] "); |
| 372 | } |
| 373 | |
| 374 | if (m_log_threadnames && m_started_new_line) { |
| 375 | str_prefixed.insert(0, "[" + util::ThreadGetInternalName() + "] "); |
| 376 | } |
| 377 | |
| 378 | str_prefixed = LogTimestampStr(str_prefixed); |
| 379 | |
| 380 | // Whether or not logging to disk was/is ratelimited for this source location. |
| 381 | bool was_ratelimited{false}; |
| 382 | bool is_ratelimited{false}; |
| 383 | |
| 384 | if (category == UNCONDITIONAL_RATE_LIMITED && m_ratelimit) { |
| 385 | was_ratelimited = m_supressed_locations.find(source_location) != m_supressed_locations.end(); |
| 386 | is_ratelimited = !m_ratelimiters[source_location].Consume(str_prefixed.size()); |
| 387 | |
| 388 | if (!is_ratelimited && was_ratelimited) { |
| 389 | // Logging will restart for this source location. |
| 390 | m_supressed_locations.erase(source_location); |
| 391 | |
| 392 | str_prefixed = LogTimestampStr(strprintf( |
| 393 | "Restarting logging from %s:%d (%s): " |
| 394 | "(%d MiB) were dropped during the last hour.\n%s", |
| 395 | source_location.m_file, source_location.m_line, logging_function, |
| 396 | m_ratelimiters[source_location].GetDroppedBytes() / (1024 * 1024), str_prefixed)); |
| 397 | } else if (is_ratelimited && !was_ratelimited) { |
| 398 | // Logging from this source location will be supressed until the current window resets. |
| 399 | m_supressed_locations.insert(source_location); |
no test coverage detected