diff --git a/include/libconcur/element.h b/include/libconcur/element.h index 7c816ec..b50c7cf 100644 --- a/include/libconcur/element.h +++ b/include/libconcur/element.h @@ -74,10 +74,9 @@ public: LIBIMP_LOG_(); LIBIMP_TRY { data_ = std::forward(src); - } LIBIMP_CATCH(std::exception const &e) { - log.error("failed: `data = std::forward(src)`. error = ", e.what()); } LIBIMP_CATCH(...) { - log.error("failed: `data = std::forward(src)`. error = unknown"); + log.error("failed: `data = std::forward(src)`.", + "\n\texception: ", ::LIBIMP::log::exception_string(std::current_exception())); } } diff --git a/include/libimp/log.h b/include/libimp/log.h index 42909f1..b1650af 100644 --- a/include/libimp/log.h +++ b/include/libimp/log.h @@ -102,20 +102,26 @@ inline auto &make_std_out() noexcept { return std_out; } -/// \brief Record the last information when an exception occurs. -inline void log_exception(char const *func, std::exception_ptr eptr) noexcept { +/// \brief Get the exception information. +inline char const *exception_string(std::exception_ptr eptr) noexcept { LIBIMP_TRY { - if (func == nullptr) { - func = "-"; - } if (eptr) { std::rethrow_exception(eptr); } } LIBIMP_CATCH(std::exception const &e) { - std::fprintf(stderr, "[F][%s] exception: %s\n", func, e.what()); + return e.what(); } LIBIMP_CATCH(...) { - std::fprintf(stderr, "[F][%s] exception: unknown\n", func); + return "unknown"; } + return "none"; +} + +/// \brief Record the last information when an exception occurs. +inline void exception_print(char const *func, std::exception_ptr eptr) noexcept { + if (func == nullptr) { + func = "-"; + } + std::fprintf(stderr, "[F][%s] exception: %s\n", func, exception_string(eptr)); } /** @@ -155,7 +161,7 @@ public: std::forward_as_tuple(std::forward(args)...), }); } LIBIMP_CATCH(...) { - log_exception(func_, std::current_exception()); + exception_print(func_, std::current_exception()); } return *this; } diff --git a/src/libimp/platform/win/system.h b/src/libimp/platform/win/system.h index c1f01fc..f0a6399 100644 --- a/src/libimp/platform/win/system.h +++ b/src/libimp/platform/win/system.h @@ -49,8 +49,9 @@ std::string error_string(DWORD code) noexcept { std::string ret(len, '\0'); cvt_cstr(lpErrText, msg_len, &ret[0], ret.size()); return ret; - } LIBIMP_CATCH(std::exception const &e) { - log.failed(e.what()); + } LIBIMP_CATCH(...) { + log.error("failed: FormatMessage(dwMessageId = ", code, ").", + "\n\texception: ", ::LIBIMP::log::exception_string(std::current_exception())); } return {}; } diff --git a/src/libpmr/monotonic_buffer_resource.cpp b/src/libpmr/monotonic_buffer_resource.cpp index 23810b6..72be867 100644 --- a/src/libpmr/monotonic_buffer_resource.cpp +++ b/src/libpmr/monotonic_buffer_resource.cpp @@ -28,7 +28,8 @@ Node *make_node(allocator const &upstream, std::size_t initial_size, std::size_t return node; } LIBIMP_CATCH(...) { log.error("failed: allocate memory for `monotonic_buffer_resource`'s node.", - " bytes = ", initial_size, ", alignment = ", alignment); + " bytes = ", initial_size, ", alignment = ", alignment, + "\n\texception: ", ::LIBIMP::log::exception_string(std::current_exception())); return nullptr; } } @@ -86,7 +87,8 @@ void monotonic_buffer_resource::release() noexcept { free_list_ = next; } } LIBIMP_CATCH(...) { - log.error("failed: deallocate memory for `monotonic_buffer_resource`."); + log.error("failed: deallocate memory for `monotonic_buffer_resource`.", + "\n\texception: ", ::LIBIMP::log::exception_string(std::current_exception())); } // reset to initial state at contruction if ((head_ = initial_buffer_) != nullptr) {