| 1 |
/* |
| 2 |
* Copyright(C) 1999-2025 National Technology & Engineering Solutions |
| 3 |
* of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with |
| 4 |
* NTESS, the U.S. Government retains certain rights in this software. |
| 5 |
* |
| 6 |
* See packages/seacas/LICENSE for details |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "exodusII.h" // for exoptval, MAX_ERR_LENGTH, etc |
| 10 |
#include "exodusII_int.h" |
| 11 |
|
| 12 |
#if defined(EXODUS_THREADSAFE) |
| 13 |
EX_errval_t *ex_errval = NULL; |
| 14 |
#define EX_PNAME ex_errval->last_pname |
| 15 |
#define EX_ERRMSG ex_errval->last_errmsg |
| 16 |
#define EX_ERR_NUM ex_errval->last_err_num |
| 17 |
#else |
| 18 |
int exerrval = 0; /* clear initial global error code value */ |
| 19 |
|
| 20 |
static char last_pname[MAX_ERR_LENGTH + 1]; |
| 21 |
static char last_errmsg[MAX_ERR_LENGTH + 1]; |
| 22 |
static int last_err_num; |
| 23 |
|
| 24 |
#define EX_PNAME last_pname |
| 25 |
#define EX_ERRMSG last_errmsg |
| 26 |
#define EX_ERR_NUM last_err_num |
| 27 |
#endif |
| 28 |
|
| 29 |
/*! |
| 30 |
\ingroup Utilities |
| 31 |
\undoc |
| 32 |
*/ |
| 33 |
void exi_reset_error_status(void) |
| 34 |
{ |
| 35 |
#if !defined(EXODUS_THREADSAFE) |
| 36 |
exerrval = 0; |
| 37 |
EX_ERR_NUM = 0; |
| 38 |
#endif |
| 39 |
} |
| 40 |
|
| 41 |
/*! |
| 42 |
\ingroup Utilities |
| 43 |
The function ex_err(exoid, ...) logs an error to stderr. It is intended |
| 44 |
to provide explanatory messages for error codes returned from other |
| 45 |
exodus routines. |
| 46 |
|
| 47 |
The passed in error codes and corresponding messages are listed in |
| 48 |
\file{exodusII.h}. The programmer may supplement the error message printed |
| 49 |
for standard errors by providing an error message. If the error code |
| 50 |
is provided with no error message, the predefined message will be |
| 51 |
used. The error code \c EX_MSG is available to log application |
| 52 |
specific messages. |
| 53 |
|
| 54 |
\param[in] module_name This is a string containing the name of the calling function. |
| 55 |
\param[in] message This is a string containing a message explaining the error |
| 56 |
or problem. If \c EX_VERBOSE (see ex_opts()) is true, |
| 57 |
this message will be printed to stderr. Otherwise, |
| 58 |
nothing will be printed. Maximum length is \c MAX_ERR_LENGTH. |
| 59 |
|
| 60 |
\param[in] err_num This is an integer code identifying the error. exodus C |
| 61 |
functions place an error code value in exerrval, |
| 62 |
an external integer. Negative values are considered |
| 63 |
fatal errors while positive values are warnings. |
| 64 |
There is a set of predefined values defined in |
| 65 |
\file{exodusII.h}. The predefined constant |
| 66 |
\c EX_PRTLASTMSG will cause the last error message |
| 67 |
to be output, regardless of the setting |
| 68 |
of the error reporting level (see ex_opts()). |
| 69 |
|
| 70 |
The following is an example of the use of this function: |
| 71 |
|
| 72 |
~~~{.c} |
| 73 |
int exoid, CPU_word_size, IO_word_size, errval; |
| 74 |
float version; |
| 75 |
char errmsg[MAX_ERR_LENGTH]; |
| 76 |
|
| 77 |
CPU_word_size = sizeof(float); |
| 78 |
IO_word_size = 0; |
| 79 |
|
| 80 |
\comment{open exodus file} |
| 81 |
if (exoid = ex_open ("test.exo", EX_READ, &CPU_word_size, |
| 82 |
&IO_word_size, &version)) { |
| 83 |
errval = 999; |
| 84 |
snprintf(errmsg, MAX_ERR_LENGTH,"ERROR: cannot open file test.exo"); |
| 85 |
ex_err(__func__, errmsg, errval); |
| 86 |
} |
| 87 |
~~~ |
| 88 |
|
| 89 |
*/ |
| 90 |
void ex_err(const char *module_name, const char *message, int err_num) |
| 91 |
{ |
| 92 |
EX_FUNC_ENTER_INT(); |
| 93 |
if (err_num == 0) { /* zero is no error, ignore and return */ |
| 94 |
exerrval = err_num; |
| 95 |
EX_FUNC_VOID(); |
| 96 |
} |
| 97 |
|
| 98 |
/* save the error message for replays */ |
| 99 |
if (message != NULL) { |
| 100 |
ex_copy_string(EX_ERRMSG, message, MAX_ERR_LENGTH + 1); |
| 101 |
} |
| 102 |
if (module_name != NULL) { |
| 103 |
ex_copy_string(EX_PNAME, module_name, MAX_ERR_LENGTH + 1); |
| 104 |
} |
| 105 |
|
| 106 |
if (err_num == EX_PRTLASTMSG) { |
| 107 |
fprintf(stderr, "\n[%s] %s\n", EX_PNAME, EX_ERRMSG); |
| 108 |
fprintf(stderr, " exerrval = %d\n", EX_ERR_NUM); |
| 109 |
fprintf(stderr, "\t%s\n", ex_strerror(EX_ERR_NUM)); |
| 110 |
EX_FUNC_VOID(); |
| 111 |
} |
| 112 |
|
| 113 |
if (err_num == EX_LASTERR) { |
| 114 |
err_num = EX_ERR_NUM; |
| 115 |
} |
| 116 |
else { |
| 117 |
exerrval = err_num; |
| 118 |
EX_ERR_NUM = err_num; |
| 119 |
} |
| 120 |
|
| 121 |
if (err_num == EX_NULLENTITY) { |
| 122 |
if (exoptval & EX_NULLVERBOSE) { |
| 123 |
fprintf(stderr, "\nExodus Library Warning: [%s]\n\t%s\n", module_name, message); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
else if (exoptval & EX_VERBOSE) { /* check see if we really want to hear this */ |
| 128 |
fprintf(stderr, "\nExodus Library Warning/Error: [%s]\n\t%s\n", module_name, message); |
| 129 |
fprintf(stderr, "\t%s\n", ex_strerror(err_num)); |
| 130 |
} |
| 131 |
fflush(stderr); |
| 132 |
|
| 133 |
/* with netCDF 3.4, (fatal) system error codes are > 0; |
| 134 |
so all EXODUS fatal error codes are > 0 */ |
| 135 |
if ((err_num > 0) && (exoptval & EX_ABORT)) { |
| 136 |
exit(err_num); |
| 137 |
} |
| 138 |
EX_FUNC_VOID(); |
| 139 |
} |
| 140 |
|
| 141 |
/*! |
| 142 |
\ingroup Utilities |
| 143 |
The function ex_err_fn() logs an error to stderr. It is intended |
| 144 |
to provide explanatory messages for error codes returned from other |
| 145 |
exodus routines. The main difference between ex_err_fn() and ex_err() is |
| 146 |
that ex_err_fn() will print the name of the exodus file that the error occurred on. |
| 147 |
|
| 148 |
The passed in error codes and corresponding messages are listed in |
| 149 |
\file{exodusII.h}. The programmer may supplement the error message printed |
| 150 |
for standard errors by providing an error message. If the error code |
| 151 |
is provided with no error message, the predefined message will be |
| 152 |
used. The error code \c EX_MSG is available to log application |
| 153 |
specific messages. |
| 154 |
|
| 155 |
\param[in] exoid exodus file ID returned from a previous call to ex_create() or ex_open(). |
| 156 |
\param[in] module_name This is a string containing the name of the calling function. |
| 157 |
\param[in] message This is a string containing a message explaining the error |
| 158 |
or problem. If \c EX_VERBOSE (see ex_opts()) is true, |
| 159 |
this message will be printed to stderr. Otherwise, |
| 160 |
nothing will be printed. Maximum length is \c MAX_ERR_LENGTH. |
| 161 |
|
| 162 |
\param[in] err_num This is an integer code identifying the error. exodus C |
| 163 |
functions place an error code value in exerrval, |
| 164 |
an external integer. Negative values are considered |
| 165 |
fatal errors while positive values are warnings. |
| 166 |
There is a set of predefined values defined in |
| 167 |
\file{exodusII.h}. The predefined constant |
| 168 |
\c EX_PRTLASTMSG will cause the last error message |
| 169 |
to be output, regardless of the setting |
| 170 |
of the error reporting level (see ex_opts()). |
| 171 |
|
| 172 |
The following is an example of the use of this function: |
| 173 |
|
| 174 |
~~~{.c} |
| 175 |
int exoid, CPU_word_size, IO_word_size, errval; |
| 176 |
float version; |
| 177 |
char errmsg[MAX_ERR_LENGTH]; |
| 178 |
|
| 179 |
CPU_word_size = sizeof(float); |
| 180 |
IO_word_size = 0; |
| 181 |
|
| 182 |
\comment{open exodus file} |
| 183 |
if (exoid = ex_open ("test.exo", EX_READ, &CPU_word_size, |
| 184 |
&IO_word_size, &version)) { |
| 185 |
errval = 999; |
| 186 |
snprintf(errmsg, MAX_ERR_LENGTH,"ERROR: cannot open file test.exo"); |
| 187 |
ex_err_fn(exoid, __func__, errmsg, errval); |
| 188 |
} |
| 189 |
~~~ |
| 190 |
|
| 191 |
*/ |
| 192 |
void ex_err_fn(int exoid, const char *module_name, const char *message, int err_num) |
| 193 |
{ |
| 194 |
EX_FUNC_ENTER_INT(); |
| 195 |
if (err_num == 0) { /* zero is no error, ignore and return */ |
| 196 |
exerrval = err_num; |
| 197 |
EX_FUNC_VOID(); |
| 198 |
} |
| 199 |
|
| 200 |
/* save the error message for replays */ |
| 201 |
if (message != NULL) { |
| 202 |
ex_copy_string(EX_ERRMSG, message, MAX_ERR_LENGTH + 1); |
| 203 |
} |
| 204 |
if (module_name != NULL) { |
| 205 |
ex_copy_string(EX_PNAME, module_name, MAX_ERR_LENGTH + 1); |
| 206 |
} |
| 207 |
|
| 208 |
if (err_num == EX_PRTLASTMSG) { |
| 209 |
fprintf(stderr, "\n[%s] %s\n", EX_PNAME, EX_ERRMSG); |
| 210 |
|
| 211 |
struct exi_file_item *file = exi_find_file_item(exoid); |
| 212 |
if (file) { |
| 213 |
size_t pathlen = 0; |
| 214 |
nc_inq_path(exoid, &pathlen, NULL); |
| 215 |
char *path = NULL; |
| 216 |
if (pathlen > 0) { |
| 217 |
path = malloc(pathlen + 1); |
| 218 |
if (path != NULL) { |
| 219 |
nc_inq_path(exoid, NULL, path); |
| 220 |
fprintf(stderr, " in file '%s'", path); |
| 221 |
free(path); |
| 222 |
} |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
fprintf(stderr, " exerrval = %d\n", EX_ERR_NUM); |
| 227 |
fprintf(stderr, "\t%s\n", ex_strerror(EX_ERR_NUM)); |
| 228 |
fflush(stderr); |
| 229 |
EX_FUNC_VOID(); |
| 230 |
} |
| 231 |
|
| 232 |
if (err_num == EX_LASTERR) { |
| 233 |
err_num = EX_ERR_NUM; |
| 234 |
} |
| 235 |
else { |
| 236 |
exerrval = err_num; |
| 237 |
EX_ERR_NUM = err_num; |
| 238 |
} |
| 239 |
|
| 240 |
if (err_num == EX_NULLENTITY) { |
| 241 |
if (exoptval & EX_NULLVERBOSE) { |
| 242 |
fprintf(stderr, "\nExodus Library Warning: [%s]\n\t%s\n", module_name, message); |
| 243 |
fflush(stderr); |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
else if (exoptval & EX_VERBOSE) { /* check see if we really want to hear this */ |
| 248 |
char *path = NULL; |
| 249 |
struct exi_file_item *file = exi_find_file_item(exoid); |
| 250 |
if (file) { |
| 251 |
size_t pathlen = 0; |
| 252 |
nc_inq_path(exoid, &pathlen, NULL); |
| 253 |
if (pathlen > 0) { |
| 254 |
path = malloc(pathlen + 1); |
| 255 |
nc_inq_path(exoid, NULL, path); |
| 256 |
} |
| 257 |
} |
| 258 |
if (path) { |
| 259 |
fprintf(stderr, "\nExodus Library Warning/Error: [%s] in file '%s'\n\t%s\n", module_name, |
| 260 |
path, message); |
| 261 |
free(path); |
| 262 |
} |
| 263 |
else { |
| 264 |
fprintf(stderr, "\nExodus Library Warning/Error: [%s]\n\t%s\n", module_name, message); |
| 265 |
} |
| 266 |
fprintf(stderr, "\t%s\n", ex_strerror(err_num)); |
| 267 |
fflush(stderr); |
| 268 |
} |
| 269 |
|
| 270 |
/* with netCDF 3.4, (fatal) system error codes are > 0; |
| 271 |
so all EXODUS fatal error codes are > 0 */ |
| 272 |
if ((err_num > 0) && (exoptval & EX_ABORT)) { |
| 273 |
exit(err_num); |
| 274 |
} |
| 275 |
EX_FUNC_VOID(); |
| 276 |
} |
| 277 |
|
| 278 |
/*! |
| 279 |
\ingroup Utilities |
| 280 |
\undoc |
| 281 |
*/ |
| 282 |
void ex_set_err(const char *module_name, const char *message, int err_num) |
| 283 |
{ |
| 284 |
EX_FUNC_ENTER_INT(); |
| 285 |
/* save the error message for replays */ |
| 286 |
ex_copy_string(EX_ERRMSG, message, MAX_ERR_LENGTH + 1); |
| 287 |
ex_copy_string(EX_PNAME, module_name, MAX_ERR_LENGTH + 1); |
| 288 |
if (err_num != EX_LASTERR) { |
| 289 |
/* Use last set error number, but add new function and message */ |
| 290 |
EX_ERR_NUM = err_num; |
| 291 |
} |
| 292 |
EX_FUNC_VOID(); |
| 293 |
} |
| 294 |
|
| 295 |
/*! |
| 296 |
\ingroup Utilities |
| 297 |
\undoc |
| 298 |
*/ |
| 299 |
void ex_get_err(const char **msg, const char **func, int *err_num) |
| 300 |
{ |
| 301 |
EX_FUNC_ENTER_INT(); |
| 302 |
if (msg) { |
| 303 |
(*msg) = EX_ERRMSG; |
| 304 |
} |
| 305 |
|
| 306 |
if (func) { |
| 307 |
(*func) = EX_PNAME; |
| 308 |
} |
| 309 |
|
| 310 |
if (err_num) { |
| 311 |
(*err_num) = EX_ERR_NUM; |
| 312 |
} |
| 313 |
EX_FUNC_VOID(); |
| 314 |
} |
| 315 |
|
| 316 |
/*! |
| 317 |
\ingroup Utilities |
| 318 |
\undoc |
| 319 |
Returns a pointer to a string which gives a text description of the error code err_num. |
| 320 |
If the error code refers to a NetCDF error, then that string is returned. |
| 321 |
|
| 322 |
~~~{.c} |
| 323 |
std::ostringstream errmsg; |
| 324 |
\comment{Create errmsg here so that the exerrval doesn't get cleared by} |
| 325 |
\comment{the ex_close call.} |
| 326 |
int status; |
| 327 |
ex_get_err(nullptr, nullptr, &status); |
| 328 |
fmt::print(errmsg, "Exodus error ({}) {} at line {} of file '{}' in function '{}'.", status, |
| 329 |
ex_strerror(status), lineno, filename, function); |
| 330 |
~~~ |
| 331 |
*/ |
| 332 |
const char *ex_strerror(int err_num) |
| 333 |
{ |
| 334 |
switch (err_num) { |
| 335 |
case EX_MEMFAIL: return "Memory allocation failure"; |
| 336 |
case EX_BADFILEMODE: return "Bad file mode -- cannot specify both EX_READ and EX_WRITE"; |
| 337 |
case EX_BADFILEID: return "Bad file id. Could not find exodus file associated with file id."; |
| 338 |
case EX_WRONGFILETYPE: |
| 339 |
return "File does not exist or is not of a supported type (netcdf3, netcdf4, netcdf5)."; |
| 340 |
case EX_LOOKUPFAIL: |
| 341 |
return "Id lookup failed for specified entity type. Could not find entity with specified id."; |
| 342 |
case EX_BADFILENAME: return "Empty or null filename specified."; |
| 343 |
case EX_DUPLICATEID: return "Duplicate entity id found."; |
| 344 |
case EX_DUPLICATEOPEN: return "File is open multiple times for both read and write."; |
| 345 |
case EX_BADPARAM: return "Bad parameter."; |
| 346 |
case -EX_BADPARAM: return "Bad parameter."; |
| 347 |
case EX_INTERNAL: return "Internal logic error in exodus library."; |
| 348 |
case EX_NOTROOTID: return "File id is not the root id; it is a subgroup id."; |
| 349 |
case EX_NULLENTITY: return "Null entity found."; |
| 350 |
case EX_NOTFOUND: return "Could not find requested variable on database."; |
| 351 |
case EX_INTSIZEMISMATCH: return "Integer sizes must match for input and output file in ex_copy."; |
| 352 |
case EX_MSG: return "Message printed; no error implied."; |
| 353 |
default: return nc_strerror(err_num); |
| 354 |
} |
| 355 |
} |