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