Following Flow and Execution
Sometimes you want to know the execution path that your application is taking, or certain things that are going on as it's executing, but you don't want to step through the application line-by-line. What you would really like to do is have a log of when the application reaches or passes certain points of code, and what the state of certain variables are. This is easy to do, adding your own code for writing logging messages to a file, but it's a pain to strip that code when you need to pass the application to someone else.
There is an easy way to accomplish this logging, without the pain of removing all of your logging messages. The way you should accomplish this is by using the TRACE macro. The TRACE macro sends any string message you pass to it to any debug output window you run on the computer the same time your application is running (see Figure 2.3). The syntax for this macro is as follows:
TRACE("This is my trace message.\n");
Figure 2.3 Trace messages from a debugging session.
The TRACE macro works just like the printf function in C, so you can pass it variables to be dynamically added to the message string just as you would with the printf function.
The output string from the TRACE macro is limited to 512 characters. If the formatted string you pass to the TRACE macro is longer than 512 characters (including the string terminating NULL character), it will trigger an ASSERT.Just as with the ASSERT macro, all uses of the TRACE macro are automatically removed from Release builds.
C++ Sidebar: Formatting Strings
The C programming language was the predecessor to the C++ programming language. C++ was created for the purpose of making an object-oriented version of C. You can still write standard C applications using any C++ compiler since part of the C++ standard is full support for the C programming language.
In the C programming language, there is a family of string formatting/printing functions. The primary function, printf, is used to format and print strings to the primary output device (this is for character-mode applications that would be running in a DOS window on a Windows system). There are two other variations to this function. The first, fprintf, allows you to send your string message to a file or output device. In fact, the printf version calls the fprintf function, specifying the standard output device as where to send the string. Finally, there is also the sprintf function, which formats the string and places it into a character array. You occasionally still see these functions used in C++ code from time to time.
The printf function takes a string as its primary parameter, which can be followed by any number of variables to be included in the string. Each variable must have a placeholder in the string. These placeholders are specified by the percent symbol (%) followed by a character indicating the data type of the value to place in that location. Table 2.2 lists the characters and their corresponding data types.
Table 2.2 printf Data Type Specifiers
Placeholders |
Data Type |
%c |
A single-byte character. |
%C |
A wide or UNICODE character. |
%d or %i |
Decimal integer data type. This includes integers and longs. |
%u |
Unsigned integer data type. |
%f |
Floating-point data types, including doubles and floats. |
%s |
Null-terminated string. |
%S |
Wide or UNICODE string. |
%x |
Hexadecimal value (lower-case). |
%X |
Hexadecimal value (upper-case). |
%o |
Octal value. |
%% |
Percent sign. Because the percent sign is used to indicate that the next character in the string is a placeholder for a variable value, you have to include two percent signs in order to include a percent sign in your string. |
If you need to format a number variable to be a specific number of digits, you can insert the number of digits between the percent and the placeholder character, as follows:
printf("This number: %5d is five digits wide.", iNbr);
For floating-point numbers, you can also specify the precision by adding a decimal point and the precision following the number specifying the width, as follows:
printf("I have %3.2f dollars in my wallet.", fMoney);
If the fMoney variable's value was 123.456, the output from the above function call would be:
I have 123.46 dollars in my wallet.
Normally, when you format numbers, they are printed aligned on the right side. This means that if the fMoney value was 1.234, the output would look like the following:
I have 1.23 dollars in my wallet.
If you need the number to be aligned on the left side, add a minus sign ([ms]) to the formatting expression:
printf("I have %-3.2f dollars in my wallet.", fMoney);
The resulting output would look like the following:
I have 1.23 dollars in my wallet.
If you want the output to include the minus sign if the value is negative, place the plus sign (+) just after the percent sign:
printf("I have %+3.2f dollars in my wallet.", fMoney);
This will cause a minus sign to be printed if the value of the fMoney variable is negative.
You also can include non-printable characters in the strings by including a back-slash followed by the character that represents the non-printable character. This can be used to include tabs, new-lines, carriage-returns, and so on. Table 2.3 lists the primary non-printable characters that you're likely to include.
Table 2.3 printf Non-Printable Character Specifiers
Placeholders |
Description |
\n |
New-line character. Use this to break a string into multiple lines of text. |
\t |
Tab character. |
\r |
Carriage-return character, causes the text to scroll back to the first of the string (but does not scroll to the next line). |
\\ |
Back-slash character. Because the back-slash character is used to indicate that the next character in the string is a placeholder for a non-printable character, you have to include two back-slash characters in order to include a single back-slash character in your string. |