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