Home | History | Annotate | Line # | Download | only in isc
assertions.c revision 1.2.10.2.2.1
      1 /*	$NetBSD: assertions.c,v 1.2.10.2.2.1 2011/01/23 21:52:33 bouyer 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 #include <sys/cdefs.h>
     21 #if !defined(LINT) && !defined(CODECENTER) && !defined(lint)
     22 #ifdef notdef
     23 static const char rcsid[] = "Id: assertions.c,v 1.2.18.1 2005/04/27 05:01:05 sra Exp";
     24 #else
     25 __RCSID("$NetBSD: assertions.c,v 1.2.10.2.2.1 2011/01/23 21:52:33 bouyer Exp $");
     26 #endif
     27 #endif
     28 
     29 #include "port_before.h"
     30 
     31 #include <errno.h>
     32 #include <stdio.h>
     33 #include <stdlib.h>
     34 #include <string.h>
     35 
     36 #include <isc/assertions.h>
     37 
     38 #include "port_after.h"
     39 
     40 /*
     41  * Forward.
     42  */
     43 
     44 static void default_assertion_failed(const char *, int, assertion_type,
     45 				     const char *, int);
     46 
     47 /*
     48  * Public.
     49  */
     50 
     51 assertion_failure_callback __assertion_failed = default_assertion_failed;
     52 
     53 void
     54 set_assertion_failure_callback(assertion_failure_callback f) {
     55 	if (f == NULL)
     56 		__assertion_failed = default_assertion_failed;
     57 	else
     58 		__assertion_failed = f;
     59 }
     60 
     61 const char *
     62 assertion_type_to_text(assertion_type type) {
     63 	const char *result;
     64 
     65 	switch (type) {
     66 	case assert_require:
     67 		result = "REQUIRE";
     68 		break;
     69 	case assert_ensure:
     70 		result = "ENSURE";
     71 		break;
     72 	case assert_insist:
     73 		result = "INSIST";
     74 		break;
     75 	case assert_invariant:
     76 		result = "INVARIANT";
     77 		break;
     78 	default:
     79 		result = NULL;
     80 	}
     81 	return (result);
     82 }
     83 
     84 /*
     85  * Private.
     86  */
     87 
     88 /* coverity[+kill] */
     89 static void
     90 default_assertion_failed(const char *file, int line, assertion_type type,
     91 			 const char *cond, int print_errno)
     92 {
     93 	fprintf(stderr, "%s:%d: %s(%s)%s%s failed.\n",
     94 		file, line, assertion_type_to_text(type), cond,
     95 		(print_errno) ? ": " : "",
     96 		(print_errno) ? strerror(errno) : "");
     97 	abort();
     98 	/* NOTREACHED */
     99 }
    100 
    101 /*! \file */
    102