mirror of
https://github.com/mutouyun/cpp-ipc.git
synced 2025-12-06 16:56:45 +08:00
Fix exception handling and logging in the code
This commit is contained in:
parent
1407d1815e
commit
9f44298350
@ -74,10 +74,9 @@ public:
|
|||||||
LIBIMP_LOG_();
|
LIBIMP_LOG_();
|
||||||
LIBIMP_TRY {
|
LIBIMP_TRY {
|
||||||
data_ = std::forward<U>(src);
|
data_ = std::forward<U>(src);
|
||||||
} LIBIMP_CATCH(std::exception const &e) {
|
|
||||||
log.error("failed: `data = std::forward<U>(src)`. error = ", e.what());
|
|
||||||
} LIBIMP_CATCH(...) {
|
} 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()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -102,20 +102,26 @@ inline auto &make_std_out() noexcept {
|
|||||||
return std_out;
|
return std_out;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// \brief Record the last information when an exception occurs.
|
/// \brief Get the exception information.
|
||||||
inline void log_exception(char const *func, std::exception_ptr eptr) noexcept {
|
inline char const *exception_string(std::exception_ptr eptr) noexcept {
|
||||||
LIBIMP_TRY {
|
LIBIMP_TRY {
|
||||||
if (func == nullptr) {
|
|
||||||
func = "-";
|
|
||||||
}
|
|
||||||
if (eptr) {
|
if (eptr) {
|
||||||
std::rethrow_exception(eptr);
|
std::rethrow_exception(eptr);
|
||||||
}
|
}
|
||||||
} LIBIMP_CATCH(std::exception const &e) {
|
} LIBIMP_CATCH(std::exception const &e) {
|
||||||
std::fprintf(stderr, "[F][%s] exception: %s\n", func, e.what());
|
return e.what();
|
||||||
} LIBIMP_CATCH(...) {
|
} 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)...),
|
std::forward_as_tuple(std::forward<A>(args)...),
|
||||||
});
|
});
|
||||||
} LIBIMP_CATCH(...) {
|
} LIBIMP_CATCH(...) {
|
||||||
log_exception(func_, std::current_exception());
|
exception_print(func_, std::current_exception());
|
||||||
}
|
}
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -49,8 +49,9 @@ std::string error_string(DWORD code) noexcept {
|
|||||||
std::string ret(len, '\0');
|
std::string ret(len, '\0');
|
||||||
cvt_cstr(lpErrText, msg_len, &ret[0], ret.size());
|
cvt_cstr(lpErrText, msg_len, &ret[0], ret.size());
|
||||||
return ret;
|
return ret;
|
||||||
} LIBIMP_CATCH(std::exception const &e) {
|
} LIBIMP_CATCH(...) {
|
||||||
log.failed(e.what());
|
log.error("failed: FormatMessage(dwMessageId = ", code, ").",
|
||||||
|
"\n\texception: ", ::LIBIMP::log::exception_string(std::current_exception()));
|
||||||
}
|
}
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|||||||
@ -28,7 +28,8 @@ Node *make_node(allocator const &upstream, std::size_t initial_size, std::size_t
|
|||||||
return node;
|
return node;
|
||||||
} LIBIMP_CATCH(...) {
|
} LIBIMP_CATCH(...) {
|
||||||
log.error("failed: allocate memory for `monotonic_buffer_resource`'s node.",
|
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;
|
return nullptr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -86,7 +87,8 @@ void monotonic_buffer_resource::release() noexcept {
|
|||||||
free_list_ = next;
|
free_list_ = next;
|
||||||
}
|
}
|
||||||
} LIBIMP_CATCH(...) {
|
} 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
|
// reset to initial state at contruction
|
||||||
if ((head_ = initial_buffer_) != nullptr) {
|
if ((head_ = initial_buffer_) != nullptr) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user