Below is a relatively simple below is a relatively simple TRACE and ASSERT's WIN32 implementation:
#define assert (expr) /
DO {/
IF (! (expr) && /
(1 == _CRTDBGREPORT (_CRT_ASSERT, __FILE___LINE__, NULL, #EXPR)) /
__ASM {INT 3}; /
} while (0)
#define TRACE (EXPR) /
_CRTDBGREPORT (_CRT_WARN, __FILE__, __LINE__, NULL, EXPR)
They call all the _crtdbgreport function, whose prototype is as follows:
_CRTIMP INT __CDECL _CRTDBGREPORT
Int nrpttype,
Const char * szfile,
Int nline,
Const char * szmodule,
Const char * szformat,
...
);
_CRTDBGREPORT's first parameter represents the type of report message, there are three types below:
#define _CRT_WARN 0
#define _CRT_ERROR 1
#define _CRT_ASSERT 2
The implementation of the _CRTDBGREPORT function is generally splicing the report message, then send the message to the appropriate place according to the type of message, as follows:
First, judge the value of _CRTDBGMODE [NRPTTYPE], by observation, know
_CRTDBGMODE [0] = 2;
_CRTDBGMODE [1] = 4;
_CRTDBGMODE [2] = 4;
The system defines the following macro to indicate where these report messages are sent.
#define _crtdbg_mode_file 0x1
#define _CRTDBG_MODE_DEBUG 0x2
#define _crtdbg_mode_wndw 0x4
Then, through the help of the source code and MSDN, finally clear the entry of the message:
If _CRTDBGMODE [NRPTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTDBG_MODE_DEBUG, then outputDebugstring calls the report message to the debugger.
If _CRTDBGMODE [NRPTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTDBG_MODE_WNDW, the dialog box with termination, retry, ignore three buttons is popped up.
Let us look back to see the Assert macro, obviously, if the expr is non-zero, askSERT successfully, exits the While loop directly, do not perform _CRTDBGREPORT; if the expr is 0, execute _CRTDBGREPORT, if the user selects termination, the application Termination immediately; if you choose to try again, _CRTDBGREPORT returns 1, the program will perform the following assembly statement, this statement will make the program interrupt, just set the breakpoint, if the user selects ignore, the program continues. By the way, # expr means converting the expr to a string, for example, if assert (0 == i), #expr is "0 == i".
If _CRTDBGMODE [NRPTTTTTTTTTTTTTTTTTTTTTTTTTDBG_MODE_FILE, the report information is written to the file represented by _CRTDBGFILE [NRPTTTYPE].
The non-Win32 implementation of the Trace macro is very simple, that is, call the printf direct output report message.