-
Notifications
You must be signed in to change notification settings - Fork 4.5k
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
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
Is it possible to log source file name, called function and line number where logging occurs? #235
Comments
You can use the SPDLOG_TRACE macro (and define SPDLOG_TRACE_ON in tweakme.h or before including spdlog.h) SPDLOG_TRACE(file_logger , "Enabled only #ifdef SPDLOG_TRACE_ON..{} ,{}", 1, 3.23); |
Ahh ok but as I see you use the following formatting: #ifdef SPDLOG_DEBUG_ON
#define SPDLOG_DEBUG(logger, ...) logger->debug(__VA_ARGS__) << " (" << __FILE__ << " #" << __LINE__ <<")";
#else
#define SPDLOG_DEBUG(logger, ...)
#endif so the filename, line number and function names are logged after the passed in formatting/arguments. Is it somehow possible to write it in the following format?
|
I have managed to write something like this #define SPDLOG_DEBUG(logger, ...) \
{ \
std::string s; \
s.reserve(64); \
s.append(__FILE__); \
s.append("#"); \
s.append(std::to_string(__LINE__)); \
s.append(": "); \
logger->debug(s + __VA_ARGS__); \
} \ but this only works for regular strings without any formatting argument because of obvious reasons. |
right. there is no obvious way for this specific format |
Just as a reference I have managed to achieve this with the following macros: #define SPDLOG_DEBUGF(logger, fmt, ...) \
{ \
logger->debug("{}::{}()#{}: " fmt, __FILE__ , __FUNCTION__, __LINE__, __VA_ARGS__); \
}
#define SPDLOG_DEBUG(logger, str) \
{ \
logger->debug("{}::{}()#{}: ", __FILE__ , __LINE__, str); \
} |
@pmalek looks good. i think i will replace the present macro implementation with this one.. |
Cool. If you decide you can let me know: I'll be more than happy to submit a PR for that :) |
sure. please submit a pr if you can |
As a second thought I believe that the current solution: #define SPDLOG_TRACE(logger, ...) logger->trace(__FILE__ ## " line " ## SPDLOG_STR(__LINE__) ## ": " ## __VA_ARGS__); is better than: #define SPDLOG_TRACEF(logger, fmt, ...) logger->trace("{}::{}()#{}: " fmt, __FILE__ , __FUNCTION__, __LINE__, __VA_ARGS__);
#define SPDLOG_TRACE(logger, str) logger->trace("{}::{}()#{}: {}", __FILE__ , __FUNCTION__, __LINE__, str); Since (not taking into account style/format):
So I believe (if you want to keep the current formatting - e.g. not printing the |
ok. thanks for the update |
This works while preserving formatting. Trick is to manually call the fmt::format on the original arguments: |
Thanks. It's might be expensive though.. |
Yes, this is a concern. With such macros one can limit argument expansion/execution by adding an The previous version So full answer could be The additional level check doesn't look nice, but at least is cheap and will pay for itself on a large project. Leaving it here just for reference. I got here looking for this answer as well, so maybe it will help someone. |
The last macro (thanks, useful!) does not behave intuitively when branching, like
Maybe someone finds it useful. |
Macro overloading is a possible solution to the problem of requiring two different macros depending on whether formatting is used. E.g.
Below is a method to overload macros based on the number of input arguments, adapted from: https://rextester.com/ONP80107 . This solution currently works for up to 9 arguments, and could easily be expanded. E.g.
Solution:
Examples:
|
I am wondering whether it is possible source file name, called function and line number where logging occurs.
Something like:
Is it possible?
The text was updated successfully, but these errors were encountered: