文章链接:https://codemouse.online/archives/1215235231
重新订制print
#define DEBUG 1
#if DEBUG
#define ERR_PRINT(fmt, args...) do{printf("[*]Errorinfo: <%s>: " fmt "\n" \
,__func__, ##args); } while (false)
#define DBG_PRINT(fmt, args...) do{printf("[*]printinfo: <%s>: " fmt "\n" \
,__func__, ##args); } while (false)
#else
#define ERR_PRINT(...) do {} while (false)
#define DBG_PRINT(...) do {} while (false)
#endif
#include <stdio.h>
#include <stdarg.h>
//#define DEBUG
int Debug_printf(char const*__restrict fmt,...)
{
#ifdef DEBUG
int ret;
va_list ap;
va_start(ap,fmt);
ret=vfprintf(stdout,fmt,ap);
va_end(ap);
return(ret);
#else
return 0;
#endif
}
void showtime(char * buff)
{
time_t t;
struct tm * lt;
time(&t);
lt = localtime(&t);//转为时间结构。
sprintf(buff, "%d-%.2d-%.2d %.2d:%.2d:%.2d", lt->tm_year + 1900,
lt->tm_mon, lt->tm_mday, lt->tm_hour,
lt->tm_min, lt->tm_sec);//输出结果
//2019-04-16 12:32:33
}
#define LOGPRIN (1)
#define PRIN (1)
#if LOGPRIN
#if PRIN
// 打印出来并写入文件
int Log_Printf(const char *szFormat, ...)
{
int iLength; //存储字符串的长度信息
va_list pArgs; //声明一个va_list型变量
FILE *file = fopen("log.txt", "a");
char time[20];
showtime(time);
fprintf(file, "%s :\t", time);
va_start(pArgs, szFormat); //让pArgs指向变参
iLength = vfprintf(file, szFormat, pArgs); //输出到缓冲区szBuffer中
iLength = vprintf(szFormat, pArgs); //输出到缓冲区szBuffer中
va_end(pArgs); //释放pArgs
fclose(file);
return iLength; //返回字符串长度
}
#else
// 写入文件
int Log_Printf(const char *szFormat, ...)
{
int iLength; //存储字符串的长度信息
va_list pArgs; //声明一个va_list型变量
FILE *file = fopen("log.txt", "a");
char time[20];
showtime(time);
fprintf(file, "%s :\t", time);
va_start(pArgs, szFormat); //让pArgs指向变参
iLength = vfprintf(file, szFormat, pArgs); //输出到缓冲区szBuffer中
va_end(pArgs); //释放pArgs
fclose(file);
return iLength; //返回字符串长度
}
#endif
#else
// 打印到控制台
int Log_Printf(const char *szFormat, ...)
{
int iLength; //存储字符串的长度信息
va_list pArgs; //声明一个va_list型变量
va_start(pArgs, szFormat); //让pArgs指向变参
iLength = vprintf(szFormat, pArgs); //输出到缓冲区szBuffer中
va_end(pArgs); //释放pArgs
return iLength; //返回字符串长度
}
#endif