Skip to content

Commit 9f44298

Browse files
committed
Fix exception handling and logging in the code
1 parent 1407d18 commit 9f44298

File tree

4 files changed

+23
-15
lines changed

4 files changed

+23
-15
lines changed

include/libconcur/element.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,9 @@ class element {
7474
LIBIMP_LOG_();
7575
LIBIMP_TRY {
7676
data_ = std::forward<U>(src);
77-
} LIBIMP_CATCH(std::exception const &e) {
78-
log.error("failed: `data = std::forward<U>(src)`. error = ", e.what());
7977
} LIBIMP_CATCH(...) {
80-
log.error("failed: `data = std::forward<U>(src)`. error = unknown");
78+
log.error("failed: `data = std::forward<U>(src)`.",
79+
"\n\texception: ", ::LIBIMP::log::exception_string(std::current_exception()));
8180
}
8281
}
8382

include/libimp/log.h

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -102,20 +102,26 @@ inline auto &make_std_out() noexcept {
102102
return std_out;
103103
}
104104

105-
/// \brief Record the last information when an exception occurs.
106-
inline void log_exception(char const *func, std::exception_ptr eptr) noexcept {
105+
/// \brief Get the exception information.
106+
inline char const *exception_string(std::exception_ptr eptr) noexcept {
107107
LIBIMP_TRY {
108-
if (func == nullptr) {
109-
func = "-";
110-
}
111108
if (eptr) {
112109
std::rethrow_exception(eptr);
113110
}
114111
} LIBIMP_CATCH(std::exception const &e) {
115-
std::fprintf(stderr, "[F][%s] exception: %s\n", func, e.what());
112+
return e.what();
116113
} LIBIMP_CATCH(...) {
117-
std::fprintf(stderr, "[F][%s] exception: unknown\n", func);
114+
return "unknown";
115+
}
116+
return "none";
117+
}
118+
119+
/// \brief Record the last information when an exception occurs.
120+
inline void exception_print(char const *func, std::exception_ptr eptr) noexcept {
121+
if (func == nullptr) {
122+
func = "-";
118123
}
124+
std::fprintf(stderr, "[F][%s] exception: %s\n", func, exception_string(eptr));
119125
}
120126

121127
/**
@@ -155,7 +161,7 @@ class logger : public logger_base {
155161
std::forward_as_tuple(std::forward<A>(args)...),
156162
});
157163
} LIBIMP_CATCH(...) {
158-
log_exception(func_, std::current_exception());
164+
exception_print(func_, std::current_exception());
159165
}
160166
return *this;
161167
}

src/libimp/platform/win/system.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,9 @@ std::string error_string(DWORD code) noexcept {
4949
std::string ret(len, '\0');
5050
cvt_cstr(lpErrText, msg_len, &ret[0], ret.size());
5151
return ret;
52-
} LIBIMP_CATCH(std::exception const &e) {
53-
log.failed(e.what());
52+
} LIBIMP_CATCH(...) {
53+
log.error("failed: FormatMessage(dwMessageId = ", code, ").",
54+
"\n\texception: ", ::LIBIMP::log::exception_string(std::current_exception()));
5455
}
5556
return {};
5657
}

src/libpmr/monotonic_buffer_resource.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ Node *make_node(allocator const &upstream, std::size_t initial_size, std::size_t
2828
return node;
2929
} LIBIMP_CATCH(...) {
3030
log.error("failed: allocate memory for `monotonic_buffer_resource`'s node.",
31-
" bytes = ", initial_size, ", alignment = ", alignment);
31+
" bytes = ", initial_size, ", alignment = ", alignment,
32+
"\n\texception: ", ::LIBIMP::log::exception_string(std::current_exception()));
3233
return nullptr;
3334
}
3435
}
@@ -86,7 +87,8 @@ void monotonic_buffer_resource::release() noexcept {
8687
free_list_ = next;
8788
}
8889
} LIBIMP_CATCH(...) {
89-
log.error("failed: deallocate memory for `monotonic_buffer_resource`.");
90+
log.error("failed: deallocate memory for `monotonic_buffer_resource`.",
91+
"\n\texception: ", ::LIBIMP::log::exception_string(std::current_exception()));
9092
}
9193
// reset to initial state at contruction
9294
if ((head_ = initial_buffer_) != nullptr) {

0 commit comments

Comments
 (0)