error.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2004, 2005, 2007  Internet Systems Consortium, Inc. ("ISC")
00003  * Copyright (C) 1998-2001  Internet Software Consortium.
00004  *
00005  * Permission to use, copy, modify, and/or distribute this software for any
00006  * purpose with or without fee is hereby granted, provided that the above
00007  * copyright notice and this permission notice appear in all copies.
00008  *
00009  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
00010  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
00011  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
00012  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
00013  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
00014  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
00015  * PERFORMANCE OF THIS SOFTWARE.
00016  */
00017 
00018 /* $Id: error.c,v 1.21 2007/06/19 23:47:17 tbox Exp $ */
00019 
00020 /*! \file */
00021 
00022 #include <config.h>
00023 
00024 #include <stdio.h>
00025 #include <stdlib.h>
00026 
00027 #include <isc/error.h>
00028 #include <isc/msgs.h>
00029 
00030 /*% Default unexpected callback. */
00031 static void
00032 default_unexpected_callback(const char *, int, const char *, va_list)
00033      ISC_FORMAT_PRINTF(3, 0);
00034 
00035 /*% Default fatal callback. */
00036 static void
00037 default_fatal_callback(const char *, int, const char *, va_list)
00038      ISC_FORMAT_PRINTF(3, 0);
00039 
00040 /*% unexpected_callback */
00041 static isc_errorcallback_t unexpected_callback = default_unexpected_callback;
00042 static isc_errorcallback_t fatal_callback = default_fatal_callback;
00043 
00044 void
00045 isc_error_setunexpected(isc_errorcallback_t cb) {
00046         if (cb == NULL)
00047                 unexpected_callback = default_unexpected_callback;
00048         else
00049                 unexpected_callback = cb;
00050 }
00051 
00052 void
00053 isc_error_setfatal(isc_errorcallback_t cb) {
00054         if (cb == NULL)
00055                 fatal_callback = default_fatal_callback;
00056         else
00057                 fatal_callback = cb;
00058 }
00059 
00060 void
00061 isc_error_unexpected(const char *file, int line, const char *format, ...) {
00062         va_list args;
00063 
00064         va_start(args, format);
00065         (unexpected_callback)(file, line, format, args);
00066         va_end(args);
00067 }
00068 
00069 void
00070 isc_error_fatal(const char *file, int line, const char *format, ...) {
00071         va_list args;
00072 
00073         va_start(args, format);
00074         (fatal_callback)(file, line, format, args);
00075         va_end(args);
00076         abort();
00077 }
00078 
00079 void
00080 isc_error_runtimecheck(const char *file, int line, const char *expression) {
00081         isc_error_fatal(file, line, "RUNTIME_CHECK(%s) %s", expression,
00082                         isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
00083                                        ISC_MSG_FAILED, "failed"));
00084 }
00085 
00086 static void
00087 default_unexpected_callback(const char *file, int line, const char *format,
00088                             va_list args)
00089 {
00090         fprintf(stderr, "%s:%d: ", file, line);
00091         vfprintf(stderr, format, args);
00092         fprintf(stderr, "\n");
00093         fflush(stderr);
00094 }
00095 
00096 static void
00097 default_fatal_callback(const char *file, int line, const char *format,
00098                        va_list args)
00099 {
00100         fprintf(stderr, "%s:%d: %s: ", file, line,
00101                 isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
00102                                ISC_MSG_FATALERROR, "fatal error"));
00103         vfprintf(stderr, format, args);
00104         fprintf(stderr, "\n");
00105         fflush(stderr);
00106 }

Generated on Tue Apr 28 17:41:04 2015 by Doxygen 1.5.4 for BIND9 Internals 9.11.0pre-alpha