KaiserJohaan
KaiserJohaan

Reputation: 9240

C++ - variadic functions and cout

I have a logging function which accepts variadic parameters. This works fine for say android logging and printf, but I want to do the same with std::cout and file streams. Is there an easy way solve this?

void LogManagerImpl::LogInfo(const char* msg, ...)
    {
        va_list argptr;
        va_start(argptr, msg);

        /* Log to stdout */
        if (mLogToStdOut)
        {
            #ifdef ANDROID
                __android_log_vprint(ANDROID_LOG_INFO, __ENGINE_LOG_TAG, msg, argptr);
            #elif defined _WIN32 || _WIN64
                //printf ("%s:%s",__ENGINE_LOG_TAG,"INFO:"); vprintf(msg, argptr); printf("\n");
                // how do I do the same as above except with for example std::cout?
            #endif
        }

        /* Log to file */
        if (mLogToFile)
        {
                      // TODO

        }

        va_end(argptr);
    }

Upvotes: 3

Views: 843

Answers (1)

Mark B
Mark B

Reputation: 96301

Don't try to use a variadic wrapper for C++ streams, just use the corresponding C API such as vprintf/vnsprintf. Wrapping streams in this way just throws away all the benefits and causes additional complexity.

Why not have your wrapper API use streams, and map them to printf on Android platforms. That way you get all the benefits of streams and only lose them on platforms that don't natively support them.

Upvotes: 2

Related Questions