assertions.c revision 1.1.1.3 1 /* $NetBSD: assertions.c,v 1.1.1.3 2007/03/30 20:16:19 ghen Exp $ */
2
3 /*
4 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (c) 1997,1999 by Internet Software Consortium.
6 *
7 * Permission to use, copy, modify, and 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
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
17 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20 #if !defined(LINT) && !defined(CODECENTER)
21 static const char rcsid[] = "Id: assertions.c,v 1.2.18.1 2005/04/27 05:01:05 sra Exp";
22 #endif
23
24 #include "port_before.h"
25
26 #include <errno.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 #include <isc/assertions.h>
32
33 #include "port_after.h"
34
35 /*
36 * Forward.
37 */
38
39 static void default_assertion_failed(const char *, int, assertion_type,
40 const char *, int);
41
42 /*
43 * Public.
44 */
45
46 assertion_failure_callback __assertion_failed = default_assertion_failed;
47
48 void
49 set_assertion_failure_callback(assertion_failure_callback f) {
50 if (f == NULL)
51 __assertion_failed = default_assertion_failed;
52 else
53 __assertion_failed = f;
54 }
55
56 const char *
57 assertion_type_to_text(assertion_type type) {
58 const char *result;
59
60 switch (type) {
61 case assert_require:
62 result = "REQUIRE";
63 break;
64 case assert_ensure:
65 result = "ENSURE";
66 break;
67 case assert_insist:
68 result = "INSIST";
69 break;
70 case assert_invariant:
71 result = "INVARIANT";
72 break;
73 default:
74 result = NULL;
75 }
76 return (result);
77 }
78
79 /*
80 * Private.
81 */
82
83 static void
84 default_assertion_failed(const char *file, int line, assertion_type type,
85 const char *cond, int print_errno)
86 {
87 fprintf(stderr, "%s:%d: %s(%s)%s%s failed.\n",
88 file, line, assertion_type_to_text(type), cond,
89 (print_errno) ? ": " : "",
90 (print_errno) ? strerror(errno) : "");
91 abort();
92 /* NOTREACHED */
93 }
94
95 /*! \file */
96