C/C++打印带颜色的调试信息

来源:互联网 发布:网络营销策划gzfq 编辑:程序博客网 时间:2024/06/05 07:01

虽然有gdb和IDE等调试工具,但在编程和调试的过程中,打印调试信息还是必不可少的——它方便、快捷,尤其是对于需要依赖关系错综复杂的大型工程,在关键的地方打印调试信息非常行之有效。不过调试信息打的多了,看着满屏幕的小字实在眼晕——而且,在正式版本发布的时候,还需要一行一行地删除这些调试信息,实在是个苦力活儿。于是,这两天写了一个打印带颜色的调试信息的工具,可以打印出各种各样颜色的信息,这样调试的时候看起来就方便多了,重要的信息可以一目了然。同时,这个工具和assert类似,只有在debug模式编译的时候才生效,在release模式编译时相当于一条空语句,这样就省去了删除/注释这些调试信息的功夫。

这个debug工具的代码如下:

/* * Copyright 2012@jike.com. All rights reserved. *  * debug_info.h * *    Created on: 2012-10-17 *        Author: zhangrui@jike.com *   Description: This is used to print colorful debug infos to help *   us with debugging. */#ifndef ECOM_BASE_DEBUG_INFO_H_#define ECOM_BASE_DEBUG_INFO_H_#define NONE "\033[m"#define RED "\033[0;31m"#define LIGHT_RED "\033[1;31m"#define GREEN "\033[0;32;32m"#define LIGHT_GREEN "\033[1;32m"#define BLUE "\033[0;32;34m"#define LIGHT_BLUE "\033[1;34m"#define DARY_GRAY "\033[1;30m"#define CYAN "\033[0;36m"#define LIGHT_CYAN "\033[1;36m"#define PURPLE "\033[0;35m"#define LIGHT_PURPLE "\033[1;35m"#define BROWN "\033[0;33m"#define YELLOW "\033[0;33m"#define LIGHT_YELLOW "\033[1;33m"#define LIGHT_GRAY "\033[0;37m"#define WHITE "\033[1;37m"#define COLORFUL_PRINT(color, fmt, arg...) \    do { \        printf(color fmt NONE, ##arg); \    } while(0)#define PRINT_RED(fmt, arg...) \    COLORFUL_PRINT(RED, fmt, ##arg)#define PRINT_LRED(fmt, arg...) \    CLORFUL_PRINT(LIGHT_RED, fmt, ##arg)#define PRINT_GREEN(fmt, arg...) \    CLORFUL_PRINT(GREEN, fmt, ##arg)#define PRINT_LGREEN(fmt, arg...) \    CLORFUL_PRINT(LIGHT_GREEN, fmt, ##arg)#define PRINT_BLUE(fmt, arg...) \    CLORFUL_PRINT(BLUE, fmt, ##arg)#define PRINT_LBLUE(fmt, arg...) \    CLORFUL_PRINT(LIGHT_BLUE, fmt, ##arg)#define PRINT_GRAY(fmt, arg...) \    CLORFUL_PRINT(DARY_GRAY, fmt, ##arg)#define PRINT_LGRAY(fmt, arg...) \    COLORFUL_PRINT(LIGHT_GRAY, fmt, ##arg)#define PRINT_CYAN(fmt, arg...) \    CLORFUL_PRINT(CYAN, fmt, ##arg)#define PRINT_LCYAN(fmt, arg...) \    CLORFUL_PRINT(LIGHT_CYAN, fmt, ##arg)#define PRINT_PURPLE(fmt, arg...) \    CLORFUL_PRINT(PURPLE, fmt, ##arg)#define PRINT_LPURPLE(fmt, arg...) \    CLORFUL_PRINT(LIGHT_PURPLE, fmt, ##arg)#define PRINT_BROWN(fmt, arg...) \    CLORFUL_PRINT(BROWN, fmt, ##arg)#define PRINT_YELLOW(fmt, arg...) \    CLORFUL_PRINT(YELLOW, fmt, ##arg)#define PRINT_LYELLOW(fmt, arg...) \    CLORFUL_PRINT(LIGHT_YELLOW, fmt, ##arg)#define PRINT_WHITE(fmt, arg...) \    CLORFUL_PRINT(WHITE, fmt, ##arg)#ifdef NDEBUG  #ifdef DEBUG_MESSAGE  #undef DEBUG_MESSAGE  #endif  #define DEBUG_MESSAGE(color, fmt, arg...) ((void)(0))#else  #ifdef DEBUG_MESSAGE  #undef DEBUG_MESSAGE  #endif  #define DEBUG_MESSAGE(color, fmt, arg...) \    do { \      time_t __cur_time__ = time(NULL); \      struct tm __cur_tm__; \      localtime_r(&__cur_time__, &__cur_tm__); \      COLORFUL_PRINT(color, \          "[%04d-%02d-%02d %02d:%02d:%02d : %s(%d)]:" fmt "\n", \          __cur_tm__.tm_year + 1900, __cur_tm__.tm_mon, __cur_tm__.tm_mday, \          __cur_tm__.tm_hour, __cur_tm__.tm_min, __cur_tm__.tm_sec, \          __FILE__, __LINE__, ##arg); \      } while(0)#endif#ifdef DEBUG_ERROR#undef DEBUG_ERROR#endif#ifdef DEBUG_WARNING#undef DEBUG_WARNING#endif#ifdef DEBUG_INFO#undef DEBUG_INFO#endif#define DEBUG_ERROR(fmt, arg...) DEBUG_MESSAGE(RED, fmt, ##arg)#define DEBUG_WARNING(fmt, arg...) DEBUG_MESSAGE(YELLOW, fmt, ##arg)#define DEBUG_INFO(fmt, arg...) DEBUG_MESSAGE(NONE, fmt, ##arg)#endif  // ECOM_BASE_DEBUG_INFO_H_


一个简单的例子及运行结果:

#include "debug_info.h"#include <cstdio>#include <string>//#include <ctime>int main() {    std::string str = "Congratulations!";    int a = 5;    int b = 10;    DEBUG_ERROR("If you can run this....%s, %d, %d", str.c_str(), a, b);    return 0;}

运行结果: