1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
|
/* error.c -- Error reporting routines for Khepera
* Created: Wed Dec 21 12:55:00 1994 by faith@dict.org
* Copyright 1994-1997, 2002 Rickard E. Faith (faith@dict.org)
* Copyright 2002-2008 Aleksey Cheusov (vle@gmx.net)
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* \section{Error Reporting Routines}
*
* \intro Several error reporting routines are provided. These routines
* are always used to print errors generated within the \khepera library,
* and are available for the programmer as well.
*
*/
#include "maaP.h"
#include <errno.h>
const char *_err_programName;
/* \doc |err_set_program_name| records the value of |argv[0]| for the
calling program. If this value is not "NULL", then it will be used when
printing errors and warnings. */
void err_set_program_name(const char *programName)
{
if (!programName){
_err_programName = programName;
return;
}
const char *pt =strrchr(programName, '/');
if (pt)
_err_programName = pt + 1;
else
_err_programName = programName;
}
/* \doc |err_program_name| returns the value of |programName| that was
previously set with |err_set_program_name|. This value may be
"NULL". */
const char *err_program_name(void)
{
return _err_programName;
}
/* \doc |err_fatal| flushes "stdout", prints a fatal error report on
"stderr", flushes "stderr" and "stdout", and calls |exit|. |routine| is
the name of the routine in which the error took place. */
void err_fatal(const char *routine, const char *format, ...)
{
va_list ap;
va_list ap_copy;
fflush(stdout);
if (_err_programName) {
if (routine)
fprintf(stderr, "%s (%s): ", _err_programName, routine);
else
fprintf(stderr, "%s: ", _err_programName);
} else {
if (routine) fprintf(stderr, "%s: ", routine);
}
va_start(ap, format);
va_copy(ap_copy, ap);
vfprintf(stderr, format, ap);
fprintf(stderr, "\n");
log_error_va(routine, format, ap_copy);
va_end(ap);
fflush(stderr);
fflush(stdout);
exit (1);
}
/* \doc |err_fatal_errno| flushes "stdout", prints a fatal error report on
"stderr", prints the system error corresponding to |errno|, flushes
"stderr" and "stdout", and calls |exit|. |routine| is the name of the
routine in which the error took place. */
void err_fatal_errno(const char *routine, const char *format, ...)
{
va_list ap;
va_list ap_copy;
int errorno = errno;
fflush(stdout);
if (_err_programName) {
if (routine)
fprintf(stderr, "%s (%s): ", _err_programName, routine);
else
fprintf(stderr, "%s: ", _err_programName);
} else {
if (routine) fprintf(stderr, "%s: ", routine);
}
va_start(ap, format);
va_copy(ap_copy, ap);
vfprintf(stderr, format, ap);
log_error_va(routine, format, ap_copy);
va_end(ap);
fprintf(stderr, " %s: %s\n", routine, strerror(errorno));
log_error(routine, "%s: %s", routine, strerror(errorno));
fflush(stderr);
fflush(stdout);
exit(1);
}
/* \doc |err_warning| flushes "stdout", prints a non-fatal warning on
"stderr", and returns to the caller. |routine| is the name of the
calling routine. */
void err_warning(const char *routine, const char *format, ...)
{
va_list ap;
va_list ap_copy;
fflush(stdout);
fflush(stderr);
if (_err_programName) {
if (routine)
fprintf(stderr, "%s (%s): ", _err_programName, routine);
else
fprintf(stderr, "%s: ", _err_programName);
} else {
if (routine) fprintf(stderr, "%s: ", routine);
}
va_start(ap, format);
va_copy(ap_copy, ap);
vfprintf(stderr, format, ap);
fprintf(stderr, "\n");
log_error_va(routine, format, ap_copy);
va_end(ap);
}
/* \doc |err_internal| flushes "stdout", prints the fatal error message,
flushes "stderr" and "stdout", and calls |abort| so that a core dump is
generated. */
void err_internal(const char *routine, const char *format, ...)
{
va_list ap;
va_list ap_copy;
va_start(ap, format);
va_copy(ap_copy, ap);
fflush(stdout);
if (_err_programName) {
if (routine)
fprintf(stderr, "%s (%s): Internal error\n ",
_err_programName, routine);
else
fprintf(stderr, "%s: Internal error\n ", _err_programName);
} else {
if (routine) fprintf(stderr, "%s: Internal error\n ", routine);
else fprintf(stderr, "Internal error\n ");
}
vfprintf(stderr, format, ap);
fprintf(stderr, "\n");
log_error_va(routine, format, ap_copy);
va_end(ap);
if (_err_programName)
fprintf(stderr, "Aborting %s...\n", _err_programName);
else
fprintf(stderr, "Aborting...\n");
fflush(stderr);
fflush(stdout);
abort();
}
|