VC: a method for implementing non-MFC program trace and assert macros

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.