| 293 | } |
| 294 | |
| 295 | void BCLog::Logger::LogPrintStr(const BCLog::LogFlags& category, const char* file, int line, const char* func, const std::string& str) { |
| 296 | |
| 297 | std::lock_guard<std::mutex> scoped_lock(m_cs); |
| 298 | std::string str_prefixed = LogEscapeMessage(str); |
| 299 | |
| 300 | string s_file = string(file); |
| 301 | string s_func = string(func); |
| 302 | |
| 303 | if (m_print_file_line) { |
| 304 | string file_line = strprintf("%s:%d", s_file, line); |
| 305 | |
| 306 | #ifndef VER_DEBUG |
| 307 | if (file_line.size() > 38) |
| 308 | file_line = strprintf("%s**%s", file_line.substr(0, 10), file_line.substr(file_line.size()-10, 10)); |
| 309 | |
| 310 | if (s_func.size() > 20) |
| 311 | s_func = s_func.substr(0, 20); |
| 312 | #endif |
| 313 | |
| 314 | string file_line_func = strprintf("[%s %s]", file_line, s_func); |
| 315 | string log_cat = strprintf("[%s]", GetLogCategoryName(category)); |
| 316 | string file_line_cat_func = strprintf("%-15s %-44s", log_cat, file_line_func); |
| 317 | |
| 318 | str_prefixed.insert(0, tfm::format("%-80s ", file_line_cat_func)); |
| 319 | } |
| 320 | |
| 321 | if (m_log_threadnames && m_started_new_line) { |
| 322 | str_prefixed.insert(0, "[" + util::ThreadGetInternalName() + "] "); |
| 323 | } |
| 324 | |
| 325 | str_prefixed = LogTimestampStr(str_prefixed); |
| 326 | |
| 327 | m_started_new_line = !str.empty() && str[str.size()-1] == '\n'; |
| 328 | |
| 329 | if (m_buffering) { |
| 330 | // buffer if we haven't started logging yet |
| 331 | m_msgs_before_open.push_back(str_prefixed); |
| 332 | return; |
| 333 | } |
| 334 | |
| 335 | if (m_print_to_console) { |
| 336 | // print to console |
| 337 | fwrite(str_prefixed.data(), 1, str_prefixed.size(), stdout); |
| 338 | fflush(stdout); |
| 339 | } |
| 340 | for (const auto& cb : m_print_callbacks) { |
| 341 | cb(str_prefixed); |
| 342 | } |
| 343 | if (m_print_to_file) { |
| 344 | |
| 345 | assert(m_fileout != nullptr); |
| 346 | // reopen the log file, if requested |
| 347 | if (m_reopen_file) { |
| 348 | m_reopen_file = false; |
| 349 | FILE* new_fileout = fsbridge::fopen(m_file_path, "a"); |
| 350 | if (new_fileout) { |
| 351 | setbuf(new_fileout, nullptr); // unbuffered |
| 352 | fclose(m_fileout); |
no test coverage detected