From ed27592537802d403f09c4736dfaff1a8c91d686 Mon Sep 17 00:00:00 2001 From: Wolfgang Petroschka Date: Tue, 17 Aug 2021 15:26:59 +0200 Subject: [PATCH] Switch additional information to source location of bad log message --- include/spdlog/async_logger-inl.h | 4 ++-- include/spdlog/logger-inl.h | 4 ++-- include/spdlog/logger.h | 28 +++++++++++++++++++--------- 3 files changed, 23 insertions(+), 13 deletions(-) diff --git a/include/spdlog/async_logger-inl.h b/include/spdlog/async_logger-inl.h index 9f94d14f..a1c27a59 100644 --- a/include/spdlog/async_logger-inl.h +++ b/include/spdlog/async_logger-inl.h @@ -62,7 +62,7 @@ SPDLOG_INLINE void spdlog::async_logger::backend_sink_it_(const details::log_msg { sink->log(msg); } - SPDLOG_LOGGER_CATCH(msg.payload) + SPDLOG_LOGGER_CATCH(msg.source) } } @@ -80,7 +80,7 @@ SPDLOG_INLINE void spdlog::async_logger::backend_flush_() { sink->flush(); } - SPDLOG_LOGGER_CATCH("") + SPDLOG_LOGGER_CATCH(source_loc()) } } diff --git a/include/spdlog/logger-inl.h b/include/spdlog/logger-inl.h index c44caff3..411f2cb5 100644 --- a/include/spdlog/logger-inl.h +++ b/include/spdlog/logger-inl.h @@ -185,7 +185,7 @@ SPDLOG_INLINE void logger::sink_it_(const details::log_msg &msg) { sink->log(msg); } - SPDLOG_LOGGER_CATCH(msg.payload) + SPDLOG_LOGGER_CATCH(msg.source) } } @@ -203,7 +203,7 @@ SPDLOG_INLINE void logger::flush_() { sink->flush(); } - SPDLOG_LOGGER_CATCH("") + SPDLOG_LOGGER_CATCH(source_loc()) } } diff --git a/include/spdlog/logger.h b/include/spdlog/logger.h index 6cc70dd5..70c79ed5 100644 --- a/include/spdlog/logger.h +++ b/include/spdlog/logger.h @@ -28,10 +28,24 @@ #include #ifndef SPDLOG_NO_EXCEPTIONS -# define SPDLOG_LOGGER_CATCH(additional_info) \ +# define SPDLOG_LOGGER_CATCH(location) \ catch (const std::exception &ex) \ { \ - err_handler_(fmt::format("{} ({})", ex.what(), additional_info)); \ + if(location.filename) \ + { \ + try \ + { \ + err_handler_(fmt::format("{} [{}({})]", ex.what(), location.filename, location.line)); \ + } \ + catch (const std::exception &ex) \ + { \ + err_handler_(ex.what()); \ + } \ + } \ + else \ + { \ + err_handler_(ex.what()); \ + } \ } \ catch (...) \ { \ @@ -333,7 +347,7 @@ protected: details::log_msg log_msg(loc, name_, lvl, string_view_t(buf.data(), buf.size())); log_it_(log_msg, log_enabled, traceback_enabled); } - SPDLOG_LOGGER_CATCH(fmt) + SPDLOG_LOGGER_CATCH(loc) } #ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT @@ -356,9 +370,7 @@ protected: details::log_msg log_msg(loc, name_, lvl, string_view_t(buf.data(), buf.size())); log_it_(log_msg, log_enabled, traceback_enabled); } - // TODO: This isn't working yet. - SPDLOG_LOGGER_CATCH("") - //SPDLOG_LOGGER_CATCH(fmt) + SPDLOG_LOGGER_CATCH(loc) } // T can be statically converted to wstring_view, and no formatting needed. @@ -378,9 +390,7 @@ protected: details::log_msg log_msg(loc, name_, lvl, string_view_t(buf.data(), buf.size())); log_it_(log_msg, log_enabled, traceback_enabled); } - // TODO: This isn't working yet. - SPDLOG_LOGGER_CATCH("") - //SPDLOG_LOGGER_CATCH(msg) + SPDLOG_LOGGER_CATCH(loc) } #endif // SPDLOG_WCHAR_TO_UTF8_SUPPORT