Changeset 1685 for trunk/psLib/src/sysUtils/psError.c
- Timestamp:
- Sep 3, 2004, 10:39:50 AM (22 years ago)
- File:
-
- 1 edited
-
trunk/psLib/src/sysUtils/psError.c (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/src/sysUtils/psError.c
r1407 r1685 11 11 * @author Eric Van Alst, MHPCC 12 12 * 13 * @version $Revision: 1.1 0$ $Name: not supported by cvs2svn $14 * @date $Date: 2004-0 8-07 00:06:06$13 * @version $Revision: 1.11 $ $Name: not supported by cvs2svn $ 14 * @date $Date: 2004-09-03 20:39:50 $ 15 15 * 16 16 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii 17 17 */ 18 18 19 /******************************************************************************/ 19 #include <stdarg.h> 20 #include <pthread.h> 21 #include <string.h> 20 22 21 /* INCLUDE FILES */22 23 /******************************************************************************/24 #include <stdarg.h>25 23 #include "psLogMsg.h" 26 24 #include "psError.h" 25 #include "psMemory.h" 27 26 28 /******************************************************************************/ 27 #define MAX_ERROR_STACK_SIZE 64 28 static psErr* errorStack[MAX_ERROR_STACK_SIZE]; 29 static unsigned int errorStackSize = 0; 30 pthread_mutex_t lockErrorStack = PTHREAD_MUTEX_INITIALIZER; 29 31 30 /* DEFINE STATEMENTS */31 32 32 /******************************************************************************/ 33 static void pushErrorStack(psErr* err); 33 34 34 // None 35 static void pushErrorStack(psErr* err) 36 { 35 37 36 /******************************************************************************/ 38 pthread_mutex_lock(&lockErrorStack); 37 39 38 /* TYPE DEFINITIONS */ 40 if (errorStackSize < MAX_ERROR_STACK_SIZE) { 41 errorStack[errorStackSize] = psMemIncrRefCounter(err); 42 errorStackSize++; 43 } 39 44 40 /******************************************************************************/ 45 pthread_mutex_unlock(&lockErrorStack); 46 } 41 47 42 // None 48 static void errFree(psErr* err) 49 { 50 if (err != NULL) { 51 psFree(err->msg); 52 psFree(err->name); 53 } 54 } 43 55 44 /*****************************************************************************/ 56 psErr* 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; 45 62 46 /* GLOBAL VARIABLES */ 63 p_psMemSetDeallocator(err,(psFreeFcn)errFree); 47 64 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 } 73 67 74 68 void psError(const char *name, const char *fmt, ...) … … 85 79 va_end(argPtr); 86 80 } 81 82 void 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 92 void 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 119 const 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 137 const psErr* psErrorLast(void) 138 { 139 return psErrorGet(0); 140 } 141 142 void 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 154 void 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
Note:
See TracChangeset
for help on using the changeset viewer.
