From 119467c580d019cfb23d833f83b84b8642703719 Mon Sep 17 00:00:00 2001 From: Wolfgang Petroschka Date: Fri, 13 Aug 2021 12:11:59 +0200 Subject: [PATCH 1/6] Added additional information for error handler Useful when formatting log messages fails. Now you can tell which log message caused the problem. --- include/spdlog/async_logger-inl.h | 4 ++-- include/spdlog/logger-inl.h | 4 ++-- include/spdlog/logger.h | 12 ++++++------ 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/include/spdlog/async_logger-inl.h b/include/spdlog/async_logger-inl.h index 6fac8fc9..9f94d14f 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() + SPDLOG_LOGGER_CATCH(msg.payload) } } @@ -80,7 +80,7 @@ SPDLOG_INLINE void spdlog::async_logger::backend_flush_() { sink->flush(); } - SPDLOG_LOGGER_CATCH() + SPDLOG_LOGGER_CATCH("") } } diff --git a/include/spdlog/logger-inl.h b/include/spdlog/logger-inl.h index fd841657..c44caff3 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() + SPDLOG_LOGGER_CATCH(msg.payload) } } @@ -203,7 +203,7 @@ SPDLOG_INLINE void logger::flush_() { sink->flush(); } - SPDLOG_LOGGER_CATCH() + SPDLOG_LOGGER_CATCH("") } } diff --git a/include/spdlog/logger.h b/include/spdlog/logger.h index ff3bef58..43ac4fb4 100644 --- a/include/spdlog/logger.h +++ b/include/spdlog/logger.h @@ -28,10 +28,10 @@ #include #ifndef SPDLOG_NO_EXCEPTIONS -# define SPDLOG_LOGGER_CATCH() \ +# define SPDLOG_LOGGER_CATCH(additional_info) \ catch (const std::exception &ex) \ { \ - err_handler_(ex.what()); \ + err_handler_(fmt::format("{} ({})", ex.what(), additional_info)); \ } \ catch (...) \ { \ @@ -39,7 +39,7 @@ throw; \ } #else -# define SPDLOG_LOGGER_CATCH() +# define SPDLOG_LOGGER_CATCH(additional_info) #endif namespace spdlog { @@ -333,7 +333,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() + SPDLOG_LOGGER_CATCH(fmt) } #ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT @@ -356,7 +356,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() + SPDLOG_LOGGER_CATCH(fmt) } // T can be statically converted to wstring_view, and no formatting needed. @@ -376,7 +376,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() + SPDLOG_LOGGER_CATCH(msg) } #endif // SPDLOG_WCHAR_TO_UTF8_SUPPORT From 388679b00e6b07fc20767a6eecafded8fb6eadfd Mon Sep 17 00:00:00 2001 From: Wolfgang Petroschka Date: Fri, 13 Aug 2021 12:30:49 +0200 Subject: [PATCH 2/6] Fix empty additional info does not work with wchar_t based string. --- include/spdlog/async_logger-inl.h | 2 +- include/spdlog/logger-inl.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/spdlog/async_logger-inl.h b/include/spdlog/async_logger-inl.h index 9f94d14f..60126cf0 100644 --- a/include/spdlog/async_logger-inl.h +++ b/include/spdlog/async_logger-inl.h @@ -80,7 +80,7 @@ SPDLOG_INLINE void spdlog::async_logger::backend_flush_() { sink->flush(); } - SPDLOG_LOGGER_CATCH("") + SPDLOG_LOGGER_CATCH(string_view_t()) } } diff --git a/include/spdlog/logger-inl.h b/include/spdlog/logger-inl.h index c44caff3..b6d02624 100644 --- a/include/spdlog/logger-inl.h +++ b/include/spdlog/logger-inl.h @@ -203,7 +203,7 @@ SPDLOG_INLINE void logger::flush_() { sink->flush(); } - SPDLOG_LOGGER_CATCH("") + SPDLOG_LOGGER_CATCH(string_view_t()) } } From c98b29aa6744a9a70b3af2084490fbda97f66cb8 Mon Sep 17 00:00:00 2001 From: Wolfgang Petroschka Date: Fri, 13 Aug 2021 12:49:02 +0200 Subject: [PATCH 3/6] Fix empty additional info, 2nd try There's actually a diffent string view type for wide string... --- include/spdlog/async_logger-inl.h | 4 ++++ include/spdlog/logger-inl.h | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/include/spdlog/async_logger-inl.h b/include/spdlog/async_logger-inl.h index 60126cf0..db449d61 100644 --- a/include/spdlog/async_logger-inl.h +++ b/include/spdlog/async_logger-inl.h @@ -80,7 +80,11 @@ SPDLOG_INLINE void spdlog::async_logger::backend_flush_() { sink->flush(); } +#if (defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT) || defined(SPDLOG_WCHAR_FILENAMES)) && defined(_WIN32) + SPDLOG_LOGGER_CATCH(wstring_view_t()) +#else SPDLOG_LOGGER_CATCH(string_view_t()) +#endif } } diff --git a/include/spdlog/logger-inl.h b/include/spdlog/logger-inl.h index b6d02624..9d6e082e 100644 --- a/include/spdlog/logger-inl.h +++ b/include/spdlog/logger-inl.h @@ -203,7 +203,11 @@ SPDLOG_INLINE void logger::flush_() { sink->flush(); } +#if (defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT) || defined(SPDLOG_WCHAR_FILENAMES)) && defined(_WIN32) + SPDLOG_LOGGER_CATCH(wstring_view_t()) +#else SPDLOG_LOGGER_CATCH(string_view_t()) +#endif } } From df45d78d143af03e76e54ce7d86eaea40ba37362 Mon Sep 17 00:00:00 2001 From: Wolfgang Petroschka Date: Fri, 13 Aug 2021 13:53:35 +0200 Subject: [PATCH 4/6] Windows/wchar problems Mixing char types in libfmt is a problem and WIP. --- include/spdlog/async_logger-inl.h | 6 +----- include/spdlog/logger-inl.h | 6 +----- include/spdlog/logger.h | 8 ++++++-- 3 files changed, 8 insertions(+), 12 deletions(-) diff --git a/include/spdlog/async_logger-inl.h b/include/spdlog/async_logger-inl.h index db449d61..9f94d14f 100644 --- a/include/spdlog/async_logger-inl.h +++ b/include/spdlog/async_logger-inl.h @@ -80,11 +80,7 @@ SPDLOG_INLINE void spdlog::async_logger::backend_flush_() { sink->flush(); } -#if (defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT) || defined(SPDLOG_WCHAR_FILENAMES)) && defined(_WIN32) - SPDLOG_LOGGER_CATCH(wstring_view_t()) -#else - SPDLOG_LOGGER_CATCH(string_view_t()) -#endif + SPDLOG_LOGGER_CATCH("") } } diff --git a/include/spdlog/logger-inl.h b/include/spdlog/logger-inl.h index 9d6e082e..c44caff3 100644 --- a/include/spdlog/logger-inl.h +++ b/include/spdlog/logger-inl.h @@ -203,11 +203,7 @@ SPDLOG_INLINE void logger::flush_() { sink->flush(); } -#if (defined(SPDLOG_WCHAR_TO_UTF8_SUPPORT) || defined(SPDLOG_WCHAR_FILENAMES)) && defined(_WIN32) - SPDLOG_LOGGER_CATCH(wstring_view_t()) -#else - SPDLOG_LOGGER_CATCH(string_view_t()) -#endif + SPDLOG_LOGGER_CATCH("") } } diff --git a/include/spdlog/logger.h b/include/spdlog/logger.h index 43ac4fb4..6cc70dd5 100644 --- a/include/spdlog/logger.h +++ b/include/spdlog/logger.h @@ -356,7 +356,9 @@ 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) + // TODO: This isn't working yet. + SPDLOG_LOGGER_CATCH("") + //SPDLOG_LOGGER_CATCH(fmt) } // T can be statically converted to wstring_view, and no formatting needed. @@ -376,7 +378,9 @@ 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(msg) + // TODO: This isn't working yet. + SPDLOG_LOGGER_CATCH("") + //SPDLOG_LOGGER_CATCH(msg) } #endif // SPDLOG_WCHAR_TO_UTF8_SUPPORT From ed27592537802d403f09c4736dfaff1a8c91d686 Mon Sep 17 00:00:00 2001 From: Wolfgang Petroschka Date: Tue, 17 Aug 2021 15:26:59 +0200 Subject: [PATCH 5/6] 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 From 0d10e21c2fee9142da39d03d4507ff8554349e37 Mon Sep 17 00:00:00 2001 From: Wolfgang Petroschka Date: Tue, 17 Aug 2021 17:50:35 +0200 Subject: [PATCH 6/6] Remove inner try catch in SPDLOG_LOGGER_CATCH The fmt::format call should not throw formatting the exception message and the source code location. --- include/spdlog/logger.h | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/include/spdlog/logger.h b/include/spdlog/logger.h index 70c79ed5..ce42d69d 100644 --- a/include/spdlog/logger.h +++ b/include/spdlog/logger.h @@ -33,14 +33,7 @@ { \ if(location.filename) \ { \ - try \ - { \ - err_handler_(fmt::format("{} [{}({})]", ex.what(), location.filename, location.line)); \ - } \ - catch (const std::exception &ex) \ - { \ - err_handler_(ex.what()); \ - } \ + err_handler_(fmt::format("{} [{}({})]", ex.what(), location.filename, location.line)); \ } \ else \ { \