@@ -53,33 +53,30 @@ bool unfold_tuple_fmt_to(fmt_context &ctx, Tp const &tp, std::index_sequence<I..
53
53
} // namespace detail
54
54
55
55
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) {
57
57
static constexpr char types[] = {
58
58
' T' , ' D' , ' I' , ' W' , ' E' , ' F' ,
59
59
};
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 , " ] " );
69
65
}
70
66
71
67
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);
73
71
LIBIMP_TRY {
74
- std::string log_txt;
75
- fmt_context f_ctx (log_txt);
76
72
if (!context_to_string (f_ctx, l_ctx)) {
77
73
return {};
78
74
}
79
75
f_ctx.finish ();
80
76
return log_txt;
81
77
} LIBIMP_CATCH (...) {
82
- return {};
78
+ f_ctx.finish ();
79
+ throw ;
83
80
}
84
81
}
85
82
@@ -105,6 +102,22 @@ inline auto &make_std_out() noexcept {
105
102
return std_out;
106
103
}
107
104
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
+
108
121
/* *
109
122
* \brief Log information base class.
110
123
*/
@@ -141,7 +154,9 @@ class logger : public logger_base {
141
154
l, std::chrono::system_clock::now (), func_,
142
155
std::forward_as_tuple (std::forward<A>(args)...),
143
156
});
144
- } LIBIMP_CATCH (...) {}
157
+ } LIBIMP_CATCH (...) {
158
+ log_exception (func_, std::current_exception ());
159
+ }
145
160
return *this ;
146
161
}
147
162
0 commit comments