简单日志输出管理

#define SOURCE_FILE_NAME (strrchr((char*)__FILE__, '/') == NULL) ? (char*)__FILE__ : (strrchr((char*)__FILE__, '/') + 1)
#define LOG_BUF_LEN 1024
#define LOG_OUT(level, color, msg...) \
do{ \
unsigned char logBuf[LOG_BUF_LEN] = {0};\
struct timeval tv; \
gettimeofday(&tv, NULL); \
snprintf(logBuf + strlen(logBuf), LOG_BUF_LEN-strlen(logBuf), "\033[1;"color"m"); \
snprintf(logBuf + strlen(logBuf), LOG_BUF_LEN-strlen(logBuf), "[%ld.%03ld]:", tv.tv_sec, tv.tv_usec/1000);\
snprintf(logBuf + strlen(logBuf), LOG_BUF_LEN-strlen(logBuf), "[%s]", level);\
snprintf(logBuf + strlen(logBuf), LOG_BUF_LEN-strlen(logBuf), "[%ld]", syscall(SYS_gettid));\
snprintf(logBuf + strlen(logBuf), LOG_BUF_LEN-strlen(logBuf), "[%s|%s|%d]:", SOURCE_FILE_NAME, __FUNCTION__, __LINE__);\
snprintf(logBuf + strlen(logBuf), LOG_BUF_LEN-strlen(logBuf), msg); \
snprintf(logBuf + strlen(logBuf), LOG_BUF_LEN-strlen(logBuf), "\033[0m"); \
fprintf(stdout, "%s", logBuf);\
}while(0)

posted @ 2018-09-18 08:25  mingzhang  阅读(252)  评论(0编辑  收藏  举报