|
#include <stdio.h>
#include <string.h> /* for strlen(), strcpy(), and strcmp() */
#include <errno.h>
#include "stdAfx.h"
/**
@file
This file contains the implementation of the rtAssert() function, which is called
by the #rtASSERT macro to evaluate an expression and (possibly) report an error.
Typical rtASSERT output:
@code
rtASSERT: 08/11/2006 14:31:06
'DataLen >= (cMaxAlarms * cMaxAlarmThresh * SHORTSIZE' is 0 in OddBall.cpp, line 96 (in myFunc())
@endcode
The actual text given to the rtASSERT macro is displayed.
<B>DANGER, Will Robinson, DANGER!</B>
Do NOT put anything in the #rtASSERT clause that you need to be executed
if there is even the slightest chance that rtASSERTs will be disabled.
For instance, this might seem a good thing:
@code
if(rtASSERT(!myStrangeFunction(x,y,z)))
{
// We reported the error, now return gracefully
return(ERROR);
}
@endcode
The problem is that if rtASSERTs are disabled, then the call to
myStrangeFunction() will not happen at all! A better way is to do:
@code
rc = myStrangeFunction(x,y,z);
if(rtASSERT(!rc))
{
// We reported the error, now return gracefully
return(rc); // return the original error code
}
@endcode
This will always call myStrangeFunction() even if rtASSERT is disabled.
<HR>
**********************************************************************/
/**
VerboseAssert can be used by the if(rtASSERT(expr)) clause as well as the
rtAssert() function.
- If VerboseAssert is at least 1, then the basic assert message is printed
- If VerboseAssert is at least 2, then a stacktrace() is also run.
- If VerboseAssert is at least 3, then the errno is printed (might not apply)
Other levels could be used for special debugging cases. The default is 3,
which gives both the basic assert as well as a stack trace and errno.
***************************************************************************/
//lint -esym(843,VerboseAssert) could be made static
//lint -esym(765,VerboseAssert) could be declared as const
int VerboseAssert = 3;
/**
base function invoked by the #rtASSERT macro
@param expr String containing the expression being evaluated, usually via the C-Preprocessor
trick of putting a '#' in front of the argument
@param exprValue Value of the expression, 0 means the assertion has failed
@param file Name of the file, usually via __FILE__
@param line Line number in the file, usually via __LINE__
@param func Name of the function called from, usually via __FUNCTION__
@retval 1 if the assertion failed, 0 otherwise
Side effects are controlled via the VerboseAssert variable but generally include a simple
message on the console (or however you implement this).
************************************************************************************************************/
//lint -esym(765,rtAssert) could be made static
int rtAssert(const char *expr, int exprValue, const char *file, int line, const char *func)
{
if(exprValue)
{
// save the most recent value. In some systems, errno is actually a macro to get thread-specific values.
//const int savErrno = errno; //lint !e746 call to function '__errno()' not made in the presence of a prototype
// If VerboseAssert is not set, then output is suppressed. Not recommended.
// Increasing values of VerboseAssert cause increasing amount of output.
if(VerboseAssert > 0)
{
/*********************************************************************************
* Display the test that failed and where it happened.
*********************************************************************************/
/*lint -e{746} call to function 'printf()' not made in the presence of a prototype */
printf("rtASSERT: '%s' is 0 in %s, line %d (in %s())\n", expr, file, line, func);
}
if(VerboseAssert > 1)
{
/* Display the stack trace to give a clue how we got in this situation */
}
if(VerboseAssert > 2)
{
/* Display any system error codes, which may or may not be relevent at this time */
}
}
return(exprValue);
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.