| 1 |
/* |
| 2 |
* Copyright (C) 1996-2011 Daniel Waggoner |
| 3 |
* |
| 4 |
* This free software: you can redistribute it and/or modify |
| 5 |
* it under the terms of the GNU General Public License as published by |
| 6 |
* the Free Software Foundation, either version 3 of the License, or |
| 7 |
* (at your option) any later version. |
| 8 |
* |
| 9 |
* It is distributed in the hope that it will be useful, |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
* GNU General Public License for more details. |
| 13 |
* |
| 14 |
* If you did not received a copy of the GNU General Public License |
| 15 |
* with this software, see <http://www.gnu.org/licenses/>. |
| 16 |
*/ |
| 17 |
|
| 18 |
#include "dw_error.h" |
| 19 |
#include "dw_std.h" |
| 20 |
#include <stdlib.h> |
| 21 |
#include <string.h> |
| 22 |
|
| 23 |
#define ERROR_MESSAGE_BUFFER_LENGTH 256 |
| 24 |
static int ERROR_FLAG=NO_ERR; |
| 25 |
static char ERROR_MESSAGE[ERROR_MESSAGE_BUFFER_LENGTH]=""; |
| 26 |
static int TerminalErrors=ALL_ERRORS; |
| 27 |
static int VerboseErrors=ALL_ERRORS; |
| 28 |
static FILE* f_err=(FILE*)NULL; |
| 29 |
static char* filename_err=(char*)NULL; |
| 30 |
|
| 31 |
/* |
| 32 |
Returns the value of the current error flag. |
| 33 |
*/ |
| 34 |
int dw_GetError(void) |
| 35 |
{ |
| 36 |
return ERROR_FLAG; |
| 37 |
} |
| 38 |
|
| 39 |
/* |
| 40 |
Returns pointer to current error message. This buffer should not be modified |
| 41 |
or freed. |
| 42 |
*/ |
| 43 |
char* dw_GetErrorMessage(void) |
| 44 |
{ |
| 45 |
return ERROR_MESSAGE; |
| 46 |
} |
| 47 |
|
| 48 |
/* |
| 49 |
Clears the error flag and returns the value of the previous flag. This is the |
| 50 |
most efficient way to clear the error flag and message. |
| 51 |
*/ |
| 52 |
int dw_ClearError(void) |
| 53 |
{ |
| 54 |
int rtrn=ERROR_FLAG; |
| 55 |
ERROR_FLAG=NO_ERR; |
| 56 |
ERROR_MESSAGE[0]='\0'; |
| 57 |
return rtrn; |
| 58 |
} |
| 59 |
|
| 60 |
/* |
| 61 |
Sets the error flag to err, the error message to msg and returns the value of |
| 62 |
the previous flag. If the null terminated string msg is longer than 255 |
| 63 |
characters, only the first 255 characters are used. If msg is null, then a |
| 64 |
predefined error message is used. |
| 65 |
*/ |
| 66 |
int dw_SetError(int err, char *msg) |
| 67 |
{ |
| 68 |
int rtrn=ERROR_FLAG; |
| 69 |
if (msg) |
| 70 |
switch (ERROR_FLAG=err) |
| 71 |
{ |
| 72 |
case MEM_ERR: |
| 73 |
case FILE_ERR: |
| 74 |
case FILE_OPEN_ERR: |
| 75 |
case FILE_READ_WRITE_ERR: |
| 76 |
case PARSE_ERR: |
| 77 |
case FLOAT_ERR: |
| 78 |
case NULL_ERR: |
| 79 |
case ARG_ERR: |
| 80 |
case ITERATION_ERR: |
| 81 |
case NOT_IMPLEMENTED_ERR: |
| 82 |
case SIZE_ERR: |
| 83 |
case SING_ERR: |
| 84 |
case POSDEF_ERR: |
| 85 |
case BLAS_LAPACK_ERR: |
| 86 |
case USER_ERR: |
| 87 |
strncpy(ERROR_MESSAGE,msg,ERROR_MESSAGE_BUFFER_LENGTH-1); |
| 88 |
ERROR_MESSAGE[ERROR_MESSAGE_BUFFER_LENGTH-1]='\0'; |
| 89 |
break; |
| 90 |
case NO_ERR: |
| 91 |
ERROR_MESSAGE[0]='\0'; |
| 92 |
break; |
| 93 |
default: |
| 94 |
ERROR_FLAG=UNKNOWN_ERR; |
| 95 |
strcpy(ERROR_MESSAGE,"Unknown error."); |
| 96 |
break; |
| 97 |
} |
| 98 |
else |
| 99 |
switch (ERROR_FLAG=err) |
| 100 |
{ |
| 101 |
case MEM_ERR: |
| 102 |
strcpy(ERROR_MESSAGE,"Out of memory."); |
| 103 |
break; |
| 104 |
case FILE_ERR: |
| 105 |
strcpy(ERROR_MESSAGE,"File operation error."); |
| 106 |
break; |
| 107 |
case FILE_OPEN_ERR: |
| 108 |
strcpy(ERROR_MESSAGE,"Unable to open/create file."); |
| 109 |
break; |
| 110 |
case FILE_READ_WRITE_ERR: |
| 111 |
strcpy(ERROR_MESSAGE,"Unable to read/write to file."); |
| 112 |
break; |
| 113 |
case PARSE_ERR: |
| 114 |
strcpy(ERROR_MESSAGE,"Error parsing data."); |
| 115 |
break; |
| 116 |
case FLOAT_ERR: |
| 117 |
strcpy(ERROR_MESSAGE,"Floating point error."); |
| 118 |
break; |
| 119 |
case NULL_ERR: |
| 120 |
strcpy(ERROR_MESSAGE,"Unexpected null pointer encountered."); |
| 121 |
break; |
| 122 |
case ARG_ERR: |
| 123 |
strcpy(ERROR_MESSAGE,"Argument error."); |
| 124 |
break; |
| 125 |
case ITERATION_ERR: |
| 126 |
strcpy(ERROR_MESSAGE,"Maximum iteration limit exceeded."); |
| 127 |
break; |
| 128 |
case NOT_IMPLEMENTED_ERR: |
| 129 |
strcpy(ERROR_MESSAGE,"Feature not yet implemented."); |
| 130 |
break; |
| 131 |
case SIZE_ERR: |
| 132 |
strcpy(ERROR_MESSAGE,"Matrices/vectors not conformable."); |
| 133 |
break; |
| 134 |
case SING_ERR: |
| 135 |
strcpy(ERROR_MESSAGE,"Singular matrix."); |
| 136 |
break; |
| 137 |
case POSDEF_ERR: |
| 138 |
strcpy(ERROR_MESSAGE,"Matrix not positive definite."); |
| 139 |
break; |
| 140 |
case BLAS_LAPACK_ERR: |
| 141 |
strcpy(ERROR_MESSAGE,"Blas/Lapack error."); |
| 142 |
break; |
| 143 |
case USER_ERR: |
| 144 |
strcpy(ERROR_MESSAGE,"Undocumented error."); |
| 145 |
break; |
| 146 |
case NO_ERR: |
| 147 |
ERROR_MESSAGE[0]='\0'; |
| 148 |
break; |
| 149 |
default: |
| 150 |
ERROR_FLAG=UNKNOWN_ERR; |
| 151 |
strcpy(ERROR_MESSAGE,"Unknown error."); |
| 152 |
break; |
| 153 |
} |
| 154 |
if (VerboseErrors & ERROR_FLAG) |
| 155 |
{ |
| 156 |
if (f_err) |
| 157 |
fprintf(f_err,"%s\n",ERROR_MESSAGE); |
| 158 |
else |
| 159 |
printf("%s\n",ERROR_MESSAGE); |
| 160 |
} |
| 161 |
if (TerminalErrors & ERROR_FLAG) dw_exit(ERROR_FLAG); |
| 162 |
return rtrn; |
| 163 |
} |
| 164 |
|
| 165 |
/* |
| 166 |
Sets the error flag and to err, sets the error message to the predefined error |
| 167 |
message, and returns the value of the previous error flag. |
| 168 |
*/ |
| 169 |
int dw_Error(int err) |
| 170 |
{ |
| 171 |
return dw_SetError(err,(char*)NULL); |
| 172 |
} |
| 173 |
|
| 174 |
/* |
| 175 |
Sets the error flag and to USER_ERR, sets the error message to msg, and |
| 176 |
returns the value of the previous error flag. |
| 177 |
*/ |
| 178 |
int dw_UserError(char *msg) |
| 179 |
{ |
| 180 |
return dw_SetError(USER_ERR,msg); |
| 181 |
} |
| 182 |
|
| 183 |
/* |
| 184 |
Sets the error flag error message using the passed filename. Returns the |
| 185 |
value of the previous error flag. |
| 186 |
*/ |
| 187 |
int dw_FileError(int err, char *filename) |
| 188 |
{ |
| 189 |
char *msg, *fmt; |
| 190 |
if (filename) |
| 191 |
{ |
| 192 |
switch(err) |
| 193 |
{ |
| 194 |
case FILE_ERR: |
| 195 |
fmt="File operation error on %s"; break; |
| 196 |
case FILE_OPEN_ERR: |
| 197 |
fmt="Unable to open/create %s"; break; |
| 198 |
case FILE_READ_WRITE_ERR: |
| 199 |
fmt="Unable to read/write %s"; break; |
| 200 |
default: |
| 201 |
return dw_SetError(UNKNOWN_ERR,(char*)NULL); |
| 202 |
} |
| 203 |
sprintf(msg=dw_malloc(strlen(fmt)+strlen(filename)-1),fmt,filename); |
| 204 |
err=dw_SetError(err,msg); |
| 205 |
dw_free(msg); |
| 206 |
return err; |
| 207 |
} |
| 208 |
else |
| 209 |
switch(err) |
| 210 |
{ |
| 211 |
case FILE_ERR: |
| 212 |
case FILE_OPEN_ERR: |
| 213 |
case FILE_READ_WRITE_ERR: |
| 214 |
return dw_SetError(err,(char*)NULL); |
| 215 |
default: |
| 216 |
return dw_SetError(UNKNOWN_ERR,(char*)NULL); |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
/* |
| 221 |
Sets errors which terminate program. The integer err should be a combination |
| 222 |
of the error flags defined in dw_error.h. |
| 223 |
*/ |
| 224 |
int dw_SetTerminalErrors(int err) |
| 225 |
{ |
| 226 |
int rtrn=TerminalErrors; |
| 227 |
TerminalErrors=err & ALL_ERRORS; |
| 228 |
return rtrn; |
| 229 |
} |
| 230 |
|
| 231 |
/* |
| 232 |
Returns the current terminal errors. |
| 233 |
*/ |
| 234 |
int dw_GetTerminalErrors(void) |
| 235 |
{ |
| 236 |
return TerminalErrors; |
| 237 |
} |
| 238 |
|
| 239 |
/* |
| 240 |
Sets errors which causes program to print a error message to f_err. The |
| 241 |
integer err should be a combination of the error flags defined in dw_error.h. |
| 242 |
*/ |
| 243 |
int dw_SetVerboseErrors(int err) |
| 244 |
{ |
| 245 |
int rtrn=VerboseErrors; |
| 246 |
VerboseErrors=err & ALL_ERRORS; |
| 247 |
return rtrn; |
| 248 |
} |
| 249 |
|
| 250 |
/* |
| 251 |
Returns the current verbose errors. |
| 252 |
*/ |
| 253 |
int dw_GetVerboseErrors(void) |
| 254 |
{ |
| 255 |
return VerboseErrors; |
| 256 |
} |
| 257 |
|
| 258 |
/* |
| 259 |
Creates file for writing error messages. Returns one upon success and zero |
| 260 |
upon failure. Upon failure, error messages will be sent to the console. |
| 261 |
*/ |
| 262 |
int dw_CreateErrorMessageFile(char *filename) |
| 263 |
{ |
| 264 |
if (!filename) |
| 265 |
dw_ConsoleErrorMessage(); |
| 266 |
else |
| 267 |
{ |
| 268 |
if (f_err) fclose(f_err); |
| 269 |
if (filename_err) dw_free(filename_err); |
| 270 |
if ((f_err=fopen(filename,"wt"))) |
| 271 |
if ((filename_err=(char*)malloc(strlen(filename)+1))) |
| 272 |
{ |
| 273 |
strcpy(filename_err,filename); |
| 274 |
return 1; |
| 275 |
} |
| 276 |
else |
| 277 |
{ |
| 278 |
dw_Error(MEM_ERR); |
| 279 |
dw_ConsoleErrorMessage(); |
| 280 |
} |
| 281 |
else |
| 282 |
{ |
| 283 |
filename_err=(char*)NULL; |
| 284 |
dw_FileError(FILE_OPEN_ERR,filename); |
| 285 |
} |
| 286 |
} |
| 287 |
return 0; |
| 288 |
} |
| 289 |
|
| 290 |
/* |
| 291 |
Opens file for appending error messages. Returns one upon success and zero |
| 292 |
upon failure. Upon failure, error messages will be sent to the console. |
| 293 |
*/ |
| 294 |
int dw_AppendErrorMessageFile(char *filename) |
| 295 |
{ |
| 296 |
if (!filename) |
| 297 |
dw_ConsoleErrorMessage(); |
| 298 |
else |
| 299 |
{ |
| 300 |
if (f_err) fclose(f_err); |
| 301 |
if (filename_err) dw_free(filename_err); |
| 302 |
if ((f_err=fopen(filename,"at"))) |
| 303 |
if ((filename_err=(char*)malloc(strlen(filename)+1))) |
| 304 |
{ |
| 305 |
strcpy(filename_err,filename); |
| 306 |
return 1; |
| 307 |
} |
| 308 |
else |
| 309 |
{ |
| 310 |
dw_Error(MEM_ERR); |
| 311 |
dw_ConsoleErrorMessage(); |
| 312 |
} |
| 313 |
else |
| 314 |
{ |
| 315 |
filename_err=(char*)NULL; |
| 316 |
dw_FileError(FILE_OPEN_ERR,filename); |
| 317 |
} |
| 318 |
} |
| 319 |
return 0; |
| 320 |
} |
| 321 |
|
| 322 |
/* |
| 323 |
Sends error messages to the console (stdout). |
| 324 |
*/ |
| 325 |
void dw_ConsoleErrorMessage(void) |
| 326 |
{ |
| 327 |
if (f_err) fclose(f_err); |
| 328 |
if (filename_err) dw_free(filename_err); |
| 329 |
f_err=(FILE*)NULL; |
| 330 |
filename_err=(char*)NULL; |
| 331 |
} |
| 332 |
|
| 333 |
/* |
| 334 |
Get current error file pointer. This allows the user to: |
| 335 |
1) Print an error message without changing the current error number. |
| 336 |
2) Not have to construct a complex error message string. |
| 337 |
The returned file pointer must not be closed. Because this circumvents the |
| 338 |
error handling framework, it should be used with care. |
| 339 |
*/ |
| 340 |
FILE* dw_GetErrorFilePointer(void) |
| 341 |
{ |
| 342 |
return f_err ? f_err : stdout; |
| 343 |
} |
| 344 |
|
| 345 |
/* |
| 346 |
Get current error filename. A null return indicates that error messages are |
| 347 |
sent to the console (stdout). The returned string must not be modified in any |
| 348 |
way. This function is useful if the user wants to |
| 349 |
1) Determine where error messages are currently being sent. |
| 350 |
2) Change the current error file via a dw_CreateErrorMessage(), |
| 351 |
dw_AppendErrorMessage(), or dw_ConsoleErrorMessage() call, and then |
| 352 |
reset it via a dw_AppendErrorMessageFile() or dw_ConsoleErrorMessage() |
| 353 |
call. |
| 354 |
*/ |
| 355 |
char* dw_GetErrorFilename(void) |
| 356 |
{ |
| 357 |
return filename_err; |
| 358 |
} |