向來寫的調試信息都是隨意輸出,不管是查看起來仍是寫起代碼來都不方便,因而寫了一個debug信息輸出庫,便於輸出debug信息。
debug信息分爲close,info,warning,error,critical五級,分別以不一樣的顏色輸出,使用DEBUG1,DEBUG2,DEBUG3,DEBUG4宏來調用,全局變量g_debug定義爲0時,關閉debug信息輸出,高等級會使低於這個等級的debug信息輸出,高於的則不輸出,如g_debug=3,DEBUG1,DEBUG2,DEBUG3的都會輸出,DEBUG4則不輸出,這樣用起來方便多了。
代碼以下:ide
/* ***************************
* print.h *
* define debug information *
* author: vin_do@live.cn *
* **************************/
#ifndef __PRINT_H__
#define __PRINT_H__
#include
#include
#include
#include
#define FD stdout
#define COLOR_CODE(CODES) "\033[" CODES "m"
#define BLACK COLOR_CODE("30")
#define RED COLOR_CODE("31")
#define GREEN COLOR_CODE("32")
#define YELLOW COLOR_CODE("33")
#define BLUE COLOR_CODE("34")
#define PURPLE COLOR_CODE("35")
#define CYAN COLOR_CODE("36")
#define WHITE COLOR_CODE("37")
#define MODE_CODE(CODES) "\033[" CODES "m"
#define BRIGHT MODE_CODE("1")
#define UNDERLINE MODE_CODE("4")
#define FLASH MODE_CODE("5")
#define INVERSE MODE_CODE("7")
#define NONE MODE_CODE("0")
#define PRINT_ANSI_START(MODE,COLOR) fputs(MODE,FD); \
fputs(COLOR,FD)
#define PRINT_ANSI_STOP fputs(NONE,FD); \
fputs("\n",FD)
typedef enum debug_Level
{
DEBUG_NONE = -1,
DEBUG_INFO,
DEBUG_WARNING,
DEBUG_ERROR,
DEBUG_CRITICAL
}debug_Level;
typedef struct debug_St
{
debug_Level level;
char* msg_head;
char* mode;
char* color;
}debug_St;
void inline DebugPrint(debug_Level level, const char* file, const int line, const char* fmt, ...);
#define DEBUG(level, file, line, fmt, ...) DebugPrint(level, file, line, fmt, ##__VA_ARGS__)
#define DEBUG1(fmt, ...) if(g_debug-1 > DEBUG_NONE) \
DEBUG(DEBUG_INFO, __FILE__, __LINE__, fmt, ##__VA_ARGS__)
#define DEBUG2(fmt, ...) if(g_debug-1 > DEBUG_INFO) \
DEBUG(DEBUG_WARNING, __FILE__, __LINE__, fmt, ##__VA_ARGS__)
#define DEBUG3(fmt, ...) if(g_debug-1 > DEBUG_WARNING) \
DEBUG(DEBUG_ERROR, __FILE__, __LINE__, fmt, ##__VA_ARGS__)
#define DEBUG4(fmt, ...) if(g_debug-1 > DEBUG_ERROR) \
DEBUG(DEBUG_CRITICAL, __FILE__, __LINE__, fmt, ##__VA_ARGS__)
#endif //__PRINT_H__
/* ****************************
* print.c *
* debug information function *
* author: vin_do@live.cn *
* ***************************/
#include "print.h"
static debug_St debug_st[] =
{
{DEBUG_INFO, "[INFO]", NONE, NONE},
{DEBUG_WARNING, "[WARNING]", BRIGHT, YELLOW},
{DEBUG_ERROR, "[ERROR]", BRIGHT, RED},
{DEBUG_CRITICAL,"[CRITITAL]", UNDERLINE BRIGHT, RED},
};
void inline DebugPrint(debug_Level level, const char* file, const int line, const char* fmt, ...)
{
va_list args;
PRINT_ANSI_START(debug_st[level].mode, debug_st[level].color);
fprintf(FD,"%s[%s %d]",debug_st[level].msg_head, file,line);
va_start(args, fmt);
vfprintf(FD, fmt, args);
va_end(args);
PRINT_ANSI_STOP;
fflush(FD);
}
/* **********************************
* logtest.c *
* testing debug information *
* using parameter '-d' '-dd' etc. *
* author: vin_do@live.cn *
***********************************/
#include
#include "print.h"
void usage();
int g_debug = 0;
int main(int argc, char **argv)
{
int i = 1;
char *server = NULL;
for (i = 0; i