-
Notifications
You must be signed in to change notification settings - Fork 4.6k
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
Using std::source_location. #2667
base: v1.x
Are you sure you want to change the base?
Conversation
Thanks. To add this fully means duplicating all of this for all levels (trace(), debug(), warn(), error(), critical()) in spdlog.h AND in logger.h class declarations. We need to find a way to achieve this without bloating the code. Not sure how.. |
… in the std library used. Implementation extended to all logging levels.
Readded the possibility of using spdlog::info(123).
I have now extended the implementation to all the other logging levels and managed to reintroduce support for code like |
Added overloading for std::source_location in logger class.
Hello all, // common.h
template<typename T>
struct format_string_wrapper
{
format_string_wrapper(const char* fs, spdlog::details::source_location loc = spdlog::details::source_location::current())
: format_string_{fs}
, loc_{loc}
{}
operator T()
{
return format_string_;
}
T format_string_;
spdlog::details::source_location loc_;
};
template<typename... Args>
using format_string_t = format_string_wrapper<fmt::format_string<Args...>>;
//logger.h
template<typename... Args>
void log(source_loc loc, level::level_enum lvl, format_string_t<Args...> fmt, Args &&... args)
{
log_(loc, lvl, details::to_string_view(fmt.format_string_), std::forward<Args>(args)...);
} |
Just wanted to second the approach @mguludag posted — I've been using something similar for a few years in various codebases at work. I actually found this pull request when searching around to see if anyone else had come up with this idea. I have a very basic implementation here that I've used for some personal projects. Unfortunately a defaulted An alternate approach is a "double call" syntax, something like |
I implemented source_location into spdlog but I removed extra default source_location parameters from spdlog's templated non format_str log functions. Using log functions with format_string the source_location feature works fine. |
It would be great to have support for std::source_location without using the macro version when compiling for C++ 20 (related issue at #1823).
I drafted an idea on how to implement that.
The only issue with this implementation is that it would introduce a small breaking change:
spdlog::info(1)
would not compile anymore. However, that does not work with fmt anyway.