12.12.2012 Views

Teach Yourself Borland C++ in 14 Days - portal

Teach Yourself Borland C++ in 14 Days - portal

Teach Yourself Borland C++ in 14 Days - portal

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Us<strong>in</strong>g the Debugger<br />

macros, TRACE and WARN, allow you to do exactly that. These macros are convenient debugg<strong>in</strong>g<br />

tools that many programmers overlook because of sparse documentation and general lack of<br />

discussion on the subject.<br />

When you use TRACE or WARN <strong>in</strong> your programs, the messages produced by these macros go<br />

to a log file called OutDbg1.txt. <strong>C++</strong>Builder automatically creates this text file and displays<br />

it <strong>in</strong> a Code Editor w<strong>in</strong>dow whenever a TRACE or WARN statement is encountered. You can then<br />

browse the log file to see what has happened <strong>in</strong> your program.<br />

NOTE<br />

NOTE<br />

The OutDbg1.txt file has a couple <strong>in</strong>terest<strong>in</strong>g characteristics. For<br />

one, the file is not considered part of the project. When you do a Save<br />

All, the OutDbg1.txt file is not saved as the rest of the files <strong>in</strong> the project<br />

are. Along those same l<strong>in</strong>es, when you close the OutDbg1.txt file, you<br />

are not prompted to save the file. If you want the file saved, you must<br />

specifically do a Save or Save All prior to clos<strong>in</strong>g it.<br />

This might seem a little odd, but it makes sense when you th<strong>in</strong>k about<br />

it. The log file is almost always used as a temporary debugg<strong>in</strong>g tool.<br />

Rarely do you need to save the contents of the log file, so by not<br />

prompt<strong>in</strong>g you to save the file, <strong>C++</strong>Builder saves you the aggravation of<br />

hav<strong>in</strong>g to deal with another message box.<br />

The TRACE macro simply outputs a l<strong>in</strong>e of text to the log file <strong>in</strong> the Code Editor. For example,<br />

you might have the follow<strong>in</strong>g code <strong>in</strong> your FormCreate() function:<br />

void __fastcall TMa<strong>in</strong>Form::FormCreate(TObject *Sender)<br />

{<br />

TRACE(“Enter<strong>in</strong>g FormCreate()”);<br />

// <strong>in</strong>tialization code<br />

TRACE(“Leav<strong>in</strong>g FormCreate()”);<br />

}<br />

When this code is executed, two l<strong>in</strong>es will be written to the OutDbg1.txt file. You can view<br />

the messages <strong>in</strong> the log file at any time—either dur<strong>in</strong>g program execution or after the program<br />

term<strong>in</strong>ates—simply by click<strong>in</strong>g on the OutDbg1.txt tab <strong>in</strong> the Code Editor.<br />

The diagnostic macros make use of <strong>C++</strong> streams (remember when we<br />

used those way back on Day 1, “Gett<strong>in</strong>g Your Feet Wet”?). This makes<br />

it possible to create log messages like the follow<strong>in</strong>g:<br />

TRACE(“Varible x = “

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!