-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Example code how to use your library with the c++20 source_location #1959
Comments
Thanks. Very cool! Why |
Nice! That would be cool! because clang 12.0.0 does not yet support |
For the #ifdef __cpp_lib_source_location
#include <source_location>
using source_location = std::source_location;
#else
#include <experimental/source_location>
using source_location = std::experimental::source_location;
#endif |
@gabime This was neat and totally worked. I see that a 2.x branch is being developed. Is support for std::source_location being taken into consideration for it? |
Not yet. PRs are welcome. |
Why in all print functions, spdlog prints only the file name and line, and does not print the function name (which is more important than the file name for my opinion)? I needed to add it manually to get it. |
Use https://spdlog.docsforge.com/v1.x/3.custom-formatting/#pattern-flags |
Hi.
Thanks for the answer.
Using this pattern is only for the macro code.
I want to use the regular methods, which uses spdlog::source_loc.
However, when I looked at the implementation, I saw that from the loc
object only the file name and line number is taken, never the function name.
That was my comment about.
Thanks,
Miri.
…On Wed, Feb 2, 2022 at 8:57 PM David Pfahler ***@***.***> wrote:
Use %! as pattern:
https://spdlog.docsforge.com/v1.x/3.custom-formatting/#pattern-flags
—
Reply to this email directly, view it on GitHub
<#1959 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AS6XI3W4CYF5W54F7UNO2SLUZF5CZANCNFSM452YVZSQ>
.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>.
You are receiving this because you commented.Message ID:
***@***.***>
|
@theShmoo, would you be able to update your example for MSVC please. I tried to by removing ::experimental and adding template <typename... Args>
void warn(format_with_location fmt, Args &&...args)
{
spdlog::default_logger_raw()->log(fmt.loc, spdlog::level::warn, fmt.value,
std::forward<Args>(args)...); // NOSONAR
} fmt.value |
sorry the snipped I posted is no longer like this in my code base and I don't use MSVC so you have to figure it out yourself. Maybe you can post a not working example via Godbolt and I can take a look. |
if removing ::experimental, i create a souce_location instead
then
|
|
https://godbolt.org/z/Y3G4PW47n Hi! Here is a working example #include <spdlog/spdlog.h>
#include <iostream>
#include <source_location>
#include <string_view>
namespace logging {
using source_location = std::source_location;
[[nodiscard]] constexpr auto get_log_source_location(
const source_location &location) {
return spdlog::source_loc{location.file_name(),
static_cast<std::int32_t>(location.line()),
location.function_name()};
}
struct format_with_location {
std::string_view value;
spdlog::source_loc loc;
template <typename String>
format_with_location(const String &s, const source_location &location =
source_location::current())
: value{s}, loc{get_log_source_location(location)} {}
};
template <typename... Args>
void warn(format_with_location fmt, Args &&...args) {
spdlog::default_logger_raw()->log(fmt.loc, spdlog::level::warn,
fmt::runtime(fmt.value),
std::forward<Args>(args)...);
}
} // namespace logging
int main() {
spdlog::set_level(spdlog::level::trace);
std::string message = "hello {}\n";
auto value = 42;
logging::warn(message, value);
} |
@theShmoo Thanks so much, I've just tried it in VS2022 and it works great. |
I began investigating this issue for version 2.x. One problem is that the compiler embeds the location information for all code that uses spdlog, even if the user doesn't need this feature or only logs conditionally. It seems that the only way to avoid this is by defining a flag before including spdlog, such as Any thoughts? |
@gabime I assume performance is why Case it really is slower, I would prefer a For a specialized API I would prefer something like this:
What you are suggesting is:
This would result in Conversely, users who need maximum performance and definitely don't need All in all, I would prefer the Specialized API approach if including |
v2.x branch already supports source location. you need to define #define SPDLOG_SOURCE_LOCATION before including spdlog.h. Please try it out and feedback. It requires to compile with cpp 20(-DCMAKE_CXX_STANDARD=20) although it seems to even work with some compilers with cpp 17 (which is the default standard in spdlog v2.x). |
@gabime I've seen that, and I already use it ;-) However, I don't like the current solution with the macro to turn on. A define to switch off would be better in my opinion. I would find a specialized API for log without location even better. In the ideal case, a benchmark could show that the location as default parameter does not bring any performance disadvantages if it is not used. In this case, you could do without a define solution and a specialized API. |
Since embedding all source locations into the product binary is probably not desirable for many products, I decided to disable it by default and enable only if the macro is defined. Seems easy enough, instead of replicating the whole API. Feel free to suggest other means. As long as by default it is turned off. |
Yeah okay, unwanted source location strings in (commercial) binaries are a strong argument. |
Not sure what I was running at this point, as I found the project I had been using to test this, and tried just now but it failed in the VS2022 preview. Managed to update the example to work, and instead use C++20s std::format instead. #include <format>
... Same code
template <typename... Args>
void warn(format_with_location fmt, Args &&...args)
{
spdlog::default_logger_raw( )-> log(fmt.loc, spdlog::level::warn, std::vformat(fmt.value, std::make_format_args(std::forward<Args>(args)...)));
} |
Implemented in v2.x |
I created an example code on how to use your library with the c++20
source_location
instead of your macros:https://godbolt.org/z/aqdYjo6nE
Because it took some time to fiddle with the type deduction I wanted to share this. Maybe you could add it to the documentation?
Thanks for your great library!
The text was updated successfully, but these errors were encountered: