Skip to content

Commit 1407d18

Browse files
committed
Refactor logging functions and add exception handling
1 parent d6f40f4 commit 1407d18

File tree

1 file changed

+30
-15
lines changed

1 file changed

+30
-15
lines changed

include/libimp/log.h

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -53,33 +53,30 @@ bool unfold_tuple_fmt_to(fmt_context &ctx, Tp const &tp, std::index_sequence<I..
5353
} // namespace detail
5454

5555
template <typename... T>
56-
bool context_to_string(fmt_context &f_ctx, context<T...> const &l_ctx) noexcept {
56+
bool context_to_string(fmt_context &f_ctx, context<T...> const &l_ctx) {
5757
static constexpr char types[] = {
5858
'T', 'D', 'I', 'W', 'E', 'F',
5959
};
60-
LIBIMP_TRY {
61-
auto ms = std::chrono::time_point_cast<std::chrono::milliseconds>(l_ctx.tp).time_since_epoch().count() % 1000;
62-
return detail::unfold_tuple_fmt_to(f_ctx, l_ctx.params, std::index_sequence_for<T...>{},
63-
"[", types[underlyof(l_ctx.level)], "]"
64-
"[", l_ctx.tp, ".", spec("03")(ms), "]"
65-
"[", l_ctx.func, "] ");
66-
} LIBIMP_CATCH(...) {
67-
return false;
68-
}
60+
auto ms = std::chrono::time_point_cast<std::chrono::milliseconds>(l_ctx.tp).time_since_epoch().count() % 1000;
61+
return detail::unfold_tuple_fmt_to(f_ctx, l_ctx.params, std::index_sequence_for<T...>{},
62+
"[", types[underlyof(l_ctx.level)], "]"
63+
"[", l_ctx.tp, ".", spec("03")(ms), "]"
64+
"[", l_ctx.func, "] ");
6965
}
7066

7167
template <typename... T>
72-
std::string context_to_string(context<T...> const &l_ctx) noexcept {
68+
std::string context_to_string(context<T...> const &l_ctx) {
69+
std::string log_txt;
70+
fmt_context f_ctx(log_txt);
7371
LIBIMP_TRY {
74-
std::string log_txt;
75-
fmt_context f_ctx(log_txt);
7672
if (!context_to_string(f_ctx, l_ctx)) {
7773
return {};
7874
}
7975
f_ctx.finish();
8076
return log_txt;
8177
} LIBIMP_CATCH(...) {
82-
return {};
78+
f_ctx.finish();
79+
throw;
8380
}
8481
}
8582

@@ -105,6 +102,22 @@ inline auto &make_std_out() noexcept {
105102
return std_out;
106103
}
107104

105+
/// \brief Record the last information when an exception occurs.
106+
inline void log_exception(char const *func, std::exception_ptr eptr) noexcept {
107+
LIBIMP_TRY {
108+
if (func == nullptr) {
109+
func = "-";
110+
}
111+
if (eptr) {
112+
std::rethrow_exception(eptr);
113+
}
114+
} LIBIMP_CATCH(std::exception const &e) {
115+
std::fprintf(stderr, "[F][%s] exception: %s\n", func, e.what());
116+
} LIBIMP_CATCH(...) {
117+
std::fprintf(stderr, "[F][%s] exception: unknown\n", func);
118+
}
119+
}
120+
108121
/**
109122
* \brief Log information base class.
110123
*/
@@ -141,7 +154,9 @@ class logger : public logger_base {
141154
l, std::chrono::system_clock::now(), func_,
142155
std::forward_as_tuple(std::forward<A>(args)...),
143156
});
144-
} LIBIMP_CATCH(...) {}
157+
} LIBIMP_CATCH(...) {
158+
log_exception(func_, std::current_exception());
159+
}
145160
return *this;
146161
}
147162

0 commit comments

Comments
 (0)