Jump to content
  • Advertisement
  • 07/23/02 09:28 PM
    Sign in to follow this  

    ASSERT(), VERIFY() and TRACE() for non-MFC Applications

    General and Gameplay Programming

    Myopic Rhino

    When it comes to game development under C++, few people choose to use MFC. Still, I find the ASSERT(), VERIFY() and TRACE() macros useful. So I thought to write my own versions that work for any kind of project for Windows platforms.

    A few reminders:

    • ASSERT() is supposed to evaluate its parameter, and if this is zero, to break the execution. In release mode, assert should expand to nothing.
    • VERIFY() is very similar to ASSERT(), except that in Release mode, it is supposed to expand to its parameter.
    • ASSERT() should be used with expressions that do not include any function call. For expressions that include a function call, you should use VERIFY(), so the function call is preserved in release mode.
    • TRACE() is the counterpart of printf(), except that it prints to the debug window. In Release mode, TRACE() also should expand to nothing.
    • None of the three macros imply any runtime penalty in release mode. The macros distinguish between debug and release mode by the pre-defined _DEBUG macro. This is specific to Microsoft Visual C++. If you are using some other compiler you might have to use some appropriate macro.

    Since it makes no sense to re-invent the wheel, I peeked into the MFC code and build my macros similarly. For ASSERT() and VERIFY() I left the fancy "Debug assertion failed..." dialog boxes aside, and simply implemented a plain vanilla breakpoint.

    There are two files needed to support ASSERT(), VERIFY and TRACE(): debug.h and debug.cpp. You should include debug.h in some main header of your project. It does not pollute recurrent inclusions, since it does not include any file itself. You also should add debug.cpp to the source files of your project.

    Here they are:

    // file debug.h
    #ifndef __DEBUG_H__
    #define __DEBUG_H__
    #ifdef _DEBUG
    	void _trace(char *fmt, ...);
    	#define ASSERT(x) {if(!(x)) _asm{int 0x03}}
    	#define VERIFY(x) {if(!(x)) _asm{int 0x03}}
    	#define ASSERT(x)
    	#define VERIFY(x) x
    #ifdef _DEBUG
    	#define TRACE _trace
    	inline void _trace(LPCTSTR fmt, ...) { }
    	#define TRACE 1 ? (void)0 : _trace
    #endif // __DEBUG_H__
    //file debug.cpp
    #ifdef _DEBUG
    	#include <windows.h>
    	#include <cstdarg>
    	#include <cstdio>
        void _trace(char *fmt, ...)
    		char out[1024];
    		va_list body;
    		va_start(body, fmt);
    		vsprintf(out, fmt, body);

      Report Article
    Sign in to follow this  

    User Feedback

    There are no comments to display.

    Create an account or sign in to comment

    You need to be a member in order to leave a comment

    Create an account

    Sign up for a new account in our community. It's easy!

    Register a new account

    Sign in

    Already have an account? Sign in here.

    Sign In Now

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!