1 /*
2 * Copyright (C) 1998-2001 Internet Software Consortium.
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
9 * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
10 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
11 * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
13 * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
14 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
15 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18 /* $Id: error.c,v 1.16 2001/08/08 22:54:49 gson Exp $ */
19
20 #include <config.h>
21
22 #include <stdio.h>
23 #include <stdlib.h>
24
25 #include <isc/error.h>
26 #include <isc/msgs.h>
27
28 static void
29 default_unexpected_callback(const char *, int, const char *, va_list)
30 ISC_FORMAT_PRINTF(3, 0);
31
32 static void
33 default_fatal_callback(const char *, int, const char *, va_list)
34 ISC_FORMAT_PRINTF(3, 0);
35
36 static isc_errorcallback_t unexpected_callback = default_unexpected_callback;
37 static isc_errorcallback_t fatal_callback = default_fatal_callback;
38
39 void
isc_error_setunexpected(isc_errorcallback_t cb)40 isc_error_setunexpected(isc_errorcallback_t cb) {
41 if (cb == NULL)
42 unexpected_callback = default_unexpected_callback;
43 else
44 unexpected_callback = cb;
45 }
46
47 void
isc_error_setfatal(isc_errorcallback_t cb)48 isc_error_setfatal(isc_errorcallback_t cb) {
49 if (cb == NULL)
50 fatal_callback = default_fatal_callback;
51 else
52 fatal_callback = cb;
53 }
54
55 void
isc_error_unexpected(const char * file,int line,const char * format,...)56 isc_error_unexpected(const char *file, int line, const char *format, ...) {
57 va_list args;
58
59 va_start(args, format);
60 (unexpected_callback)(file, line, format, args);
61 va_end(args);
62 }
63
64 void
isc_error_fatal(const char * file,int line,const char * format,...)65 isc_error_fatal(const char *file, int line, const char *format, ...) {
66 va_list args;
67
68 va_start(args, format);
69 (fatal_callback)(file, line, format, args);
70 va_end(args);
71 abort();
72 }
73
74 void
isc_error_runtimecheck(const char * file,int line,const char * expression)75 isc_error_runtimecheck(const char *file, int line, const char *expression) {
76 isc_error_fatal(file, line, "RUNTIME_CHECK(%s) %s", expression,
77 isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
78 ISC_MSG_FAILED, "failed"));
79 }
80
81 static void
default_unexpected_callback(const char * file,int line,const char * format,va_list args)82 default_unexpected_callback(const char *file, int line, const char *format,
83 va_list args)
84 {
85 fprintf(stderr, "%s:%d: ", file, line);
86 vfprintf(stderr, format, args);
87 fprintf(stderr, "\n");
88 fflush(stderr);
89 }
90
91 static void
default_fatal_callback(const char * file,int line,const char * format,va_list args)92 default_fatal_callback(const char *file, int line, const char *format,
93 va_list args)
94 {
95 fprintf(stderr, "%s:%d: %s: ", file, line,
96 isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
97 ISC_MSG_FATALERROR, "fatal error"));
98 vfprintf(stderr, format, args);
99 fprintf(stderr, "\n");
100 fflush(stderr);
101 }
102