Fix exception handling and logging in the code

This commit is contained in:
mutouyun 2024-03-10 13:38:28 +08:00
parent 1407d1815e
commit 9f44298350
4 changed files with 23 additions and 15 deletions

View File

@ -74,10 +74,9 @@ public:
LIBIMP_LOG_();
LIBIMP_TRY {
data_ = std::forward<U>(src);
} LIBIMP_CATCH(std::exception const &e) {
log.error("failed: `data = std::forward<U>(src)`. error = ", e.what());
} LIBIMP_CATCH(...) {
log.error("failed: `data = std::forward<U>(src)`. error = unknown");
log.error("failed: `data = std::forward<U>(src)`.",
"\n\texception: ", ::LIBIMP::log::exception_string(std::current_exception()));
}
}

View File

@ -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<A>(args)...),
});
} LIBIMP_CATCH(...) {
log_exception(func_, std::current_exception());
exception_print(func_, std::current_exception());
}
return *this;
}

View File

@ -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 {};
}

View File

@ -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) {