zoukankan      html  css  js  c++  java
  • 后台日志添加颜色

    后台日志不加颜色,当遇到问题很难发现,特别是出现段错误以及SQL错误,因此加上颜色进行级别区分很有必有。

    一、详细代码

    //头文件
    #include "color.h"
    #include <stdarg.h>
    
    //日志定义
    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), "
    %s", NONECOLOR);
        fprintf(stdout, "%s
    ", 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);
    
    

    二、结果截图

    三、参考

    va_start : https://www.runoob.com/?s=va_start

    作者:yusq77

    -------------------------------------------

    Wish you all the best and good health in 2021.

  • 相关阅读:
    求最低价格
    A*算法入门
    hdu 4715
    手动扩大栈内存,让AC无忧
    hdu 4710
    hdu 1698
    poj3468区间延迟更新模板题
    hdu 1059二进制优化背包问题
    2059龟兔赛跑
    水1276
  • 原文地址:https://www.cnblogs.com/yusq77/p/10956902.html
Copyright © 2011-2022 走看看