IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Sep 3, 2004, 10:39:50 AM (22 years ago)
Author:
desonia
Message:

Added Error Stack logic. The new psError is named psError2 until all other code can be converted over to the new style.

Location:
trunk/psLib/src/sysUtils
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/psLib/src/sysUtils/psError.c

    r1407 r1685  
    1111 *  @author Eric Van Alst, MHPCC
    1212 *   
    13  *  @version $Revision: 1.10 $ $Name: not supported by cvs2svn $
    14  *  @date $Date: 2004-08-07 00:06:06 $
     13 *  @version $Revision: 1.11 $ $Name: not supported by cvs2svn $
     14 *  @date $Date: 2004-09-03 20:39:50 $
    1515 *
    1616 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
    1717 */
    1818
    19 /******************************************************************************/
     19#include <stdarg.h>
     20#include <pthread.h>
     21#include <string.h>
    2022
    21 /*  INCLUDE FILES                                                             */
    22 
    23 /******************************************************************************/
    24 #include <stdarg.h>
    2523#include "psLogMsg.h"
    2624#include "psError.h"
     25#include "psMemory.h"
    2726
    28 /******************************************************************************/
     27#define MAX_ERROR_STACK_SIZE 64
     28static psErr* errorStack[MAX_ERROR_STACK_SIZE];
     29static unsigned int errorStackSize = 0;
     30pthread_mutex_t lockErrorStack = PTHREAD_MUTEX_INITIALIZER;
    2931
    30 /*  DEFINE STATEMENTS                                                         */
    3132
    32 /******************************************************************************/
     33static void pushErrorStack(psErr* err);
    3334
    34 // None
     35static void pushErrorStack(psErr* err)
     36{
    3537
    36 /******************************************************************************/
     38    pthread_mutex_lock(&lockErrorStack);
    3739
    38 /*  TYPE DEFINITIONS                                                          */
     40    if (errorStackSize < MAX_ERROR_STACK_SIZE) {
     41        errorStack[errorStackSize] = psMemIncrRefCounter(err);
     42        errorStackSize++;
     43    }
    3944
    40 /******************************************************************************/
     45    pthread_mutex_unlock(&lockErrorStack);
     46}
    4147
    42 // None
     48static void errFree(psErr* err)
     49{
     50    if (err != NULL) {
     51        psFree(err->msg);
     52        psFree(err->name);
     53    }
     54}
    4355
    44 /*****************************************************************************/
     56psErr* psErrAlloc(const char* name, psErrorCode code, const char* msg)
     57{
     58    psErr* err = psAlloc(sizeof(psErr));
     59    err->msg = strcpy(psAlloc(strlen(msg) + 1), msg);
     60    err->name = strcpy(psAlloc(strlen(name) + 1), name);
     61    err->code = code;
    4562
    46 /*  GLOBAL VARIABLES                                                         */
     63    p_psMemSetDeallocator(err,(psFreeFcn)errFree);
    4764
    48 /*****************************************************************************/
    49 
    50 // None
    51 
    52 /*****************************************************************************/
    53 
    54 /*  FILE STATIC VARIABLES                                                    */
    55 
    56 /*****************************************************************************/
    57 
    58 // None
    59 
    60 /*****************************************************************************/
    61 
    62 /*  FUNCTION IMPLEMENTATIONS - LOCAL                                         */
    63 
    64 /*****************************************************************************/
    65 
    66 // None
    67 
    68 /*****************************************************************************/
    69 
    70 /* FUNCTION IMPLEMENTATIONS - PUBLIC                                         */
    71 
    72 /*****************************************************************************/
     65    return err;
     66}
    7367
    7468void psError(const char *name, const char *fmt, ...)
     
    8579    va_end(argPtr);
    8680}
     81
     82void psErrorClear()
     83{
     84    pthread_mutex_lock(&lockErrorStack);
     85    for (int lcv=0;lcv<errorStackSize;lcv++) {
     86        psFree(errorStack[lcv]);
     87    }
     88    errorStackSize = 0;
     89    pthread_mutex_unlock(&lockErrorStack);
     90}
     91
     92void psErrorMsg(const char *name, psErrorCode code, bool new, const char* fmt, ...)
     93{
     94    char errMsg[1024];
     95    psErr* err;
     96
     97    va_list argPtr;             // variable list arguement pointer
     98
     99    // Get the variable list parameters to pass to logging function
     100    va_start(argPtr, fmt);
     101
     102    // Call logging function with PS_LOG_ERROR level
     103    psLogMsgV(name, PS_LOG_ERROR, fmt, argPtr);
     104
     105    if (new) {
     106        psErrorClear();
     107    }
     108
     109    vsnprintf(errMsg,1024,fmt,argPtr);
     110    err = psErrAlloc(name,code,errMsg);
     111    pushErrorStack(err);
     112
     113    // Clean up stack after variable argument has been used
     114    va_end(argPtr);
     115
     116    psFree(err);
     117}
     118
     119const psErr* psErrorGet(int which)
     120{
     121    psErr* result;
     122
     123    pthread_mutex_lock(&lockErrorStack);
     124
     125    which = errorStackSize-1-which;     // the which input is from the end of errorStack
     126    if (which < 0 || which >= errorStackSize) {
     127        result = psErrAlloc("",PS_ERR_NONE,"");    // no error at the given location
     128    } else {
     129        result = psMemIncrRefCounter(errorStack[which]); // a new reference passed back
     130    }
     131
     132    pthread_mutex_unlock(&lockErrorStack);
     133
     134    return result;
     135}
     136
     137const psErr* psErrorLast(void)
     138{
     139    return psErrorGet(0);
     140}
     141
     142void psErrorStackPrint(FILE *fd, const char *fmt, ...)
     143{
     144    va_list argPtr;             // variable list arguement pointer
     145
     146    // Get the variable list parameters to pass to logging function
     147    va_start(argPtr, fmt);
     148
     149    psErrorStackPrintV(fd,fmt,argPtr);
     150
     151    va_end(argPtr);
     152}
     153
     154void psErrorStackPrintV(FILE *fd, const char *fmt, va_list va)
     155{
     156
     157    pthread_mutex_lock(&lockErrorStack);
     158
     159    if (errorStackSize > 0) {
     160        vfprintf(fd,fmt,va);
     161
     162        for (int lcv=0;lcv<errorStackSize;lcv++) {
     163            fprintf(fd," -> %s: %s\n     %s\n",
     164                    errorStack[lcv]->name,
     165                    psErrorCodeString(errorStack[lcv]->code),
     166                    errorStack[lcv]->msg);
     167        }
     168    }
     169
     170    pthread_mutex_unlock(&lockErrorStack);
     171}
     172
  • trunk/psLib/src/sysUtils/psError.h

    r1683 r1685  
    1212 *  @author Eric Van Alst, MHPCC
    1313 *
    14  *  @version $Revision: 1.9 $ $Name: not supported by cvs2svn $
    15  *  @date $Date: 2004-09-02 22:23:20 $
     14 *  @version $Revision: 1.10 $ $Name: not supported by cvs2svn $
     15 *  @date $Date: 2004-09-03 20:39:50 $
    1616 *
    1717 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    2222
    2323#include<stdio.h>
     24#include<stdbool.h>
    2425
    2526#include "psErrorCodes.h"
     
    2930 */
    3031
     32/** Error message object */
    3133typedef struct
    3234{
    33     char *name;                        ///< category of code that caused the error
     35    char* name;                        ///< category of code that caused the error
    3436    psErrorCode code;                  ///< class of error
    35     char *msg;                         ///< the message associated with the error
     37    char* msg;                         ///< the message associated with the error
    3638}
    3739psErr;
    3840
    39 const psErr *psErrorGet(int which);
    40 const psErr *psErrorLast(void);
     41/** Get a error from the error stack
     42 *
     43 *  Previous errors on the stack are returned by psErrorGet (a value of 0
     44 *  passed to psErrorGet is equivalent to a call to psErrorLast).
     45 *
     46 *  if no error is at the which position, a non-NULL psErr is returned with
     47 *  code PS_ERR_NONE.
     48 *
     49 *  @return    Error message object at 'which'
     50 */
     51const psErr* psErrorGet(
     52    int which                          ///< position in the error stack. 0 is last error on stack.
     53);
     54
     55/** Get last error put on the error stack
     56 *
     57 *  The last error reported is available from psErrorLast; if no errors are
     58 *  current, a non-NULL psErr is returned with code PS_ERR_NONE.
     59 *
     60 *  @return psErr*     Reference to last error message on error stack
     61 */
     62const psErr* psErrorLast(void);
     63
     64/** Clears the error stack.
     65 *
     66 *  The error stack may be completely cleared with psErrorClear.
     67 *
     68 */
    4169void psErrorClear(void);
    4270
    43 void psErrorStackPrint(FILE *fd, const char *fmt, ...);
    44 void psErrorStackPrintV(FILE *fd, const char *fmt, va_list va);
     71/** Prints error stack to specified open file descriptor
     72 *
     73 *  The entire error stack may be printed to an open file descriptor by
     74 *  calling psErrorStackPrint; if and only if there are current errors, the
     75 *  printf-style string fmt is first printed to the file descriptor fd. In
     76 *  this printout, error codes are replaced by their string equivalents.
     77 *
     78 */
     79void psErrorStackPrint(
     80    FILE* fd,                          ///< destination file descriptor
     81    const char* fmt,                   ///< printf-style format of header line
     82    ...                                ///< any parameters required in fmt
     83);
     84
     85/** Prints error stack to specified open file descriptor
     86 *
     87 *  The entire error stack may be printed to an open file descriptor by
     88 *  calling psErrorStackPrintV; if and only if there are current errors, the
     89 *  vprintf-style string fmt is first printed to the file descriptor fd. In
     90 *  this printout, error codes are replaced by their string equivalents.
     91 *
     92 */
     93void psErrorStackPrintV(
     94    FILE* fd,                          ///< destination file descriptor
     95    const char* fmt,                   ///< printf-style format of header line
     96    va_list va                         ///< any parameters required in fmt
     97);
    4598
    4699/** Reports an error message to the logging facility
     
    57110);
    58111
     112/** Reports an error message to the logging facility
     113 *
     114 *  This function will invoke the psLogMsg function with a level of
     115 *  PS_LOG_ERROR and pass the parameters name and fmt to generate a proper
     116 *  log message.
     117 *
     118 */
     119void psErrorMsg(
     120    const char *name,                  ///< Name of error in the form aaa.bbb.ccc
     121    psErrorCode code,                  ///< Error class code
     122    bool new,                          ///< true if error originates at this location
     123    const char* fmt,                   ///< printf style formatting statement defining error message
     124    ...
     125);
     126
     127/** Create a new psErr struct
     128 *
     129 *  Creates a new psErr struct, making a copy of the parameters.
     130 *
     131 *  @return psErr*     new psErr object
     132 */
     133psErr* psErrAlloc(
     134    const char* name,                  ///< Name of error in the form aaa.bbb.ccc
     135    psErrorCode code,                  ///< Error class code
     136    const char* msg                    ///< Error message
     137);
     138
    59139/* @} */// End of SysUtils Functions
    60140
Note: See TracChangeset for help on using the changeset viewer.