简体   繁体   中英

How to log stuff in console in Visual Studio C++

I'm working on a little C++-Game in Visual Studio 2008. I want to see the content of a vector after a couple of seconds or after I pressed some buttons. Breakpoints are useless in this case, because they stop me at every call of the gameloop (~60 times per second). How do I debug in this case?

Thanks!

Use function OutputDebugString from Windows API. You can call it anytime you want eg every 100th loop in your code.

Function info is here

Please read all comments on this page - some people claim that in your IDE (VS2008) output of this function is shown in "Immediate Window" not the "Output".

You can set conditional breakpoints, ie breakpoints which hit at a certain position only when a given expression is true. You can, for example, set a breakpoint which hits only every nth time in a loop.

you can use a simple output to the console.

say you want to display an integer, you can simply use printf for example:

printf("the number is %d \n", vectorArray.at(place) );

Setup an elapsed timer and something extremely basic.

if elapsedTime > 3 seconds: hits your break point, check out your vector

Or if you want to stop on a very specific point, just flag a counter to keep track of how many frames you've done.

Along with conditional breakpoints you can also have the breakpoint write the vector values to the console and not stop.

Right click on your breakpoint and select "When Hit" click "print a message" and then add your values to the message in curly braces. Use the "Hit Count" to have the breakpoint execute after so many cycles. The "Condition" option is also useful for setting the breakpoint dependent on a certain value in your variables.

You can also set your breakpoint inside a piece of conditional code, eg:

if(keyPressed('S'))
{
  int a = 42; // <-- set breakpoint here
}

The pro vs a conditional breakpoint is that the condition can be a bit more complex, the con is that each time you need to change the condition, you need to compile and link your app.

i found out that if you include and use fprintf(stdout,"")

it returns a command prompt on the screen while your program is still running

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM