/contrib/ntp/libisc/error.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 101 lines · 68 code · 16 blank · 17 comment · 4 complexity · 77ac32ddd53c734e41ef07b0b81f7033 MD5 · raw file

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