diff --git a/include/utils/debug.h b/include/utils/debug.h index 989a5f4..897d3dc 100644 --- a/include/utils/debug.h +++ b/include/utils/debug.h @@ -21,11 +21,17 @@ #define _DEBUG "[DEBUG]" #endif +void _cws_log_info_internal(const char *file, int line, const char *fmt, ...); +void _cws_log_warning_internal(const char *file, int line, const char *fmt, ...); +void _cws_log_error_internal(const char *file, int line, const char *fmt, ...); +void _cws_log_debug_internal(const char *file, int line, const char *fmt, ...); + +#define cws_log_info(fmt, ...) _cws_log_info_internal(__FILE__, __LINE__, fmt, ##__VA_ARGS__) +#define cws_log_warning(fmt, ...) _cws_log_warning_internal(__FILE__, __LINE__, fmt, ##__VA_ARGS__) +#define cws_log_error(fmt, ...) _cws_log_error_internal(__FILE__, __LINE__, fmt, ##__VA_ARGS__) +#define cws_log_debug(fmt, ...) _cws_log_debug_internal(__FILE__, __LINE__, fmt, ##__VA_ARGS__) + void cws_log_init(void); -void cws_log_info(const char *fmt, ...); -void cws_log_debug(const char *fmt, ...); -void cws_log_warning(const char *fmt, ...); -void cws_log_error(const char *fmt, ...); void cws_log_shutdown(void); #endif diff --git a/src/utils/debug.c b/src/utils/debug.c index eac1604..7e00990 100644 --- a/src/utils/debug.c +++ b/src/utils/debug.c @@ -1,65 +1,73 @@ #include "utils/debug.h" #include #include -#include +#include void cws_log_init(void) { openlog("cws", LOG_PID | LOG_CONS, LOG_DAEMON); } -void cws_log_info(const char *fmt, ...) { +void _cws_log_info_internal(const char *file, int line, const char *fmt, ...) { va_list args; - fprintf(stdout, _INFO " "); + fprintf(stdout, _INFO " [%s:%d] ", file, line); va_start(args, fmt); - vfprintf(stdout, fmt, args); + va_end(args); + fprintf(stdout, "\n"); + + va_start(args, fmt); syslog(LOG_INFO, fmt, args); - va_end(args); - fprintf(stdout, "\n"); } -void cws_log_warning(const char *fmt, ...) { +void _cws_log_warning_internal(const char *file, int line, const char *fmt, ...) { va_list args; - fprintf(stdout, _WARNING " "); + fprintf(stdout, _WARNING " [%s:%d] ", file, line); va_start(args, fmt); - vfprintf(stdout, fmt, args); - va_end(args); fprintf(stdout, "\n"); + + va_start(args, fmt); + syslog(LOG_WARNING, fmt, args); + va_end(args); } -void cws_log_error(const char *fmt, ...) { +void _cws_log_error_internal(const char *file, int line, const char *fmt, ...) { va_list args; - fprintf(stdout, _ERR " "); + fprintf(stdout, _ERR " [%s:%d] ", file, line); va_start(args, fmt); - vfprintf(stdout, fmt, args); + va_end(args); + fprintf(stdout, "\n"); + + va_start(args, fmt); syslog(LOG_ERR, fmt, args); + va_end(args); +} +#ifdef DEVELOPER +void _cws_log_debug_internal(const char *file, int line, const char *fmt, ...) { + va_list args; + fprintf(stdout, _DEBUG " [%s:%d] ", file, line); + va_start(args, fmt); + vfprintf(stdout, fmt, args); va_end(args); fprintf(stdout, "\n"); + + va_start(args, fmt); + vsyslog(LOG_DEBUG, fmt, args); + va_end(args); } +#else +void _cws_log_debug_internal(const char *file, int line, const char *fmt, ...) { + (void)file; + (void)line; + (void)fmt; + /* Nothing */ +} +#endif void cws_log_shutdown(void) { closelog(); } - -#ifdef EVELOPER -void cws_log_debug(const char *fmt, ...) { - fprintf(stdout, _DEBUG " [%s:%d] ", __FILE__, __LINE__); - va_list args; - va_start(args, fmt); - - vfprintf(stdout, fmt, args); - syslog(LOG_DEBUG, fmt, args); - - va_end(args); - fprintf(stdout, "\n"); -} -#else -void cws_log_debug(const char *fmt, ...) { - /* Nothing */ -} -#endif