Background log without color, hard to find when encountering problems, particularly SQL mistakes and errors, thus adding color must be very distinguished level. Figure is not added to the color of the log.
Detailed code
Highlight code block
//头文件
#include "color.h"
#include
//日志定义
void SQLResult1(const char *color, const char *file, int line, const char *format, ...)
{
va_list ap;
time_t timep;
struct tm *p;
char buf[2048];
const char *basefile;
time(&timep);
p = localtime(&timep);
sprintf(buf, "%s%02d-%02d-%02d %02d:%02d:%02d [%d]:%s:%d $ ",
color, p->tm_year - 100, p->tm_mon + 1, p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec,
getpid() % 10000,file, line);
va_start(ap, format);
vsprintf(buf+strlen(buf), format, ap);
va_end(ap);
sprintf(buf+strlen(buf), "\n%s", NONECOLOR);
fprintf(stdout, "%s\n", buf);
}
//进行简化格式
#define SQLRED(format, args...) SQLResult1(RED, __FILE__, __LINE__, format, ##args) //RED 为红色,可以自定义更改
#define SQLYELLOW(format, args...) SQLResult1(YELLOW, __FILE__, __LINE__, format, ##args) //YELLOW为红色,可以自定义更改
//使用示例
SQLRED("[sql error]: %s", sql);
SQLYELLOW("[sql error]: %s", sql);