• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

ink_error.cc

Go to the documentation of this file.
00001 /** @file
00002 
00003   Error reporting routines
00004 
00005   @section license License
00006 
00007   Licensed to the Apache Software Foundation (ASF) under one
00008   or more contributor license agreements.  See the NOTICE file
00009   distributed with this work for additional information
00010   regarding copyright ownership.  The ASF licenses this file
00011   to you under the Apache License, Version 2.0 (the
00012   "License"); you may not use this file except in compliance
00013   with the License.  You may obtain a copy of the License at
00014 
00015       http://www.apache.org/licenses/LICENSE-2.0
00016 
00017   Unless required by applicable law or agreed to in writing, software
00018   distributed under the License is distributed on an "AS IS" BASIS,
00019   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00020   See the License for the specific language governing permissions and
00021   limitations under the License.
00022 
00023  */
00024 
00025 #include "libts.h"
00026 #include "ink_error.h"
00027 #include "ink_stack_trace.h"
00028 
00029 #include <syslog.h>
00030 #include <signal.h>    /* MAGIC_EDITING_TAG */
00031 
00032 static int ink_dprintf_level = 0;
00033 
00034 /**
00035   This routine causes process death. Some signal handler problems got
00036   in the way of abort before, so this is an overzealous and somewhat
00037   amusing implementation.
00038 
00039 */
00040 void
00041 ink_die_die_die(int retval)
00042 {
00043   abort();
00044   _exit(retval);
00045   exit(retval);
00046 }
00047 
00048 /**
00049   This routine prints/logs an error message given the printf format
00050   string in message_format, and the optional arguments.  The program is
00051   then terminated with return_code.
00052 
00053 */
00054 void
00055 ink_fatal_va(int return_code, const char * fmt, va_list ap)
00056 {
00057   char msg[1024];
00058   const size_t len = sizeof("FATAL: ") - 1;
00059 
00060   strncpy(msg, "FATAL: ", sizeof(msg));
00061   vsnprintf(msg + len, sizeof(msg) - len, fmt, ap);
00062   msg[sizeof(msg) - 1] = 0;
00063 
00064   fprintf(stderr, "%s\n", msg);
00065   syslog(LOG_CRIT, "%s", msg);
00066   ink_stack_trace_dump();
00067   ink_die_die_die(return_code);
00068 }
00069 
00070 void
00071 ink_fatal(int return_code, const char *message_format, ...)
00072 {
00073   va_list ap;
00074   va_start(ap, message_format);
00075   ink_fatal_va(return_code, message_format, ap);
00076   va_end(ap);
00077 }
00078 
00079 void
00080 ink_fatal_die(const char *message_format, ...)
00081 {
00082   va_list ap;
00083   va_start(ap, message_format);
00084   ink_fatal_va(1, message_format, ap);
00085   va_end(ap);
00086 }
00087 
00088 /**
00089   This routine prints/logs an error message given the printf format
00090   string in message_format, and the optional arguments.  The current
00091   errno is also printed.  The program is then terminated with return_code.
00092 
00093 */
00094 void
00095 ink_pfatal(int return_code, const char *message_format, ...)
00096 {
00097   va_list ap;
00098   char extended_format[4096], message[4096];
00099 
00100   char *errno_string;
00101 
00102   va_start(ap, message_format);
00103   errno_string = strerror(errno);
00104   snprintf(extended_format, sizeof(extended_format) - 1, "FATAL: %s <last errno = %d (%s)>",
00105            message_format, errno, (errno_string == NULL ? "unknown" : errno_string));
00106   extended_format[sizeof(extended_format) - 1] = 0;
00107   vsnprintf(message, sizeof(message) - 1, extended_format, ap);
00108   message[sizeof(message) - 1] = 0;
00109   fprintf(stderr, "%s\n", message);
00110   syslog(LOG_CRIT, "%s", message);
00111   va_end(ap);
00112   ink_stack_trace_dump();
00113   ink_die_die_die(return_code);
00114 }
00115 
00116 /**
00117   This routine prints/logs a warning message given the printf format
00118   string in message_format, and the optional arguments.
00119 
00120 */
00121 void
00122 ink_warning(const char *message_format, ...)
00123 {
00124   va_list ap;
00125   char extended_format[4096], message[4096];
00126   va_start(ap, message_format);
00127   snprintf(extended_format, sizeof(extended_format) - 1, "WARNING: %s", message_format);
00128   extended_format[sizeof(extended_format) - 1] = 0;
00129   vsnprintf(message, sizeof(message) - 1, extended_format, ap);
00130   message[sizeof(message) - 1] = 0;
00131   fprintf(stderr, "%s\n", message);
00132   syslog(LOG_WARNING, "%s", message);
00133   va_end(ap);
00134 }
00135 
00136 /**
00137   This routine prints/logs a warning message given the printf format
00138   string in message_format, and the optional arguments.  The current
00139   errno is also printed.
00140 
00141 */
00142 void
00143 ink_pwarning(const char *message_format, ...)
00144 {
00145   va_list ap;
00146   char extended_format[4096], message[4096];
00147   char *errno_string;
00148 
00149   va_start(ap, message_format);
00150   errno_string = strerror(errno);
00151   snprintf(extended_format, sizeof(extended_format) - 1, "WARNING: %s <last errno = %d (%s)>",
00152            message_format, errno, (errno_string == NULL ? "unknown" : errno_string));
00153   extended_format[sizeof(extended_format) - 1] = 0;
00154   vsnprintf(message, sizeof(message) - 1, extended_format, ap);
00155   message[sizeof(message) - 1] = 0;
00156   fprintf(stderr, "%s\n", message);
00157   syslog(LOG_WARNING, "%s", message);
00158   va_end(ap);
00159 }
00160 
00161 /**
00162   This routine prints/logs a notice message given the printf format
00163   string in message_format, and the optional arguments.
00164 
00165 */
00166 void
00167 ink_notice(const char *message_format, ...)
00168 {
00169   va_list ap;
00170   char extended_format[4096], message[4096];
00171   va_start(ap, message_format);
00172   snprintf(extended_format, sizeof(extended_format) - 1, "NOTE: %s", message_format);
00173   extended_format[sizeof(extended_format) - 1] = 0;
00174   vsnprintf(message, sizeof(message) - 1, extended_format, ap);
00175   message[sizeof(message) - 1] = 0;
00176   fprintf(stderr, "%s\n", message);
00177   syslog(LOG_NOTICE, "%s", message);
00178   va_end(ap);
00179 }
00180 
00181 /**
00182   This routine prints/logs a message given the printf format string in
00183   message_format, and the optional arguments.
00184 
00185 */
00186 void
00187 ink_eprintf(const char *message_format, ...)
00188 {
00189   va_list ap;
00190   char message[4096];
00191   va_start(ap, message_format);
00192   vsnprintf(message, sizeof(message) - 1, message_format, ap);
00193   message[sizeof(message) - 1] = 0;
00194   fprintf(stderr, "ERROR: %s\n", message);
00195   va_end(ap);
00196 }
00197 
00198 /**
00199   This routine prints/logs a warning message given the printf format
00200   string in message_format, and the optional arguments.
00201 
00202 */
00203 void
00204 ink_error(const char *message_format, ...)
00205 {
00206   va_list ap;
00207   char extended_format[2048], message[4096];
00208   va_start(ap, message_format);
00209   snprintf(extended_format, sizeof(extended_format) - 1, "ERROR: %s", message_format);
00210   extended_format[sizeof(extended_format) - 1] = 0;
00211   vsnprintf(message, sizeof(message) - 1, extended_format, ap);
00212   message[sizeof(message) - 1] = 0;
00213   fprintf(stderr, "%s\n", message);
00214   syslog(LOG_ERR, "%s", message);
00215   va_end(ap);
00216 }
00217 
00218 /**
00219   This routine prints/logs a message given the printf format string in
00220   message_format, and the optional arguments.
00221 
00222 */
00223 void
00224 ink_dprintf(int debug_level, const char *message_format, ...)
00225 {
00226   char message[4096];
00227   va_list ap;
00228   if (debug_level <= ink_dprintf_level) {
00229     va_start(ap, message_format);
00230     vsnprintf(message, sizeof(message) - 1, message_format, ap);
00231     message[sizeof(message) - 1] = 0;
00232     fprintf(stderr, "%s\n", message);
00233     va_end(ap);
00234   }
00235 }
00236 
00237 /**
00238   Set output level for ink_dprintf() function. For debugging purposes
00239   only!
00240 
00241 */
00242 int
00243 ink_set_dprintf_level(int debug_level)
00244 {
00245   int old_ink_dprintf_level = ink_dprintf_level;
00246   if ((ink_dprintf_level = debug_level) < 0)
00247     ink_dprintf_level = 0;
00248   return old_ink_dprintf_level;
00249 }

Generated by  doxygen 1.7.1