Search code examples
c++windows-phone-8consolewindows-runtimevisual-studio-debugging

Debug C++ Runtime Component in a WP8 project


I've tried a lot of solutions, but none of them is working.

  • printf("Test : %d", 123);
  • std::cout << "Test" << 123 << std::endl;
  • ...

Actually I've setup my project's debugger like this, I'm able to write in the console using OutputDebugStringA("Test"); but this function doesn't accept more than one parameter.

How can I print something like this : ("Test : %d", 123)?


Solution

  • I've finally found this solution (here and here) :

    Debug.cpp:

    #pragma once
    
    #include "pch.h"
    #define BUFFER_LENGTH 1024
    
    //char* to wchar_t*
    const wchar_t *GetWC(const char *c)
    {
        const size_t cSize = strlen(c) + 1;
        wchar_t* wc = new wchar_t[cSize];
        mbstowcs(wc, c, cSize);
    
        return wc;
    }
    
    void Debug::Log(const char *format, ...)
    {
        char buffer[BUFFER_LENGTH];
        va_list args;
        va_start(args, format);
        vsnprintf(buffer, BUFFER_LENGTH, format, args);
        va_end(args);
        buffer[BUFFER_LENGTH - 1] = '\0'; //prevent buffer overflow
        OutputDebugString(GetWC(buffer));
    }
    

    Debug.h:

    class Debug{
    public:
        static void Log(const char *format, ...);
    };
    

    We can use it like printf function:

    Debug::Log("Test : %d", 123);