log macro to print file name and line number

This commit is contained in:
wangyu
2017-07-25 00:04:49 +08:00
committed by wangyu
parent 3e50007667
commit 966ec3c925
4 changed files with 30 additions and 14 deletions

12
log.cpp
View File

@@ -1,10 +1,11 @@
#include <log.h>
const int log_level=log_debug;
int enable_log_position=1;
int enable_log_color=1;
char log_text[][10]={"FATAL","ERROR","WARN","INFO","DEBUG","TRACE"};
char log_color[][10]={RED,RED,YEL,GRN,BLU,""};
void log(int level,const char* str, ...) {
void log0(const char * file,const char * function,int line,int level,const char* str, ...) {
if(level>log_level) return ;
if(level>log_trace||level<0) return ;
@@ -17,17 +18,20 @@ void log(int level,const char* str, ...) {
time(&timer);
tm_info = localtime(&timer);
if(enable_log_color)
printf(log_color[level]);
strftime(buffer, 100, "%Y-%m-%d %H:%M:%S", tm_info);
printf("[%s][%s]",buffer,log_text[level]);
printf("[%s][%s]",buffer,log_text[level],file,line);
if(enable_log_position)printf("[%s,func:%s,line:%d]",file,function,line);
va_list vlist;
va_start(vlist, str);
vfprintf(stdout, str, vlist);
va_end(vlist);
printf(RESET);
if(enable_log_color)
printf(RESET);
//printf("\n");
fflush(stdout);
}