Custom debug function used in C / C + + Development

Time:2021-5-7

1、 Colorless version

1、 Custom printf 
#include <stdio.h> 
#ifdef MYDEBUG 
#define DEBUG(arg...) {\ 
printf("[debug]:%s:%s:%d ---->",__FILE__,__FUNCTION__,__LINE__);\ 
printf(arg);\ 
fflush(stdout);\ 
} 
#else 
#define DEBUG(arg...) {} 
#endif 
Call instance: 
DEBUG("my debug......\n"); 
DEBUG("the a is %d\n",a); 

2、 Custom printf 
#ifdef DEBUG 
#define D_STR(_STR_) printf("f:%s(l:%d)%s\n",__FILE__,__LINE__,_STR_); 
#define D_LINE printf("f:%s(l:%d)-fn:%s\n",__FILE__,__LINE__,__FUNCTION__); 
#define D_CHAR(_CHAR_) printf("f:%s(l:%d)%d\n",__FILE__,__LINE__,_CHAR_); 
#define D_HEX(_HEX_) printf("f:%s(l:%d)[%x]\n",__FILE__,__LINE__,_HEX_); 
#else 
#define D_STR(_STR_) ; 
#define D_LINE ; 
#define D_CHAR(_CHAR_) ; 
#define D_HEX(_HEX_) ; 
#endif 

3、 Vfprintf 
static void fprint_message_to_stdout(const char *format, va_list arg_ptr) 
{ 
(void) vfprintf(stdout, format, arg_ptr); 
}

2、 Color version

#define LOG_DEBUG 0 
#define LOG_INFO 1 
#define LOG_WARNING 2 
#define LOG_ERR 3 

#define printf_error(s) \ 
printf("\e[1;31m[%s:%d func:%s]%s\e[0m\n", __FILE__, __LINE__, __FUNCTION__, s); 

#define printf_run(s) \ 
printf("\e[1;32m[%s:%d func:%s]%s\e[0m\n", __FILE__, __LINE__, __FUNCTION__, s); 

#define printf_warn(s) \ 
printf("\e[1;33m[%s:%d func:%s]%s\e[0m\n", __FILE__, __LINE__, __FUNCTION__, s); 

#define printf_log(t,s) \ 
switch (t) \ 
{ \ 
case LOG_ERR: \ 
printf_error(s); \ 
break; \ 
case LOG_DEBUG: \ 
case LOG_INFO: \ 
printf_run(s); \ 
break; \ 
case LOG_WARNING: \ 
printf_warn(s); \ 
default: \ 
printf(s); \ 
break; \ 
}

Welcome to my WeChat official account [MySQL database technology].