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

Fix undetected block issue in a debug macro #74

Open
JosueGauthier opened this issue May 23, 2023 · 0 comments
Open

Fix undetected block issue in a debug macro #74

JosueGauthier opened this issue May 23, 2023 · 0 comments

Comments

@JosueGauthier
Copy link

JosueGauthier commented May 23, 2023

Hello,

I made a small macro to display a debugging log.
Except that when you forget to specify the argument after using brackets, it causes an undetected block

Example:

LOG_DEBUG("{}::blabla") -> The code crashes at runtime with no errors or anything.

correct version: LOG_DEBUG("{}::blabla", a.x) ;

# define LOG_DEBUG(...)
    { \
        logd(__VA_ARGS__) ; \N- fmtlog::poll("{}::blabla") ; \N- cpp
        fmtlog::poll() ; \N- \N- \N- \N- \N- \N
    }

Would you have a solution to avoid this kind of error? I looked at static assert when trying to create a template function, but when I don't want to use brackets in my debugging macro, I also get an error at compile time (e.g. LOG_DEBUG("hello"); ) :

#include <stdexcept>

template <typename... Args>
void logDebug(const char* format, const Args&... args)
{
    static_assert(sizeof...(Args) > 0, "Missing argument !") ;

    try {
        logd(format, args...) ;
    } catch (const std::exception& e) {
        loge("Error : {}", e.what()) ;
    }

    fmtlog::poll() ;
}

struct LogDebugWrapper {
    template <typename... Args>
    LogDebugWrapper(const char* format, const Args&... args)
    {
        logDebug(format, args...) ;
    }
} ;

#define LOG_DEBUG(...) LogDebugWrapper(__VA_ARGS__)

Thanks,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant