VC平台运行日志

系统运行时日志输出,可变参数输出到文件:

#define _CRT_SECURE_NO_WARNINGS
#include 
#include 
#include 
#include 
#include 
#include 
 
#define TRACE_BUFF	1024
 
int my_trace(const char* fmt, ...)
{
	char	TempBuff[TRACE_BUFF] = { 0 };
	FILE*	filehandle;
	size_t	strSize;
	va_list valist;
 
	va_start(valist, fmt);
	sprintf(TempBuff, fmt, valist);
	va_end(valist);
 
	if ((filehandle = fopen("D:\\ATL_Debug_log.txt", "at+")) == NULL) {
		return -1;
	}
 
	strSize = strlen(TempBuff);
	fwrite(TempBuff, sizeof(char), strSize, filehandle);
 
	if (fclose(filehandle)) {
		return -2;
	}
 
	return 0;
}
VS20xx以前的平台不需要_CRT_SECURE_NO_WARNINGS

发表评论