Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added additional information for error handler #2048

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Switch additional information to source location of bad log message
  • Loading branch information
D-r-P-3-p-p-3-r committed Aug 17, 2021
commit ed27592537802d403f09c4736dfaff1a8c91d686
4 changes: 2 additions & 2 deletions include/spdlog/async_logger-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand All @@ -80,7 +80,7 @@ SPDLOG_INLINE void spdlog::async_logger::backend_flush_()
{
sink->flush();
}
SPDLOG_LOGGER_CATCH("")
SPDLOG_LOGGER_CATCH(source_loc())
}
}

Expand Down
4 changes: 2 additions & 2 deletions include/spdlog/logger-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand All @@ -203,7 +203,7 @@ SPDLOG_INLINE void logger::flush_()
{
sink->flush();
}
SPDLOG_LOGGER_CATCH("")
SPDLOG_LOGGER_CATCH(source_loc())
}
}

Expand Down
28 changes: 19 additions & 9 deletions include/spdlog/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,24 @@
#include <vector>

#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 \
{ \
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont think an extra try catch needed here. why would it fail? the lib is in total control of the source loc param

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just figured as fmt::format can throw (e.g. in case of OOM), it would be the safest bet to catch.
I'll remove it.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, please remove it and I will merge.

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 (...) \
{ \
Expand Down Expand Up @@ -333,7 +347,7 @@ class SPDLOG_API logger
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
Expand All @@ -356,9 +370,7 @@ class SPDLOG_API logger
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.
Expand All @@ -378,9 +390,7 @@ class SPDLOG_API logger
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
Expand Down