#include #include #define LOG_DEBUG 0 #define LOG_EVENT 1 #define LOG_NOTICE 2 #define LOG_WARNING 3 #define LOG_ERROR 4 // These defines should be used to get "verbosity" of spercific logging level. Like: // if (DEBUG_LEVEL > 2) // ast_log(LOG_DEBUG, ......) // Below they have hard-coded values. But in real life they should point to some global // variables. Like: // #define DEBUG_LEVEL options_debug // #define DEBUG_LEVEL 0 #define EVENT_LEVEL 1 #define NOTICE_LEVEL 1 #define WARNING_LEVEL 1 #define ERROR_LEVEL 1 void __ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...) __attribute__ ((format (printf, 5, 6))); #define ast_log(level, ...) { \ if (level == LOG_DEBUG) { \ if (DEBUG_LEVEL) { \ __ast_log(level, __FILE__, __LINE__, __PRETTY_FUNCTION__, __VA_ARGS__); \ } \ } else if (level == LOG_EVENT) { \ if (EVENT_LEVEL) { \ __ast_log(level, __FILE__, __LINE__, __PRETTY_FUNCTION__, __VA_ARGS__); \ } \ } else if (level == LOG_NOTICE) { \ if (NOTICE_LEVEL) { \ __ast_log(level, __FILE__, __LINE__, __PRETTY_FUNCTION__, __VA_ARGS__); \ } \ } else if (level == LOG_WARNING) { \ if (WARNING_LEVEL) { \ __ast_log(level, __FILE__, __LINE__, __PRETTY_FUNCTION__, __VA_ARGS__); \ } \ } else if (level == LOG_ERROR) { \ if (ERROR_LEVEL) { \ __ast_log(level, __FILE__, __LINE__, __PRETTY_FUNCTION__, __VA_ARGS__); \ } \ } else { \ __ast_log(level, __FILE__, __LINE__, __PRETTY_FUNCTION__, __VA_ARGS__); \ } \ } // Example of shorthand macros if we need them... #define ast_log_debug(...) \ if (DEBUG_LEVEL) { \ __ast_log(LOG_DEBUG, __FILE__, __LINE__, __PRETTY_FUNCTION__, __VA_ARGS__); \ } #define ast_log_error(...) \ if (ERROR_LEVEL) { \ __ast_log(LOG_ERROR, __FILE__, __LINE__, __PRETTY_FUNCTION__, __VA_ARGS__); \ } // ... void __ast_log(int level, const char *file, int line, const char *function, const char *fmt, ...) { va_list ap; va_start(ap, fmt); printf("<%d> %s:%d - ", level, file, line); vprintf(fmt, ap); va_end(ap); } int main(int argc, char** argv) { ast_log(LOG_DEBUG, "Hello\n"); ast_log(LOG_ERROR, "%d + %d = %s\n", 2, 2, "4"); ast_log_debug("Hello\n"); ast_log_error("%d + %d = %s\n", 2, 2, "4"); };