Capture console output for debugging in VS?

后端 未结 6 470
说谎
说谎 2020-12-06 17:20

Under VS\'s external tools settings there is a \"Use Output Window\" check box that captures the tools command line output and dumps it to a VS tab.

The question is:

6条回答
  •  無奈伤痛
    2020-12-06 17:58

    I'm going to make a few assumptions here. First, I presume that you are talking about printf output from an application (whether it be from a console app or from a windows GUI app). My second assumption is the C language.

    To my knowledge, you cannot direct printf output to the output window in dev studio, not directly anyway. [emphasis added by OP]

    There might be a way but I'm not aware of it. One thing that you could do though would be to direct printf function calls to your own routine which will

    1. call printf and print the string
    2. call OuputDebugString() to print the string to the output window

    You could do several things to accomplish this goal. First would be to write your own printf function and then call printf and the OuputDebugString()

    void my_printf(const char *format, ...)
    {
        char buf[2048];
    
        // get the arg list and format it into a string
        va_start(arglist, format);
        vsprintf_s(buf, 2048, format, arglist);
        va_end(arglist); 
    
        vprintf_s(buf);            // prints to the standard output stream
        OutputDebugString(buf);    // prints to the output window
    }
    

    The code above is mostly untested, but it should get the concepts across.

    If you are not doing this in C/C++, then this method won't work for you. :-)

提交回复
热议问题