lundi 25 juin 2018

conditional removal of macros

I have codes like this:

    #define SUPPORTS_LOGGING

    class Logger {
        // ...
        template<typename... Args>
        void info(const char* fmt, const Args&... args) { /*...*/ }
        // ...
    };

    Logger logger_ {};

    void someMethod() {
#ifdef SUPPORTS_LOGGING
        logger_.info("....");
#endif
        //....
#ifdef SUPPORTS_LOGGING
        logger_.info("....");
#endif
    }

So, my problem is, logging code clutters main code, due to lot of #ifdefs. Can we remove #ifdefs in one line? Like:

LOG_INFO(logger_, info, "%d%d%d", a, b, c);

And above code conditionally expands to: logger_.info("%d%d%d", a, b, c); if SUPPORTS_LOGGING is defined.

Can we do this?

Aucun commentaire:

Enregistrer un commentaire