-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Formatting of null-pointers (char*)? #1009
Comments
@tomszilagyi Thanks for the update. Closing it. |
Hello, i still see the error on windows with spdlog 1.3.1. I don't understand the fmt-fix or what to do with that. Simple test case: const char* emtpy = nullptr;
SPDLOG_INFO("{}", empty); |
try casting to void* before passing it |
Thanks for the quick answer. Casting it prints 0x0 for a null pointer (OK) but breaks printing the real value for non-null values. (not OK) |
oh, so you can try cast to string view instead: https://stackoverflow.com/a/47240730 |
I'm pre c++17 and don't have std::string_view. I tried the bundled fmt::string_view but this gives an exception with a nullptr. |
char* nullptr_char = nullptr;
std::cout << *nullptr_char << std::endl; // Segmentation fault
char* nullptr_char = nullptr;
std::string null_str(nullptr_char); // Abort
std::cout << null_str << std::endl; The empty characters is '\n': https://wandbox.org/permlink/cGF3hn330ARw8b5x char empty[] = { '\0' };
char* empty_char = empty;
std::cout << *empty_char << std::endl; |
I know this, but it does not matter. I am getting the null pointer from an external library. My request is the same like the original poster had:
My main problem is, that the error message does not include the original maybe unformatted message or filename and linenumber like my normal logs. So it is impossible to say where this error occurs. My current workaround is to wrap all possible null pointer in something like |
Hi,
I am using spdlog 1.3.1 with bundled fmt on MacOS X.
Not sure if this is a bug or a feature. If I have a char* (C-string) that is sometimes a nullptr, and in that case I get this when attempting to log it (nothing fancy, just
spdlog::debug("ptr={}\n", ptr);
):Naturally, I get the correct log output when
ptr
is non-null and points to a valid C-string.What I would prefer is if, instead of the above error message, I would get an indication that the passed value is null, with the rest of the format string rendered, perhaps like plain old printf in glibc:
Is this achievable somehow? I would prefer not to add if-checks for such logging entries.
N.B.: After some digging around, I see that fmtlib has a fix for this, see fmtlib/fmt#226
However, the check is only included when using
fmt::printf
.Thanks!
The text was updated successfully, but these errors were encountered: