IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
May 3, 2004, 4:25:41 PM (22 years ago)
Author:
rhl
Message:

Support for psErrorRegister()

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/archive/pslib/src/Utils/error.c

    r490 r567  
    1919#include "psErrorCodes.c"
    2020
    21 static char *errorStringList[PS_ERR_N_ERR_CLASSES]; // error strings indexed by errorCode
     21static int nerrorString = 0;            // number of known error messages
     22static char **errorStringList = NULL;   // error strings indexed by errorCode
    2223
    2324/************************************************************************************************************/
     
    139140
    140141/************************************************************************************************************/
    141 
    142 static void
    143 build_errorStringList(void)
    144 {
    145     for (int i = 0; i < PS_ERR_N_ERR_CLASSES; i++) {
    146         errorStringList[i] = NULL;
    147     }
    148 
    149     errorStringList[PS_ERR_NONE] = "No error";
    150     for (int i = 0; i < PS_ERR_N_ERR_CLASSES; i++) {
    151         if (errorStrings[i].descrip == NULL) {
    152             break;
    153         }
    154        
    155         errorStringList[errorStrings[i].code] = errorStrings[i].descrip;
     142/*
     143 * Register a set of psErrorDescription
     144 */
     145void p_psErrorRegister(const psErrorDescription *errors,       // errors to register
     146                       int nerror)                             // number of errors
     147{
     148    if (nerror <= 0) {                  // no new errors
     149        return;
     150    }
     151
     152    int codeMax = errors[nerror - 1].code; // largest new error code
     153
     154    if (codeMax > nerrorString) {
     155        errorStringList = psRealloc(errorStringList, (codeMax + 1)*sizeof(char *));
     156
     157        for (int i = nerrorString; i <= codeMax; i++) {
     158            errorStringList[i] = NULL;
     159        }
     160        nerrorString = codeMax + 1;
     161    }
     162
     163    for (int i = 0; i < nerror; i++) {
     164        if (errorStringList[errors[i].code] != NULL && // that value is already registered differently
     165            strcmp(errorStringList[errors[i].code], errors[i].descrip) != 0) {
     166            psLogMsg("utils.error", PS_LOG_WARN,
     167                     "Attempt to register error code %d as \"%s\" that is already registered as \"%s\"",
     168                     errors[i].code, errors[i].descrip, errorStringList[errors[i].code]);
     169        }
     170
     171        errorStringList[errors[i].code] = errors[i].descrip;
    156172    }
    157173}
     
    163179const char *psErrorCodeString(psErrorCode code)
    164180{
    165     if (errorStringList[PS_ERR_UNKNOWN] == NULL) { // need to build the errorStringList
    166         build_errorStringList();
    167     }
    168 
    169     return (code < PS_ERR_BASE ? strerror(code) : errorStringList[code]);
     181    if (errorStringList == NULL) {      // need to build the errorStringList
     182        psErrorRegister();
     183    }
     184
     185    if (code < PS_ERR_BASE) {
     186        return strerror(code);
     187    } else if (code >= nerrorString) {
     188        static char buff[30];
     189        sprintf(buff, "(Unknown code %d)", code);
     190        return buff;
     191    } else {
     192        return errorStringList[code];
     193    }
    170194}
    171195
     
    197221                        va_list ap)     // arguments for format
    198222{
    199     if (errorStringList[PS_ERR_UNKNOWN] == NULL) { // need to build the errorStringList
    200         build_errorStringList();
     223    if (errorStringList == NULL) {      // need to build the errorStringList
     224        psErrorRegister();
    201225    }
    202226
Note: See TracChangeset for help on using the changeset viewer.