日志打印
根据cout颜色的处理,封装了一个日志处理函数。一个小小的工具,方便日志调试。
"\33[0m" :白色
"\33[1m\33[34m":蓝色
"\33[1m\33[33m":黄色
"\33[1m\33[31m":红色
"\33[1m\33[35m":粉色
"\33[1m\33[32m":绿色
根据此进行封装
直接贴代码
文件log.h
#ifndef LOG_H
#define LOG_H
#define LOG_DEBUG "DEBUG"
#define LOG_WARNING "WARNING"
#define LOG_ERROR "ERROR"
#define LOG_INFO "INFO"
#define LOG_USER "USER"
#define LOG(level, format, ...)\
do{\
if(level == LOG_DEBUG)\
{\
fprintf(stderr, "\33[0m" "[%s|%s@%s,%d] " format "\n",level, __func__, __FILE__, __LINE__, ##__VA_ARGS__ );\
}\
else if(level == LOG_WARNING)\
{\
fprintf(stderr, "\33[1m\33[33m" "[%s|%s@%s,%d] " format "\n",level, __func__, __FILE__, __LINE__, ##__VA_ARGS
__ );\
}\
else if(level == LOG_ERROR)\
{\
fprintf(stderr, "\33[1m\33[31m" "[%s|%s@%s,%d] " format "\n",level, __func__, __FILE__, __LINE__, ##__VA_ARGS
__ );\
}\
else if(level == LOG_INFO)\
{\
fprintf(stderr, "\33[1m\33[34m" "[%s|%s@%s,%d] " format "\n",level, __func__, __FILE__, __LINE__, ##__VA_ARGS
__ );\
}\
else if(level == LOG_USER)\
{\
fprintf(stderr, "\33[1m\33[32m" "[%s|%s@%s,%d] " format "\n",level, __func__, __FILE__, __LINE__, ##__VA_ARGS
__ );\
}\
}while (0)
#endif
测试函数:main.cpp
#include <stdio.h>
#include "log.h"
int main()
{
int num = 10;
LOG(LOG_DEBUG, "Debug! white, num: %d\n", num);
double num1 = 10.1;
LOG(LOG_WARNING, "Warning! yellow, num1: %f\n", num1);
std::string num2 = "Hello World!";
LOG(LOG_ERROR, "Error! red, num: %s\n", num2.c_str());
char *num3 = "Welcome to C++!";
LOG(LOG_INFO, "Info! blue, num3: %s\n", num3);
LOG(LOG_USER, "User! green\n");
return 0;
}
运行结果:
如是转载,请指明原出处:http://blog.csdn.net/feitianxuxue,谢谢合作!