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