Skip to content
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

Problematic implementation of LOG\LOGF macros #224

Closed
omerah opened this issue Aug 29, 2017 · 6 comments
Closed

Problematic implementation of LOG\LOGF macros #224

omerah opened this issue Aug 29, 2017 · 6 comments

Comments

@omerah
Copy link

omerah commented Aug 29, 2017

Hi,
There's a basic pitfall in the way LOG\LOGF macros were implemented:
#define LOG(level) if(g3::logLevel(level)) INTERNAL_LOG_MESSAGE(level).stream()
If we take the following code:
if (x) LOG(INFO) << "This message"; else LOG(INFO) << "That message";

After the preprocessing stage we get:
if (x) if(g3::logLevel(level)) INTERNAL_LOG_MESSAGE(level).stream() << "This message"; else if(g3::logLevel(level)) INTERNAL_LOG_MESSAGE(level).stream() << "That message";

As you can see, the compiler has no way to know that the 'else' statement belongs to the first if statement and not the the second one (that comes from the definition of the LOG macro).

The correct way to write this macro would be:
#define LOG(level) if(!g3::logLevel(level)){\ } else INTERNAL_LOG_MESSAGE(level).stream()
Which will, after preprocessing, turn into:
if (x) if(!g3::logLevel(level)){ } else INTERNAL_LOG_MESSAGE(level).stream() << "This message"; else if(!g3::logLevel(level)){ } else INTERNAL_LOG_MESSAGE(level).stream() << "That message";

Which keeps the original logic we're interested in.

@KjellKod
Copy link
Owner

Looks good. Please put up a pull request

@KjellKod
Copy link
Owner

@omerah : have you considered opening up a pull request for this change?

@maj-tom
Copy link
Contributor

maj-tom commented Oct 1, 2017

The dangling else strikes again :)
I'll try to find time this weekend to do this as well as fix the test fails in the PR I sent before (started a new job so I've been overwhelmed).

@KjellKod
Copy link
Owner

Candidate for closing unless a PR shows up. @omerah?

@omerah
Copy link
Author

omerah commented Oct 26, 2017 via email

KjellKod pushed a commit that referenced this issue Dec 7, 2017
* Fix dangling else in LOG and LOGF macros
Closes #224

* added unit test
@KjellKod
Copy link
Owner

KjellKod commented Dec 7, 2017

@maj-tom

Many thanks!

@KjellKod KjellKod mentioned this issue Mar 31, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants