clog.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #pragma once
  2. #include <stdarg.h>
  3. #include <stdlib.h>
  4. #include <inttypes.h>
  5. #define CLOG_NONE 0
  6. #define CLOG_FATAL 1
  7. #define CLOG_ERROR 2
  8. #define CLOG_WARNING 3
  9. #define CLOG_INFO 4
  10. #define CLOG_DEBUG 5
  11. #ifndef CLOG_VISIBILITY
  12. #if defined(__ELF__)
  13. #define CLOG_VISIBILITY __attribute__((__visibility__("internal")))
  14. #elif defined(__MACH__)
  15. #define CLOG_VISIBILITY __attribute__((__visibility__("hidden")))
  16. #else
  17. #define CLOG_VISIBILITY
  18. #endif
  19. #endif
  20. #ifndef CLOG_ARGUMENTS_FORMAT
  21. #if defined(__GNUC__)
  22. #define CLOG_ARGUMENTS_FORMAT __attribute__((__format__(__printf__, 1, 2)))
  23. #else
  24. #define CLOG_ARGUMENTS_FORMAT
  25. #endif
  26. #endif
  27. #ifdef __cplusplus
  28. extern "C" {
  29. #endif
  30. CLOG_VISIBILITY void clog_vlog_debug(const char* module, const char* format, va_list args);
  31. CLOG_VISIBILITY void clog_vlog_info(const char* module, const char* format, va_list args);
  32. CLOG_VISIBILITY void clog_vlog_warning(const char* module, const char* format, va_list args);
  33. CLOG_VISIBILITY void clog_vlog_error(const char* module, const char* format, va_list args);
  34. CLOG_VISIBILITY void clog_vlog_fatal(const char* module, const char* format, va_list args);
  35. #define CLOG_DEFINE_LOG_DEBUG(log_debug_function_name, module, level) \
  36. CLOG_ARGUMENTS_FORMAT \
  37. inline static void log_debug_function_name(const char* format, ...) { \
  38. if (level >= CLOG_DEBUG) { \
  39. va_list args; \
  40. va_start(args, format); \
  41. clog_vlog_debug(module, format, args); \
  42. va_end(args); \
  43. } \
  44. }
  45. #define CLOG_DEFINE_LOG_INFO(log_info_function_name, module, level) \
  46. CLOG_ARGUMENTS_FORMAT \
  47. inline static void log_info_function_name(const char* format, ...) { \
  48. if (level >= CLOG_INFO) { \
  49. va_list args; \
  50. va_start(args, format); \
  51. clog_vlog_info(module, format, args); \
  52. va_end(args); \
  53. } \
  54. }
  55. #define CLOG_DEFINE_LOG_WARNING(log_warning_function_name, module, level) \
  56. CLOG_ARGUMENTS_FORMAT \
  57. inline static void log_warning_function_name(const char* format, ...) { \
  58. if (level >= CLOG_WARNING) { \
  59. va_list args; \
  60. va_start(args, format); \
  61. clog_vlog_warning(module, format, args); \
  62. va_end(args); \
  63. } \
  64. }
  65. #define CLOG_DEFINE_LOG_ERROR(log_error_function_name, module, level) \
  66. CLOG_ARGUMENTS_FORMAT \
  67. inline static void log_error_function_name(const char* format, ...) { \
  68. if (level >= CLOG_ERROR) { \
  69. va_list args; \
  70. va_start(args, format); \
  71. clog_vlog_error(module, format, args); \
  72. va_end(args); \
  73. } \
  74. }
  75. #define CLOG_DEFINE_LOG_FATAL(log_fatal_function_name, module, level) \
  76. CLOG_ARGUMENTS_FORMAT \
  77. inline static void log_fatal_function_name(const char* format, ...) { \
  78. if (level >= CLOG_FATAL) { \
  79. va_list args; \
  80. va_start(args, format); \
  81. clog_vlog_fatal(module, format, args); \
  82. va_end(args); \
  83. } \
  84. abort(); \
  85. }
  86. #ifdef __cplusplus
  87. } /* extern "C" */
  88. #endif