C++ format log output implementation code
- 2020-06-15 09:59:20
- OfStack
The core code
void LogOut(LPCTSTR pFormat, ...)
{
char LogFile[128]={0};
char str[1024]={0};
FILE** fp=&(ThreadDatas[0].pf);
va_list pArg;
EnterCriticalSection(&cs_log);// Exclusive access to
__try
{
SYSTEMTIME st;
if(ThreadDatas[0].num>100000)//10W article 1 A file
{
if(fp)
{
fclose(*fp);
*fp=0;
}
ThreadDatas[0].num=0;
}
if(NULL==*fp)
{
char tstr[20]={0};
//GetSystemTime(&st);
GetLocalTime(&st);
sprintf(LogFile,".\\logs\\ASR_%04d-%02d-%02d.%02d_%02d_%02d.log",st.wYear,st.wMonth,st.wDay,st.wHour,st.wMinute,st.wSecond);
*fp=fopen(LogFile,"a+");
if( NULL==*fp )
{
//printf("Create LogFile faile %s\n",LogFile);
LogEvent("Create LogFile faile %s",LogFile);
return;
}
//else
//LogEvent("Create LogFile %s",LogFile,*fp);
}
GetLocalTime(&st);
sprintf(str,"%04d-%02d-%02d %02d:%02d:%02d.%03d %s",st.wYear,st.wMonth,st.wDay,st.wHour,st.wMinute,st.wSecond,st.wMilliseconds,pFormat);
va_start(pArg, pFormat);
//fprintf(*fp,chMsg);
vfprintf(*fp, str, pArg);
va_end(pArg);
fflush(*fp);
}
__except(EXCEPTION_EXECUTE_HANDLER)
{
LogEvent("Thread Log file Error:%s",LogFile);
}
LeaveCriticalSection(&cs_log);// Release exclusive access
}
Well, that's the end of this article, and this site will provide you with more code.