-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Poco::Logger can not output __FILE__ and __LINE__ #4553
Comments
Are you running into this issue with |
Yes, you are right ! void information(const std::string& msg, const char* file, int line);
void information(const std::string& msg, const char* file, long int line); // new method to add to fix the problem ? |
That would be a breaking change to any existing calls to |
Hi! void information(const std::string& msg, const char* file, int line); with template<typename T, std::enable_if_t<std::is_integral<T>::value, bool> = true>
void information(const std::string& msg, const char* file, T line); ? |
@bas524 that looks like it should work |
@siren186, can you help me to set correct compiler options? I want to verify my changes in godbolt, but I can't get described behavour when |
Opinions aside, this is a breaking change. Using a template doesn't change the output. The same thing would happen with just Any calls to the parameter pack version of |
@andrewauclair , I think that I found another good way to resolve the problem without templates namespace Poco {
using LineNumber = decltype(__LINE__);
} and then void log(const std::string& text, Message::Priority prio, const char* file, LineNumber line); |
That would cause the same issue. Although it would only cause the issue for when the type of The ultimate (but sadly still breaking) change would be to separate the log functions that takes file and line from the other log functions. I've wanted to add some optional C++20 logging with |
It compiled with errors.
It works well !!! |
IMO this is the most elegant solution because the type for the line number is derived from the compiler/platform. |
Here's what I think would be a good compromise. A breaking change is required to properly account for As a bonus, we can define a version of Godbolt for this code: https://godbolt.org/z/rM1T3d5qa #include <iostream>
#include <string>
#include <format>
#if __has_include(<source_location>)
#include <source_location>
#endif
struct Logger {
// delete the existing log function with file and line to make the breaking change obvious
void log(const std::string& msg, const char* file, int line) = delete;
// add a new logging function with a different name to prevent confusion with the variadic template version
void logWithLocation(const std::string& msg, const char* file, decltype(__LINE__) line) {
std::cout << file << ":" << line << ": " << msg << '\n';
}
// new log function with location using the C++20 std::source_location for information
#if __has_include(<source_location>)
void logWithLocation(const std::string& msg, std::source_location location = std::source_location::current()) {
std::cout << location.file_name() << '('
<< location.line() << ':'
<< location.column() << ") `"
<< location.function_name() << ": " << msg << '\n';
}
#endif
template<typename T, typename... Args>
void log (const std::string& format, T t, Args... args) {
std::cout << std::vformat(format, std::make_format_args(t, args...)) << '\n';
}
};
int main()
{
Logger logger;
logger.logWithLocation("failed here", __FILE__, __LINE__);
logger.logWithLocation("failed again");
logger.log("{}:{}", "127.0.0.1", 5000L);
}
|
MS Visual Studio can use type long for __LINE__ macro when /ZI compilation flag is used - https://learn.microsoft.com/en-us/cpp/build/ reference/z7-zi-zi-debug-information-format?view=msvc-170#zi-1 This breaks some poco interfaces, for ex. logger We should fix type for line number
MS Visual Studio can use type long for __LINE__ macro when /ZI compilation flag is used - https://learn.microsoft.com/en-us/cpp/build/ reference/z7-zi-zi-debug-information-format?view=msvc-170#zi-1 This breaks some poco interfaces, for ex. logger We should fix type for line number
Describe the bug
Poco::Logger
can not output__FILE__
and__LINE__
To Reproduce
If the type of
__LINE__
is int,Poco::Logger
will call this method:But if the type of
__LINE__
is long,Poco::Logger
will call this method:Then it will lost
__FILE__
and__LINE__
.Please add relevant environment information:
The text was updated successfully, but these errors were encountered: