Monday, December 29, 2008

[VC++] OutputDebugString for error/infomation tracing

Have you thought of how to make your application informing the error/information with out annoying the user (say through MessageBox()). For example to get the performance of a particular operation or if some irrelevant error/information which can be ignored from the user is to be logged/traced then we could not prefer MessageBox or so. In such a context OutputDebugString() is an API of interest.It sends a string to the debugger for display. If the application has no debugger, the system debugger displays the string. If the application has no debugger and the system debugger is not active, OutputDebugString() does nothing. Also note that applications should send very minimal debug output and provide a way for the user to enable or disable its use. Below example gives a way to get the performance of an operation and trace the performance obtained.

Code

CString csMsg;DWORD dwTime = GetTickCount();
// Do some operations here for demo Sleep() is used
Sleep(1000);
dwTime = GetTickCount() - dwTime;
csMsg.Format(" #PERF# Time consumed for the operation is %d ms", dwTime);
OutputDebugString( csMsg );

Output

#PERF# Time consumed for the operation is 1002 ms

The output can be obtained in the DebugView or will be available in the Visual Studio debugger output window (only while debugging). DebugView is an application that let you monitor debug output on your local system, or any computer on the network that you can reach via TCP/IP. There are numerous features available with Debugview, one example is filtering the debug view output. If you want only one type of output then you can filter specifying the string. For example specifying #PERF# in Edit->Filter/Highlight->Include and execute then you can find only strings with #PERF# logged in to the DebugView. Just check and find out the numerous options available with DebugView.

No comments: