参考链接:1 ((sizeof(n)+sizeof(int)-1)&~(sizeof(int)-1))
2 对C语言中va_list,va_start,va_arg和va_end的一点理解
example 1:
main.c
#include <stdio.h>
#include "Debug.h"
int main(int argc, char *argv[])
{
int data = 999;
printDebugMsg("TestProgram", "data= %d", data);
printf("Hello C-Free!\n");
return 0;
}
Debug.h
#ifndef __DEBUG_H__
#define __DEBUG_H__
#ifdef __cplusplus
extern "C" {
#endif
#include <stdio.h>
#include <stdarg.h>
#define _DEBUG
#ifdef _DEBUG
//#define DEBUG_TO_FILE
#ifdef DEBUG_TO_FILE
//调试信息输出到以下文件
#define DEBUG_FILE "..//tmp//debugmsg.txt"
//调试信息的缓冲长度
#define DEBUG_BUFFER_MAX 4096
//将调试信息输出到文件中
#define printDebugMsg(moduleName, format, ...) {\
char buffer[DEBUG_BUFFER_MAX+1] = {0};\
snprintf(buffer, DEBUG_BUFFER_MAX,\
"[%s] "format" File:%s, Line:%d\n", moduleName, ##__VA_ARGS__, __FILE__, __LINE__);\
FILE* fd = fopen(DEBUG_FILE, "a");\
if(fd != NULL){\
fwrite(buffer, strlen(buffer), 1, fd);\
fflush(fd);\
fclose(fd);\
}\
}
#else //#ifndef DEBUG_TO_FILE
//将调试信息输出到终端
#define printDebugMsg(moduleName, format, ...)\
printf("[%s] "format" File:%s, Line:%d\n", moduleName, ##__VA_ARGS__, __FILE__, __LINE__);
#endif //end for #ifdef DEBUG_TO_FILE
#else
//发行版本,什么也不做
#define printDebugMsg(moduleName, format, ...)
#endif //end for #ifdef _DEBUG
/*-----------------