base.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * Copyright (c) 2023 Lain 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, void *), void *param);
  61. EXPORT void blogva(int log_level, const char *format, va_list args);
  62. #if !defined(_MSC_VER) && !defined(SWIG)
  63. #define PRINTFATTR(f, a) __attribute__((__format__(__printf__, f, a)))
  64. #else
  65. #define PRINTFATTR(f, a)
  66. #endif
  67. PRINTFATTR(2, 3)
  68. EXPORT void blog(int log_level, const char *format, ...);
  69. PRINTFATTR(1, 2)
  70. #ifndef SWIG
  71. OBS_NORETURN
  72. #endif
  73. EXPORT void bcrash(const char *format, ...);
  74. #undef PRINTFATTR
  75. #ifdef __cplusplus
  76. }
  77. #endif