printf log管理终极版本——printf字体颜色格式与log级别设置

1.printf字体格式和颜色设置

在平常的调试中,printf字体格式与颜色都是默认一致的。
如果可以根据log信息的重要程度,配以不同的颜色与格式,可以很方便的查找到要点。

printf(“\033[显示格式;字体颜色;背景颜色m 字符串 \033[0m” );
转义序列是以 ESC 开头,用 \033 完成相同的工作(ESC 的 ASCII 码用十进制表示就是 27, = 用八进制表示的 33)。

以上,第一个\033是设置随后的字体格式,结尾处的\033[0m是恢复默认值。

其中各部分参数选项如下:
\033[显示方式;前景色;背景色m
显示方式:
0(默认值)、1(高亮)、22(非粗体)、4(下划线)、24(非下划线)、5(闪烁)、25(非闪烁)、7(反显)、27(非反显)
前景色:
30(黑色)、31(红色)、32(绿色)、 33(黄色)、34(蓝色)、35(洋红)、36(青色)、37(白色)
背景色:
40(黑色)、41(红色)、42(绿色)、 43(黄色)、44(蓝色)、45(洋红)、46(青色)、47(白色)

示例:

printf( “\033[1;31;40m 【Error】 \033[0m” );
printf("\n\033[1m\033[5;31;43m ******* [INFO] *******\033[0m\033[22;33;40m %s \033[0m\n",__FUNCTION__)

printf高亮闪烁+红字黄底

将常用格式定义为宏:

#define HL_TWK_RED_YEL  "\033[1m\033[5;31;43m"	//闪烁高亮红字黄底
#define HL_RED_WRT      "\033[1;31;47m"			//高亮红色白底
#define HL_RED          "\033[1;31m"				//高亮红色
#define HL_YEL          "\033[1;33m"				//高亮黄色


#define PF_CLR  "\033[0m"							//清除

2.打印级别设置

根据以上将常用颜色与对应级别结合,设置全局宏定义。
错误:
DEBUG(HL_RED_WRT “[E]” PF_CLR"[%s]%s line:%d " format,FILE,FUNCTION, LINE,##VA_ARGS);
警告:
DEBUG(HL_YEL “[W]” PF_CLR"[%s]%s line:%d " format,FILE,FUNCTION, LINE,##VA_ARGS);

3.加入打印级别与开关
注意,字符串与宏之间需要留出空格,不然会报错:

warning: invalid suffix on literal; C++11 requires a space between literal and string macro [-Wliteral-suffix]

3.C++打印类名,打印类型名,打印变量名

使用宏定义:__PRETTY_FUNCTION__
它能以字符串的形式返回完整的函数签名,包括返回值、类名、函数名、参数列表、模板参数。

使用typeid:typeid(this).name()
可以打印变量名称。

当然还有使用#技巧:#define VNAME(name) (#name)
int myint = 5;
printf(“%s = %d\n”, VNAME(myint ), myint );
打印结果如下:
myint = 1010

类名打印测试


#include <iostream>
#include <typeinfo>
#include <string>
using namespace std;

#define __CLASSs__  \
	do { \
	string prettyFunc(__PRETTY_FUNCTION__); \
	cout<<prettyFunc<<endl; \
	prettyFunc = prettyFunc.substr(prettyFunc.find(" ")); \
	prettyFunc = prettyFunc.substr(0,prettyFunc.find("(")); \
	cout<<prettyFunc<<endl; \
	} while(0)

static std::string __CLASS__()
{
	string ad(__PRETTY_FUNCTION__);	
	return ad.substr(ad.find(" ")+1,ad.find("(")-ad.find(" ")-1);
}
static std::string _CutParenthesesNTail1(std::string&& prettyFuncon)
{
	auto posB = prettyFuncon.find(' ');
	if(posB!=std::string::npos)
        prettyFuncon.erase(prettyFuncon.begin(), prettyFuncon.begin()+posB+1);
	
    auto pos = prettyFuncon.find('(');
    if(pos!=std::string::npos)
        prettyFuncon.erase(prettyFuncon.begin()+pos, prettyFuncon.end());

    return std::move(prettyFuncon);
}

static std::string _CutParenthesesNTail(std::string&& prettyFuncon)
{
	auto posB = prettyFuncon.find(' ');
	if(posB!=std::string::npos)
        prettyFuncon.erase(prettyFuncon.begin(), prettyFuncon.begin()+posB+1);
	
    auto pos = prettyFuncon.find('(');
    if(pos!=std::string::npos)
        prettyFuncon.erase(prettyFuncon.begin()+pos, prettyFuncon.end());

    return std::move(prettyFuncon);
}
#define __STR_FUNCTION__ _CutParenthesesNTail(std::string(__PRETTY_FUNCTION__))


#define logv \
    do { \
           cout << "Hello, logv!" << endl;\
    } while (0)


class ygc 
{
public:
	int test()
	{

		__CLASS__;
		cout<<__STR_FUNCTION__<<endl;
		return 0;
	}
};

int main()
{
	ygc kk;
    cout << "Hello, world!" << endl;

	kk.test();

	string ad = "12 345678(9";
	ad = ad.substr(ad.find(" ")+1,ad.find("(")-ad.find(" ")-1);
	cout<< ad;
	return 0;
}

4.示例程序


    #define MY_LOG
    #ifdef MY_LOG
    #define DEBUG(format, ...) printf(format, ##__VA_ARGS__)
    #else
    #define DEBUG(format, ...)
    #endif
    
	#define VNAME(name) (#name)
    
    #define HL_TWK_RED_YEL  "\033[1m\033[5;31;43m"
    #define HL_RED_WRT      "\033[1;31;47m"
    #define HL_RED          "\033[1;31m"
    #define HL_YEL          "\033[1;33m"
    
    #define PF_CLR  "\033[0m"
    
    enum LOG_LEVEL
    {
        LOG_LEVEL_OFF = 0,
        LOG_LEVEL_FATAL,
        LOG_LEVEL_ERR,
        LOG_LEVEL_WARN,
        LOG_LEVEL_INFO,
        LOG_LEVEL_DBG,
        LOG_LEVEL_ALL,
    };
    
    static const int log_level = LOG_LEVEL_ALL;
    
    #define logv(format, ...) \
        do { \
             if(log_level>=LOG_LEVEL_FATAL)\
               DEBUG(HL_TWK_RED_YEL "[F]" PF_CLR"[%s]%s line:%d " format,__FILE__,__FUNCTION__, __LINE__,##__VA_ARGS__);\
        } while (0)
    
    #define loge(format, ...) \
        do { \
             if(log_level>=LOG_LEVEL_ERR)\
               DEBUG(HL_RED_WRT "[E]" PF_CLR"[%s]%s line:%d " format,__FILE__,__FUNCTION__, __LINE__,##__VA_ARGS__);\
        } while (0)
    
    #define logw(format, ...) \
        do { \
             if(log_level>=LOG_LEVEL_WARN)\
               DEBUG(HL_YEL "[W]" PF_CLR"[%s]%s line:%d " format,__FILE__,__FUNCTION__, __LINE__,##__VA_ARGS__);\
        } while (0)
    
    #define logi(format, ...) \
        do { \
             if(log_level>=LOG_LEVEL_INFO)\
               DEBUG(HL_RED "[I]" PF_CLR"%s line:%d " format,__FUNCTION__,__LINE__,##__VA_ARGS__);\
        } while (0)
    
    #define logd(format, ...) \
        do { \
             if(log_level>=LOG_LEVEL_DBG){\
               DEBUG( "[D]" "%s "  format,__FUNCTION__,##__VA_ARGS__);\
               }\
        } while (0)
    
    #define logr \
        do { \
             if(log_level>=LOG_LEVEL_ALL){\
               DEBUG(HL_RED "[I]" PF_CLR"%s line:%d is run!\n", __FUNCTION__,__LINE__);\
               }\
        } while (0)

#define POINT_CHECK(a)   do{ \
    if(NULL == a) { \
        loge("%s is null.",VNAME(a)); \
        return -1; \
    } \
}while(0)
  • 3
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值