From a7859f73edcb519988ce408873f4312195000300 Mon Sep 17 00:00:00 2001 From: louwei Date: Sat, 11 Dec 2021 16:44:08 +0800 Subject: [PATCH 1/3] fix: stored_backtrace_log_record caused crash while print backtrace log records --- quill/include/quill/detail/events/LogEvent.h | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/quill/include/quill/detail/events/LogEvent.h b/quill/include/quill/detail/events/LogEvent.h index d0e8d49..c263602 100644 --- a/quill/include/quill/detail/events/LogEvent.h +++ b/quill/include/quill/detail/events/LogEvent.h @@ -141,8 +141,11 @@ private: [&obtain_active_handlers, ×tamp_callback](std::string const& stored_thread_id, std::string const& stored_thread_name, BaseEvent const* stored_backtrace_log_record) { - stored_backtrace_log_record->backend_process_backtrace_log_record( - stored_thread_id.data(), stored_thread_name.data(), obtain_active_handlers, timestamp_callback); + if (stored_backtrace_log_record) + { + stored_backtrace_log_record->backend_process_backtrace_log_record( + stored_thread_id.data(), stored_thread_name.data(), obtain_active_handlers, timestamp_callback); + } }); } } @@ -204,4 +207,4 @@ private: PromotedTupleT _fmt_args; }; } // namespace detail -} // namespace quill \ No newline at end of file +} // namespace quill -- Gitee From 2ebdfc3e4519190fbadb6b80221d70be496301dc Mon Sep 17 00:00:00 2001 From: louwei Date: Tue, 4 Jan 2022 10:37:18 +0800 Subject: [PATCH 2/3] fix: prevent log backtrace thread from segment fault crash --- .../src/detail/backend/BacktraceLogRecordStorage.cpp | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/quill/src/detail/backend/BacktraceLogRecordStorage.cpp b/quill/src/detail/backend/BacktraceLogRecordStorage.cpp index 2c769c3..411ba8d 100644 --- a/quill/src/detail/backend/BacktraceLogRecordStorage.cpp +++ b/quill/src/detail/backend/BacktraceLogRecordStorage.cpp @@ -77,12 +77,18 @@ void BacktraceLogRecordStorage::process( for (uint32_t i = 0; i < stored_record_collection.size(); ++i) { // Give to the user callback the thread id and the RecordBase pointer - callback(stored_record_collection[index].thread_id, stored_record_collection[index].thread_name, - stored_record_collection[index].base_record.get()); + if (stored_record_collection[index].base_record) { + callback(stored_record_collection[index].thread_id, stored_record_collection[index].thread_name, + stored_record_collection[index].base_record.get()); + } + + // Release record + //stored_record_collection[index].base_record.reset(nullptr); // We wrap around to iterate all messages if (index < stored_record_collection.size() - 1) { + index += 1; } else @@ -129,4 +135,4 @@ void BacktraceLogRecordStorage::clear(std::string const& logger_name) } } // namespace detail -} // namespace quill \ No newline at end of file +} // namespace quill -- Gitee From 0d2aa81bfc61894037b344755e95629fb2c28329 Mon Sep 17 00:00:00 2001 From: PeiTingbin Date: Wed, 11 May 2022 09:24:34 +0800 Subject: [PATCH 3/3] fix: It may cause a crash while renaming log files, fix it up. --- quill/include/quill/detail/misc/Os.h | 2 +- .../quill/handlers/RotatingFileHandler.h | 2 +- quill/src/detail/misc/Os.cpp | 8 ++-- quill/src/handlers/RotatingFileHandler.cpp | 44 +++++++++++++------ 4 files changed, 38 insertions(+), 18 deletions(-) diff --git a/quill/include/quill/detail/misc/Os.h b/quill/include/quill/detail/misc/Os.h index d44ce98..0467b98 100644 --- a/quill/include/quill/detail/misc/Os.h +++ b/quill/include/quill/detail/misc/Os.h @@ -128,7 +128,7 @@ QUILL_ATTRIBUTE_COLD int remove(filename_t const& filename) noexcept; * @param previous_file previous file name * @param new_file new file name */ -QUILL_ATTRIBUTE_COLD void rename(filename_t const& previous_file, filename_t const& new_file); +QUILL_ATTRIBUTE_COLD bool rename(filename_t const& previous_file, filename_t const& new_file); /** * inverses of gmtime diff --git a/quill/include/quill/handlers/RotatingFileHandler.h b/quill/include/quill/handlers/RotatingFileHandler.h index 1dbe7b9..6919dc1 100644 --- a/quill/include/quill/handlers/RotatingFileHandler.h +++ b/quill/include/quill/handlers/RotatingFileHandler.h @@ -50,7 +50,7 @@ private: /** * Rotate to a new file */ - QUILL_ATTRIBUTE_COLD void _rotate(); + QUILL_ATTRIBUTE_COLD bool _rotate(); private: size_t _current_size{0}; diff --git a/quill/src/detail/misc/Os.cpp b/quill/src/detail/misc/Os.cpp index b9dd33f..b25d8f2 100644 --- a/quill/src/detail/misc/Os.cpp +++ b/quill/src/detail/misc/Os.cpp @@ -363,21 +363,23 @@ int remove(filename_t const& filename) noexcept } /***/ -void rename(filename_t const& previous_file, filename_t const& new_file) +bool rename(filename_t const& previous_file, filename_t const& new_file) { #if defined(_WIN32) int const res = ::_wrename(previous_file.c_str(), new_file.c_str()); #else int const res = std::rename(previous_file.c_str(), new_file.c_str()); #endif - if (QUILL_UNLIKELY(res != 0)) { std::ostringstream error_msg; error_msg << "failed to rename previous log file during rotation, with error message errno: \"" << errno << "\""; - QUILL_THROW(QuillError{error_msg.str()}); + //QUILL_THROW(QuillError{error_msg.str()}); + printf("%s\n", error_msg.str().c_str()); + return false; } + return true; } /***/ diff --git a/quill/src/handlers/RotatingFileHandler.cpp b/quill/src/handlers/RotatingFileHandler.cpp index 5226829..a80e2fc 100644 --- a/quill/src/handlers/RotatingFileHandler.cpp +++ b/quill/src/handlers/RotatingFileHandler.cpp @@ -27,8 +27,9 @@ void RotatingFileHandler::write(fmt::memory_buffer const& formatted_log_record, if (_current_size > _max_bytes) { - _rotate(); - _current_size = formatted_log_record.size(); + if(_rotate()) { + _current_size = formatted_log_record.size(); + } } // write to file @@ -36,12 +37,12 @@ void RotatingFileHandler::write(fmt::memory_buffer const& formatted_log_record, } /***/ -void RotatingFileHandler::_rotate() +bool RotatingFileHandler::_rotate() { if (_current_index >= _backup_count) { // we can not rotate anymore, do nothing - return; + return true; } if (_file) @@ -55,27 +56,44 @@ void RotatingFileHandler::_rotate() << errno << "\""; QUILL_THROW(QuillError{error_msg.str()}); } + else { + _file = nullptr; + } } // if we have more than 2 files we need to start renaming recursively - for (uint32_t i = _current_index; i >= 1; --i) + for (uint32_t i = _current_index; ; ) { - filename_t const previous_file = detail::file_utilities::append_index_to_filename(_filename, i); + filename_t previous_file; + if(i > 0) { + previous_file = detail::file_utilities::append_index_to_filename(_filename, i); + } + else { + previous_file = _filename; + } filename_t const new_file = detail::file_utilities::append_index_to_filename(_filename, i + 1); + if(!quill::detail::rename(previous_file, new_file)) { + _current_index = i; + if(nullptr == _file) { + _file = detail::file_utilities::open(_filename, "a"); + } + return false; + } - quill::detail::rename(previous_file, new_file); + if(0 == i) { + break; + } + else { + i--; + } } - // then we will always rename the base filename to 1 - filename_t const previous_file = _filename; - filename_t const new_file = detail::file_utilities::append_index_to_filename(_filename, 1); - - quill::detail::rename(previous_file, new_file); - // Increment the rotation index ++_current_index; // Now reopen the base filename for writing again _file = detail::file_utilities::open(_filename, "w"); + return true; } + } // namespace quill \ No newline at end of file -- Gitee