Home | History | Annotate | Line # | Download | only in isc
      1  1.1  christos /*	$NetBSD: error.c,v 1.1 2024/02/18 20:57:49 christos Exp $	*/
      2  1.1  christos 
      3  1.1  christos /*
      4  1.1  christos  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
      5  1.1  christos  *
      6  1.1  christos  * SPDX-License-Identifier: MPL-2.0
      7  1.1  christos  *
      8  1.1  christos  * This Source Code Form is subject to the terms of the Mozilla Public
      9  1.1  christos  * License, v. 2.0. If a copy of the MPL was not distributed with this
     10  1.1  christos  * file, you can obtain one at https://mozilla.org/MPL/2.0/.
     11  1.1  christos  *
     12  1.1  christos  * See the COPYRIGHT file distributed with this work for additional
     13  1.1  christos  * information regarding copyright ownership.
     14  1.1  christos  */
     15  1.1  christos 
     16  1.1  christos /*! \file */
     17  1.1  christos 
     18  1.1  christos #include <stdio.h>
     19  1.1  christos #include <stdlib.h>
     20  1.1  christos 
     21  1.1  christos #include <isc/error.h>
     22  1.1  christos #include <isc/print.h>
     23  1.1  christos 
     24  1.1  christos /*% Default unexpected callback. */
     25  1.1  christos static void
     26  1.1  christos default_unexpected_callback(const char *, int, const char *, va_list)
     27  1.1  christos 	ISC_FORMAT_PRINTF(3, 0);
     28  1.1  christos 
     29  1.1  christos /*% Default fatal callback. */
     30  1.1  christos static void
     31  1.1  christos default_fatal_callback(const char *, int, const char *, va_list)
     32  1.1  christos 	ISC_FORMAT_PRINTF(3, 0);
     33  1.1  christos 
     34  1.1  christos /*% unexpected_callback */
     35  1.1  christos static isc_errorcallback_t unexpected_callback = default_unexpected_callback;
     36  1.1  christos static isc_errorcallback_t fatal_callback = default_fatal_callback;
     37  1.1  christos 
     38  1.1  christos void
     39  1.1  christos isc_error_setunexpected(isc_errorcallback_t cb) {
     40  1.1  christos 	if (cb == NULL) {
     41  1.1  christos 		unexpected_callback = default_unexpected_callback;
     42  1.1  christos 	} else {
     43  1.1  christos 		unexpected_callback = cb;
     44  1.1  christos 	}
     45  1.1  christos }
     46  1.1  christos 
     47  1.1  christos void
     48  1.1  christos isc_error_setfatal(isc_errorcallback_t cb) {
     49  1.1  christos 	if (cb == NULL) {
     50  1.1  christos 		fatal_callback = default_fatal_callback;
     51  1.1  christos 	} else {
     52  1.1  christos 		fatal_callback = cb;
     53  1.1  christos 	}
     54  1.1  christos }
     55  1.1  christos 
     56  1.1  christos void
     57  1.1  christos isc_error_unexpected(const char *file, int line, const char *format, ...) {
     58  1.1  christos 	va_list args;
     59  1.1  christos 
     60  1.1  christos 	va_start(args, format);
     61  1.1  christos 	(unexpected_callback)(file, line, format, args);
     62  1.1  christos 	va_end(args);
     63  1.1  christos }
     64  1.1  christos 
     65  1.1  christos void
     66  1.1  christos isc_error_fatal(const char *file, int line, const char *format, ...) {
     67  1.1  christos 	va_list args;
     68  1.1  christos 
     69  1.1  christos 	va_start(args, format);
     70  1.1  christos 	(fatal_callback)(file, line, format, args);
     71  1.1  christos 	va_end(args);
     72  1.1  christos 	abort();
     73  1.1  christos }
     74  1.1  christos 
     75  1.1  christos void
     76  1.1  christos isc_error_runtimecheck(const char *file, int line, const char *expression) {
     77  1.1  christos 	isc_error_fatal(file, line, "RUNTIME_CHECK(%s) failed", expression);
     78  1.1  christos }
     79  1.1  christos 
     80  1.1  christos static void
     81  1.1  christos default_unexpected_callback(const char *file, int line, const char *format,
     82  1.1  christos 			    va_list args) {
     83  1.1  christos 	fprintf(stderr, "%s:%d: ", file, line);
     84  1.1  christos 	vfprintf(stderr, format, args);
     85  1.1  christos 	fprintf(stderr, "\n");
     86  1.1  christos 	fflush(stderr);
     87  1.1  christos }
     88  1.1  christos 
     89  1.1  christos static void
     90  1.1  christos default_fatal_callback(const char *file, int line, const char *format,
     91  1.1  christos 		       va_list args) {
     92  1.1  christos 	fprintf(stderr, "%s:%d: fatal error: ", file, line);
     93  1.1  christos 	vfprintf(stderr, format, args);
     94  1.1  christos 	fprintf(stderr, "\n");
     95  1.1  christos 	fflush(stderr);
     96  1.1  christos }
     97