#include "debug.h"
#define DBG_LEVEL_CRITICAL 0 #define DBG_LEVEL_ERR 1 #define DBG_LEVEL_WARNING 2 #define DBG_LEVEL_MSG 3 #define DBG_LEVEL_TRACE 4
const char *msg_level[] = { "CRI", "ERR", "WAR", "MSG", "TRA" };
int dbg_comm(const char *file, const int line, unsigned int level, const char *fmt, va_list arglist) { FILE *tg = NULL;
if(level == DBG_LEVEL_CRITICAL || level == DBG_LEVEL_ERR) { tg = stderr; } else { tg = stdout; } #ifdef OUTPUT_TIME struct tm *time_info; struct timeval tv = {0};
if (gettimeofday(&tv, NULL) < 0) { perror("gettimeofday"); return -1; } time_info = localtime(&(tv.tv_sec)); fprintf(tg, "%02d:%02d:%02d:%06d-", time_info->tm_hour, time_info->tm_min, time_info->tm_sec, (int)tv.tv_usec); #endif
#ifdef OUTPUT_PID pid_t pid; pid = getpid(); fprintf(tg, "%05u-", pid); #endif
#ifdef OUTPUT_TID pid_t tid; if((tid = syscall(SYS_gettid)) == -1) { perror("syscall"); tid = 65535; } fprintf(tg, "%05u-", tid); #endif
fprintf(tg, "%s-->%s:%d: ", msg_level[level], file, line); vfprintf(tg, fmt, arglist); return 0; }
int dbg_trace(const char *file, const int line, const char *fmt, ...) { int ret; va_list arglist; va_start(arglist, fmt); ret = dbg_comm(file, line, DBG_LEVEL_TRACE, fmt, arglist); return ret; }
int dbg_msg(const char *file, const int line, const char *fmt, ...) { int ret; va_list arglist; va_start(arglist, fmt); ret = dbg_comm(file, line, DBG_LEVEL_MSG, fmt, arglist); return ret; }
int dbg_warning(const char *file, const int line, const char *fmt, ...) { int ret; va_list arglist; va_start(arglist, fmt); ret = dbg_comm(file, line, DBG_LEVEL_WARNING, fmt, arglist); return ret; }
int dbg_err(const char *file, const int line, const char *fmt, ...) { int ret; va_list arglist; va_start(arglist, fmt); ret = dbg_comm(file, line, DBG_LEVEL_ERR, fmt, arglist); return ret; }
int dbg_critical(const char *file, const int line, const char *fmt, ...) { int ret; va_list arglist; va_start(arglist, fmt); ret = dbg_comm(file, line, DBG_LEVEL_CRITICAL, fmt, arglist); return ret; }
|