67 lines
1.5 KiB
C++
67 lines
1.5 KiB
C++
// stdafx.cpp : 只包括标准包含文件的源文件
|
|
// CCEXPipe.pch 将作为预编译头
|
|
// stdafx.obj 将包含预编译类型信息
|
|
|
|
#include "stdafx.h"
|
|
#include <stdio.h>
|
|
|
|
// TODO: 在 STDAFX.H 中
|
|
// 引用任何所需的附加头文件,而不是在此文件中引用
|
|
|
|
|
|
void LogOutToFile(const char* fmt, ...)
|
|
{
|
|
static CRITICAL_SECTION stCritical;
|
|
static BOOL bInit = FALSE;
|
|
static char acLogPath[2148] = {0};
|
|
if (FALSE == bInit)
|
|
{
|
|
bInit = TRUE;
|
|
GetModuleFileName(NULL, acLogPath, 2047);
|
|
|
|
for (int i = strlen(acLogPath)-1; i > 0; i--)
|
|
{
|
|
if (acLogPath[i] == '\\')
|
|
{
|
|
acLogPath[i] = '\0';
|
|
break;
|
|
}
|
|
}
|
|
strcat(acLogPath, "\\CCEXPipe.log");
|
|
|
|
InitializeCriticalSection(&stCritical);
|
|
}
|
|
|
|
EnterCriticalSection(&stCritical);
|
|
|
|
va_list ap;
|
|
va_start(ap, fmt);
|
|
char pBuffer[800] = "";
|
|
if (vsnprintf_s(pBuffer, 796,_TRUNCATE, fmt, ap) > 0)
|
|
{
|
|
if (strlen(pBuffer) == 0 || pBuffer[strlen(pBuffer)-1] != '\n')
|
|
{
|
|
pBuffer[strlen(pBuffer)+1] = '\0';
|
|
pBuffer[strlen(pBuffer)] = '\n';
|
|
}
|
|
}
|
|
va_end(ap);
|
|
|
|
FILE* pFile = NULL;
|
|
fopen_s(&pFile, acLogPath, "ab+");
|
|
if (NULL != pFile)
|
|
{
|
|
char acTime[32] = {0};
|
|
SYSTEMTIME stTime;
|
|
GetLocalTime(&stTime);
|
|
sprintf_s(acTime, "[%02d-%02d %02d:%02d:%02d.%03d] ", stTime.wMonth, stTime.wDay, stTime.wHour, stTime.wMinute, stTime.wSecond, stTime.wMilliseconds);
|
|
fwrite(acTime, 1, strlen(acTime), pFile);
|
|
fwrite(pBuffer, 1, strlen(pBuffer), pFile);
|
|
fwrite("\r\n", 1, strlen("\r\n"), pFile);
|
|
fclose(pFile);
|
|
}
|
|
LeaveCriticalSection(&stCritical);
|
|
}
|
|
|
|
|