/contrib/bind9/lib/isc/error.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 106 lines · 68 code · 17 blank · 21 comment · 4 complexity · c768bb36a943a693808b6067c5726237 MD5 · raw file

  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. /* $Id: error.c,v 1.21 2007/06/19 23:47:17 tbox Exp $ */
  18. /*! \file */
  19. #include <config.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <isc/error.h>
  23. #include <isc/msgs.h>
  24. /*% Default unexpected callback. */
  25. static void
  26. default_unexpected_callback(const char *, int, const char *, va_list)
  27. ISC_FORMAT_PRINTF(3, 0);
  28. /*% Default fatal callback. */
  29. static void
  30. default_fatal_callback(const char *, int, const char *, va_list)
  31. ISC_FORMAT_PRINTF(3, 0);
  32. /*% unexpected_callback */
  33. static isc_errorcallback_t unexpected_callback = default_unexpected_callback;
  34. static isc_errorcallback_t fatal_callback = default_fatal_callback;
  35. void
  36. isc_error_setunexpected(isc_errorcallback_t cb) {
  37. if (cb == NULL)
  38. unexpected_callback = default_unexpected_callback;
  39. else
  40. unexpected_callback = cb;
  41. }
  42. void
  43. isc_error_setfatal(isc_errorcallback_t cb) {
  44. if (cb == NULL)
  45. fatal_callback = default_fatal_callback;
  46. else
  47. fatal_callback = cb;
  48. }
  49. void
  50. isc_error_unexpected(const char *file, int line, const char *format, ...) {
  51. va_list args;
  52. va_start(args, format);
  53. (unexpected_callback)(file, line, format, args);
  54. va_end(args);
  55. }
  56. void
  57. isc_error_fatal(const char *file, int line, const char *format, ...) {
  58. va_list args;
  59. va_start(args, format);
  60. (fatal_callback)(file, line, format, args);
  61. va_end(args);
  62. abort();
  63. }
  64. void
  65. isc_error_runtimecheck(const char *file, int line, const char *expression) {
  66. isc_error_fatal(file, line, "RUNTIME_CHECK(%s) %s", expression,
  67. isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
  68. ISC_MSG_FAILED, "failed"));
  69. }
  70. static void
  71. default_unexpected_callback(const char *file, int line, const char *format,
  72. va_list args)
  73. {
  74. fprintf(stderr, "%s:%d: ", file, line);
  75. vfprintf(stderr, format, args);
  76. fprintf(stderr, "\n");
  77. fflush(stderr);
  78. }
  79. static void
  80. default_fatal_callback(const char *file, int line, const char *format,
  81. va_list args)
  82. {
  83. fprintf(stderr, "%s:%d: %s: ", file, line,
  84. isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
  85. ISC_MSG_FATALERROR, "fatal error"));
  86. vfprintf(stderr, format, args);
  87. fprintf(stderr, "\n");
  88. fflush(stderr);
  89. }