Assert/Check/Validate Macros

Apple provides a series of error handling and assertion macros in AssertMacros.h

From AssertMacros.h:

/*
 *  Macro overview:
 *  
 *      check(assertion)
 *         In production builds, pre-processed away  
 *         In debug builds, if assertion evaluates to false, calls DEBUG_ASSERT_MESSAGE
 *  
 *      verify(assertion)
 *         In production builds, evaluates assertion and does nothing
 *         In debug builds, if assertion evaluates to false, calls DEBUG_ASSERT_MESSAGE
 *  
 *      require(assertion, exceptionLabel)
 *         In production builds, if the assertion expression evaluates to false, goto exceptionLabel
 *         In debug builds, if the assertion expression evaluates to false, calls DEBUG_ASSERT_MESSAGE
 *                          and jumps to exceptionLabel
 *  
 *      In addition the following suffixes are available:
 * 
 *         _noerr     Adds "!= 0" to assertion.  Useful for asserting and OSStatus or OSErr is noErr (zero)
 *         _action    Adds statement to be executued if assertion fails
 *         _quiet     Suppress call to DEBUG_ASSERT_MESSAGE
 *         _string    Allows you to add explanitory message to DEBUG_ASSERT_MESSAGE
 *
 *        For instance, require_noerr_string(resultCode, label, msg) will do nothing if 
 *        resultCode is zero, otherwise it will call DEBUG_ASSERT_MESSAGE with msg
 *        and jump to label.
 *  Configuration:
 *
 *      By default all macros generate "production code" (i.e non-debug).  If  
 *      DEBUG_ASSERT_PRODUCTION_CODE is defined to zero or DEBUG is defined to non-zero
 *      while this header is included, the macros will generated debug code.
 *
 *      If DEBUG_ASSERT_COMPONENT_NAME_STRING is defined, all debug messages will
 *      be prefixed with it.
 *
 *      By default, all messages write to stderr.  If you would like to write a custom
 *      error message formater, defined DEBUG_ASSERT_MESSAGE to your function name.
 *
 */

In debug builds assert/verify routines print out what file/line had the error, and the value of the error, and are "compiled away" if the preprocessor symbol DEBUG = 0

Some of the more useful routines in this file are:

Output Example (DEBUG=1):

[Debug] 11:13:50: AssertMacros: Third Party Client: err == 0    file: /Users/me/Development/Project/Source/file.cpp, line: 66 (value 0xffffffce)

The error code given is a signed hex error code. Below is a simple Python script to convert this value into a human readable error code (-50):

#!/usr/bin/env python
# encoding: utf-8
"""
assertmacroerrs2err.py

Created by Ryan Wilcox, of Wilcox Development Solutions, on 2007-06-18.
Released into Public Domain on this date
"""

import sys

def main(argv=None):
    if argv is None:
        argv = sys.argv
    
    verifyNoErrStr = argv[1]
    
    ourStr = verifyNoErrStr
    asNum = int(ourStr, 16)
    maxErr = int("100000000", 16)
    print "-%d" % (maxErr - asNum)


if __name__ == "__main__":
    sys.exit(main())

Use the script as follows:

$ python assertmacroerrs2err.py 0xffffffce