Listing 6: Custom source file for application-specific error simulation

//==================== DEFINE IF SIMULATING ERRORS =============
#ifdef SIMERR 
const char *SAMPLE_ERROR_LOG = "simerr.txt";
/*--------------------------------------------------------------
g_errorInfo    defines each error type and whether it throws
               an exception
---------------------------------------------------------------*/
static errorInfo_t g_errorInfo[] = 
{
{ SIMERR_RESULT_ERROR,       "RESULT",   SIMERR_TYPE_ERROR      },
{ SIMERR_MEMORY_ERROR,       "MEMORY",   SIMERR_TYPE_ERROR      },
{ SIMERR_DEVICE_EXCEPTION,   "DEVICE",   SIMERR_TYPE_EXCEPTION  },
{ SIMERR_DATABASE_EXCEPTION, "DATABASE", SIMERR_TYPE_EXCEPTION  }
};
/*--------------------------------------------------------------
PrintSimulatorMessage  Redirect messages from error simulator
--------------------------------------------------------------*/
static void PrintSimulatorMessage(const char *pMsg)
{
   printf("<SIMERR> %s\n", pMsg);
}
/*--------------------------------------------------------------
ThrowSimulatorException  Throw an exception corresponding to
                         an error type.
The simulator calls this function to simulate an
application-specific exception.
--------------------------------------------------------------*/
static void ThrowSimulatorException(int nErrorType,
                                    const char *pSourceFilename,
                                    int nSourceLine)
{
   switch(nErrorType)
   {
   case SIMERR_DEVICE_EXCEPTION:
      {
      CDeviceException *pException = new CDeviceException();
      throw(pException);
      break; 
      }
   case SIMERR_DATABASE_EXCEPTION:
      {
      CDatabaseException *pException = new CDatabaseException();
      throw(pException);
      break;
      }
   default:
      PrintSimulatorMessage("Invalid exception type");
   }
}
/*--------------------------------------------------------------
ValidateErrorTypes  Make sure error info array matches
                    ERROR_TYPE enum
--------------------------------------------------------------*/
static bool ValidateErrorTypes(void) 
{
   bool bOK = true;
   int nErrorTypes = sizeof(g_errorInfo) / sizeof(errorInfo_t);
   if (nErrorTypes == NUM_SIMERR_TYPES)
   {
      for (int i=0; bOK && (i < NUM_SIMERR_TYPES); ++i)
      {
         if (g_errorInfo[i].m_nErrorType != i)
         {
            bOK = false;
         }
      }
   }
   else
   {
      bOK = false;
   }
   if (!bOK)
   {
      PrintSimulatorMessage
         ("error type enum does not match g_errorInfo.");
   }
   return bOK;
}
/*--------------------------------------------------------------
SimErrSampleInit  Allocate and initialize the sample
                  error simulator.
--------------------------------------------------------------*/
bool SimErrCustomInit(int nSeed, SIMERR_TEST_TYPE testType) 
{
   bool bOK = false;
   // Make sure we didn't add an error type without
   // modifying g_errorInfo

bOK = ValidateErrorTypes();


   if (bOK)
   {
      // Fill in information that initializes the simulator
      simerrInit_t init;
      init.m_nVersion     = SIMERR_VERSION;  
      init.m_pFilename    = "errlog.txt"; 
      init.m_nSeed        = nSeed;     
      init.m_testType     = testType;  
      init.m_pfMsg        = PrintSimulatorMessage;     
      init.m_pfThrow      = ThrowSimulatorException;   
      init.m_pErrInfo     = g_errorInfo;  
      init.m_nErrTypes    = NUM_SIMERR_TYPES; 
      // Init simulator
      bOK = SimErrInit(&init);
   }
   if (!bOK)
   {
      PrintSimulatorMessage
         ("error simulator could not be initialized.");
   }
   return bOK;
}
#endif //SIMERR
//End of File