base.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /******************************************************************************
  2. Copyright (c) 2013 by Hugh Bailey <[email protected]>
  3. This software is provided 'as-is', without any express or implied
  4. warranty. In no event will the authors be held liable for any damages
  5. arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it
  8. freely, subject to the following restrictions:
  9. 1. The origin of this software must not be misrepresented; you must not
  10. claim that you wrote the original software. If you use this software
  11. in a product, an acknowledgment in the product documentation would be
  12. appreciated but is not required.
  13. 2. Altered source versions must be plainly marked as such, and must not be
  14. misrepresented as being the original software.
  15. 3. This notice may not be removed or altered from any source
  16. distribution.
  17. ******************************************************************************/
  18. #include <stdio.h>
  19. #include <stdarg.h>
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. #include "c99defs.h"
  23. #include "base.h"
  24. static enum log_type log_output_level = LOG_WARNING;
  25. static int crashing = 0;
  26. static void def_log_handler(enum log_type type, const char *format,
  27. va_list args)
  28. {
  29. char out[4096];
  30. vsnprintf(out, sizeof(out), format, args);
  31. if (type >= log_output_level) {
  32. switch (type) {
  33. case LOG_DEBUG:
  34. printf("debug: %s\n", out);
  35. break;
  36. case LOG_INFO:
  37. printf("info: %s\n", out);
  38. break;
  39. case LOG_WARNING:
  40. fprintf(stderr, "warning: %s\n", out);
  41. break;
  42. case LOG_ERROR:
  43. fprintf(stderr, "error: %s\n", out);
  44. }
  45. }
  46. }
  47. static void def_crash_handler(const char *format, va_list args)
  48. {
  49. vfprintf(stderr, format, args);
  50. exit(0);
  51. }
  52. static void (*log_handler)(enum log_type, const char *, va_list) =
  53. def_log_handler;
  54. static void (*crash_handler)(const char *, va_list) = def_crash_handler;
  55. void base_set_log_handler(
  56. void (*handler)(enum log_type, const char *, va_list))
  57. {
  58. log_handler = handler;
  59. }
  60. void base_set_crash_handler(void (*handler)(const char *, va_list))
  61. {
  62. crash_handler = handler;
  63. }
  64. void bcrash(const char *format, ...)
  65. {
  66. va_list args;
  67. if (crashing) {
  68. fputs("Crashed in the crash handler", stderr);
  69. exit(2);
  70. }
  71. crashing = 1;
  72. va_start(args, format);
  73. crash_handler(format, args);
  74. va_end(args);
  75. }
  76. void blog(enum log_type type, const char *format, ...)
  77. {
  78. va_list args;
  79. va_start(args, format);
  80. log_handler(type, format, args);
  81. va_end(args);
  82. }