base.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * Copyright (c) 2013 Hugh Bailey <[email protected]>
  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 THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #pragma once
  17. #include <stdarg.h>
  18. #include "c99defs.h"
  19. /*
  20. * Just contains logging/crash related stuff
  21. */
  22. #ifdef __cplusplus
  23. extern "C" {
  24. #endif
  25. #define STRINGIFY(x) #x
  26. #define STRINGIFY_(x) STRINGIFY(x)
  27. #define S__LINE__ STRINGIFY_(__LINE__)
  28. #define INT_CUR_LINE __LINE__
  29. #define FILE_LINE __FILE__ " (" S__LINE__ "): "
  30. #define OBS_COUNTOF(x) (sizeof(x) / sizeof(x[0]))
  31. enum {
  32. /**
  33. * Use if there's a problem that can potentially affect the program,
  34. * but isn't enough to require termination of the program.
  35. *
  36. * Use in creation functions and core subsystem functions. Places that
  37. * should definitely not fail.
  38. */
  39. LOG_ERROR = 100,
  40. /**
  41. * Use if a problem occurs that doesn't affect the program and is
  42. * recoverable.
  43. *
  44. * Use in places where failure isn't entirely unexpected, and can
  45. * be handled safely.
  46. */
  47. LOG_WARNING = 200,
  48. /**
  49. * Informative message to be displayed in the log.
  50. */
  51. LOG_INFO = 300,
  52. /**
  53. * Debug message to be used mostly by developers.
  54. */
  55. LOG_DEBUG = 400
  56. };
  57. typedef void (*log_handler_t)(int lvl, const char *msg, va_list args, void *p);
  58. EXPORT void base_get_log_handler(log_handler_t *handler, void **param);
  59. EXPORT void base_set_log_handler(log_handler_t handler, void *param);
  60. EXPORT void base_set_crash_handler(void (*handler)(const char *, va_list,
  61. void *),
  62. void *param);
  63. EXPORT void blogva(int log_level, const char *format, va_list args);
  64. #if !defined(_MSC_VER) && !defined(SWIG)
  65. #define PRINTFATTR(f, a) __attribute__((__format__(__printf__, f, a)))
  66. #else
  67. #define PRINTFATTR(f, a)
  68. #endif
  69. PRINTFATTR(2, 3)
  70. EXPORT void blog(int log_level, const char *format, ...);
  71. PRINTFATTR(1, 2)
  72. #ifndef SWIG
  73. OBS_NORETURN
  74. #endif
  75. EXPORT void bcrash(const char *format, ...);
  76. #undef PRINTFATTR
  77. #ifdef __cplusplus
  78. }
  79. #endif