-
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
Macro expansion which converts Google Logger print format(i.e. <<) to spdlog print format #1804
Comments
I did it like this
LogLine class implements operator<< and stores everything on the right into internal stringstream bugger |
Here is the very basic demo of the approach https://github.com/artemyv/cmake_spdlog_example/tree/main/src |
Than you very much |
👍 excellent way. Unfortunately, streaming output like |
Also, I would replace the shared_ptr with regular pointer to logger for better performance since it get copied/destroyed in macro each call. |
Not sure if this way is thread safe |
LogStream variable instance is created locally inside the function - so you should not have any threading issues with it |
I thinks the line m_log->log(m_loc, m_lvl, "{}", line.str()); can be optimized to m_log->log(m_loc, m_lvl, line.str()); // skip formatting by logging the string directly |
It seems like the provided code only compiles using gcc and not on MSVC. The following code is more straight-forward for providing streaming functionality and it compiles on both compilers:
Maybe someone will find it useful. |
Thanks @BullyWiiPlaza. This is indeed useful. |
I have tons of code which uses Google Logger style of log printing:
LOG(ERROR) << "AAAA" <<< 123;
And currently I need to replace Google logger by spdlog logger, which supports python like format:
logger->error("{} {}", "param1", 123);
Of course I don't want to change each log line in the code. What I am trying to do, is to rewrite LOG(severity) macro definition. The original macro implementation is:
#define LOG(severity) COMPACT_GOOGLE_LOG_ ## severity.stream()
Can you please recommend me how should I implement the macro? My idea was that the original arguments that were passed to LOG(ERROR) (i.e. << "AAAA" <<< 123) should be printed to a string buffer(str_buffer), and the string buffer will be printed to the destination spdlog logger instance like this:
logger->error("{}", str_buffer.c_str());
What is the best idea for doing this? Thanks so much!!!
The text was updated successfully, but these errors were encountered: