A collection of non-MFC program trace and assert macros
We know that the trace and assert macros come with the MFC framework program. But how can we implement these two Macros in a non-MFC program. Here is an implementation method. Implement these two Macros in the Debug. h file. The program can include this header file. The header file and description are as follows:
# Ifndef _ debug_h _
# DEFINE _ debug_h _
# Ifndef _ afx_h __
# DEFINE _ T (x) x
# If defined _ debug
# Include <stdio. h>/* vsprintf */
# Include <crtdbg. h>
# Define dprintf_buf_sz 1024
# Define assert (expr )/
Do {/
If (! (Expr) & (1 = _ crtdbgreport (_ crt_assert, _ file __, _ line __, null, # expr )))/
{__ ASM {INT 3 };}/
} While (0)
Static _ inline void trace (char * FMT ,...)
{
Va_list ARGs;
Char Buf [dprintf_buf_sz];
Va_start (ARGs, FMT );
Vsprintf (BUF, FMT, argS );
Outputdebugstring (BUF );
// File * fp = fopen ("D: // test.txt", "");
// Fwrite (BUF, strlen (BUF), 1, FP );
// Fclose (FP );
}
# Else
Static _ inline void trace (char * FMT ,...){}
# Define assert (expr) if (expr) {NULL ;}
# Endif
# Endif
# Endif/* _ debug_h _*/
A little explanation:
(1) {__ ASM {INT 3};} is a system interrupt command. A simple assembly statement is embedded in C.
(2) debug statements output to debuger can also be output to text files. For example: D: // test.txt
(3) va_start, va_arg, and va_end are variable parameters implemented by Win32 programs. The implementation in UNIX is slightly different, including the va_dcl macro. Va_list is defined as typedef char * va_list. For more information, see msnd.
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/stone_kingnet/archive/2008/11/01/3199551.aspx