📄 mylog.cpp
字号:
//////////////////////////////////////////////////////////////////////
// Copyright(c) 1999-2007, TQ Digital Entertainment, All Rights Reserved
// Author: Bi quan Xu
// Created: 2007/05/18
// Describe:
//////////////////////////////////////////////////////////////////////
// MyLog.cpp: implementation of the CMyLog class.
//
#include <stdio.h>
#include "MyLog.h"
#include "time.h "
#include <sys/stat.h>
#include <direct.h>
#include <io.h>
#include <shlobj.h>
#include <assert.h>
#include <algorithm>
//#include "TMyIni.h"
#define MAX_LOG_TEXT 1*1024
#define GET_FULL_PATH(var) var
#define _EXCLUSIVE_WRITE_LOG
#pragma comment(lib,"Version.lib")
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CMyLog::CMyLog()
{
m_bWriteAppLogToFile = (0!=GetPrivateProfileInt(
"LogSetting",
"WriteAppLogToFile",
0,
GET_FULL_PATH("ini\\Log.ini")
));
#ifdef _EXCLUSIVE_WRITE_LOG
InitializeCriticalSection(&m_csLog);
#endif
_mkdir("log");
memset(m_szShortCurrentProcessName,0,sizeof(m_szShortCurrentProcessName));
memset(m_szExeFilePath,0,sizeof(m_szExeFilePath));
m_pchCurrentProcessName=m_szExeFilePath;
::GetModuleFileName(0,m_szExeFilePath,sizeof(m_szExeFilePath)-2);
m_pchCurrentProcessName=strrchr(m_szExeFilePath,'\\');
if(!m_pchCurrentProcessName)
{
m_pchCurrentProcessName=strrchr(m_szExeFilePath,'/');
}
if(m_pchCurrentProcessName)
{
++m_pchCurrentProcessName;
}
else
{
m_pchCurrentProcessName = m_szExeFilePath ;
}
strncpy(m_szShortCurrentProcessName,m_pchCurrentProcessName,sizeof(m_szShortCurrentProcessName));
char *pchToken=0;
pchToken=strchr(m_szShortCurrentProcessName,'.');
if(pchToken)
{
*pchToken='_';
}
///////
m_szAppLogFilePath[0]=0;
_snprintf(
m_szAppLogFilePath,
sizeof(m_szAppLogFilePath),
"log/%s_AppLog.log",
m_szShortCurrentProcessName
);
///////
m_szDebugLogFilePath[0]=0;
_snprintf(
m_szDebugLogFilePath,
sizeof(m_szDebugLogFilePath),
"log/%s_DebugLog.log",
m_szShortCurrentProcessName
);
///////
m_szExceptionFilePath[0]=0;
_snprintf(
m_szExceptionFilePath,
sizeof(m_szExceptionFilePath),
"log/%s_ExceptionLog.log",
m_szShortCurrentProcessName
);
SYSTEMTIME SystemTime={0,};
GetLocalTime(&SystemTime);
WRITE_APP_LOG(
"Process:%s Pid:%u StartAt:%04d-%02d-%02d %02d:%02d:%02d %04d ."
"Logger Loading...",
(const char *)GetProcessName(),
GetCurrentProcessId(),
SystemTime.wYear,
SystemTime.wMonth,
SystemTime.wDay,
SystemTime.wHour,
SystemTime.wMinute,
SystemTime.wSecond,
SystemTime.wMilliseconds
);
// CModDump::DumpModuleInfo(0);
}
CMyLog::~CMyLog()
{
WRITE_APP_LOG(
"ProcessName:%s ProcessID:%d .Logger Unloading...\n",
(const char *)GetProcessName(),
GetCurrentProcessId()
);
#ifdef _EXCLUSIVE_WRITE_LOG
DeleteCriticalSection(&m_csLog);
#endif
}
CMyLog& CMyLog::GetLogInstance()
{
static CMyLog ls_MyLog;
return ls_MyLog;
};
void CMyLog::WriteLogText(
const char *const pszLogFile,
const char* const pszTxt,
const bool bNewLine,
const bool bAppendHeader
)
{
_mkdir("log");
if(!pszLogFile)
{
return ;
}
if(!pszTxt)
{
return;
}
if(strlen(pszLogFile)<=0)
{
return ;
}
#ifdef _EXCLUSIVE_WRITE_LOG
EnterCriticalSection(&GetLogInstance().m_csLog);
#endif
FILE *fLog=fopen(pszLogFile,"a+t");
if(!fLog)
{
::OutputDebugString("\n\n WriteLog: Failed to open log file:");
::OutputDebugString(pszLogFile);
::OutputDebugString("\n\n");
#ifdef _EXCLUSIVE_WRITE_LOG
LeaveCriticalSection(&GetLogInstance().m_csLog);
#endif
return ;
}
char szTxt[128]={0,};
if(bAppendHeader)
{
SYSTEMTIME stNow={0,};
GetLocalTime(&stNow);
#ifndef _DONT_CATCH_EXCEPTION
try
#endif
{
szTxt[0]=0;
_snprintf(
szTxt,
sizeof(szTxt)-1,
"[%04d-%02d-%02d %02d:%02d:%02d.%03d]",
stNow.wYear,
stNow.wMonth,
stNow.wDay,
stNow.wHour,
stNow.wMinute,
stNow.wSecond,
stNow.wMilliseconds
);
}
#ifndef _DONT_CATCH_EXCEPTION
catch(...)
{
OutputDebugString("WriteLogTextEx format failed.");
}
#endif
fputs(szTxt,fLog);
fflush(fLog);
}
fputs(pszTxt,fLog);
fflush(fLog);
if(bNewLine)
{
fputs("\n",fLog);
fflush(fLog);
}
fclose(fLog);
#ifdef _EXCLUSIVE_WRITE_LOG
LeaveCriticalSection(&GetLogInstance().m_csLog);
#endif
}
void CMyLog::WriteDebugLog(
const char* const pszFmt,
...
)
{
if(!pszFmt)
{
return ;
}
char szBuf[MAX_LOG_TEXT]={0,};
int iRet = 0;
#ifndef _DONT_CATCH_EXCEPTION
try
#endif
{
iRet=_vsnprintf( szBuf,sizeof(szBuf), pszFmt, (char*) ((&pszFmt)+1) );
}
#ifndef _DONT_CATCH_EXCEPTION
catch(...)
{
OutputDebugString("WriteDebugLog:Failed to format string");
return ;
}
#endif
BackupDetect(GetLogInstance().m_szDebugLogFilePath);
WriteLogText(
GetLogInstance().m_szDebugLogFilePath,
szBuf
);
}
void CMyLog::WriteExceptionLog(
const char* const pszFmt,
...
)
{
if(!pszFmt)
{
return ;
};
char szBuf[MAX_LOG_TEXT]={0,};
int iRet = 0;
#ifndef _DONT_CATCH_EXCEPTION
try
#endif
{
iRet=_vsnprintf( szBuf,sizeof(szBuf), pszFmt, (char*) ((&pszFmt)+1) );
}
#ifndef _DONT_CATCH_EXCEPTION
catch(...)
{
OutputDebugString("WriteExceptionLog:Failed to format string");
return ;
}
#endif
BackupDetect(GetLogInstance().m_szExceptionFilePath);
WriteLogText(
GetLogInstance().m_szExceptionFilePath,
szBuf
);
}
void CMyLog::WriteAppLog(
const char* const pszFmt,
...
)
{
if(!pszFmt)
{
return ;
}
char szBuf[MAX_LOG_TEXT]={0,};
int iRet = 0;
#ifndef _DONT_CATCH_EXCEPTION
try
#endif
{
iRet=_vsnprintf( szBuf,sizeof(szBuf), pszFmt, (char*) ((&pszFmt)+1) );
}
#ifndef _DONT_CATCH_EXCEPTION
catch(...)
{
OutputDebugString("WriteAppLog:Failed to format string");
return ;
}
#endif
//release版本下才禁止程序日志
#ifndef _DEBUG
if(!GetLogInstance().m_bWriteAppLogToFile)
{
TRACEOUT("AppLog:%s",szBuf);
return ;
}
#endif
BackupDetect(GetLogInstance().m_szAppLogFilePath);
WriteLogText(
GetLogInstance().m_szAppLogFilePath,
szBuf
);
}
void CMyLog::WriteLogEx(
const char* const pszLogFile,
const char* const pszFmt,
...
)
{
if(!pszLogFile)
{
return ;
}
if(!pszFmt)
{
return ;
}
char szBuf[MAX_LOG_TEXT]={0,};
int iRet = 0;
#ifndef _DONT_CATCH_EXCEPTION
try
#endif
{
iRet=_vsnprintf( szBuf,sizeof(szBuf), pszFmt, (char*) ((&pszFmt)+1) );
}
#ifndef _DONT_CATCH_EXCEPTION
catch(...)
{
OutputDebugString("Failed to format string");
return ;
}
#endif
WriteLogText(
pszLogFile,
szBuf
);
}
const char*const CMyLog::GetExeFilePath()
{
return GetLogInstance().m_szExeFilePath;
}
const char *const CMyLog::GetProcessName()
{
return GetLogInstance().m_pchCurrentProcessName;
}
void TraceOut(
const char* const pszFmt,
...
)
{
if(!pszFmt)
{
return ;
}
char szBuf[MAX_LOG_TEXT]={0,};
int iRet = 0;
iRet=_vsnprintf( szBuf,sizeof(szBuf), pszFmt, (char*) ((&pszFmt)+1) );
OutputDebugString("\n ");
OutputDebugString(szBuf);
};
void CMyLog::AppendDebugLog(
const char* const pszFmt,
...
)
{
if(!pszFmt)
{
return ;
}
char szBuf[MAX_LOG_TEXT]={0,};
int iRet = 0;
#ifndef _DONT_CATCH_EXCEPTION
try
#endif
{
iRet=_vsnprintf( szBuf,sizeof(szBuf), pszFmt, (char*) ((&pszFmt)+1) );
}
#ifndef _DONT_CATCH_EXCEPTION
catch(...)
{
OutputDebugString("WriteDebugLog:Failed to format string");
return ;
}
#endif
WriteLogText(
GetLogInstance().m_szDebugLogFilePath,
szBuf,
false,
false
);
}
void CMyLog::AppendExceptionLog(
const char* const pszFmt,
...
)
{
if(!pszFmt)
{
return ;
};
char szBuf[MAX_LOG_TEXT]={0,};
int iRet = 0;
#ifndef _DONT_CATCH_EXCEPTION
try
#endif
{
iRet=_vsnprintf( szBuf,sizeof(szBuf), pszFmt, (char*) ((&pszFmt)+1) );
}
#ifndef _DONT_CATCH_EXCEPTION
catch(...)
{
OutputDebugString("WriteExceptionLog:Failed to format string");
return ;
}
#endif
WriteLogText(
GetLogInstance().m_szExceptionFilePath,
szBuf,
false,
false
);
}
void CMyLog::AppendAppLog(
const char* const pszFmt,
...
)
{
if(!pszFmt)
{
return ;
}
char szBuf[MAX_LOG_TEXT]={0,};
int iRet = 0;
#ifndef _DONT_CATCH_EXCEPTION
try
#endif
{
iRet=_vsnprintf( szBuf,sizeof(szBuf), pszFmt, (char*) ((&pszFmt)+1) );
}
#ifndef _DONT_CATCH_EXCEPTION
catch(...)
{
OutputDebugString("WriteAppLog:Failed to format string");
return ;
}
#endif
WriteLogText(
GetLogInstance().m_szAppLogFilePath,
szBuf,
false,
false
);
}
long MyGetFileSize( const char *const pszFile )
{
if(!pszFile)
{
return 0;
}
struct stat f_stat={0,};
if( stat( pszFile, &f_stat ) == -1 )
{
return -1;
}
return (long)f_stat.st_size;
}
void CMyLog::BackupDetect(const char *const pszLogFile)
{
#ifndef _DONT_CATCH_EXCEPTION
try
#endif
{
long lFileSize=MyGetFileSize(pszLogFile);
if(lFileSize>0)
{
UINT uiMaxLogFileSize=GetPrivateProfileInt(
"LogSetting",
"MaxLogFileSize",
30,
GET_FULL_PATH("ini\\Log.ini")
);
if(uiMaxLogFileSize<=0)
{
uiMaxLogFileSize = 30 ;
}
uiMaxLogFileSize <<= 10;
if((unsigned long) lFileSize>=uiMaxLogFileSize)
{
char szLogBackFile[MAX_PATH+32]={0,};
strncat(szLogBackFile,pszLogFile,sizeof(szLogBackFile));
strncat(szLogBackFile,"_bak",sizeof(szLogBackFile)-strlen(szLogBackFile));
DeleteFile(szLogBackFile);
CopyFile(pszLogFile,szLogBackFile,FALSE);
DeleteFile(pszLogFile);
}
};
}
#ifndef _DONT_CATCH_EXCEPTION
catch(...)
{
::OutputDebugString("exception on BackupDetect");
}
#endif
}
long SetDefaultCurrentDir()
{
SetCurrentDirectory(GetExeDir());
return 0;
}
std::string GetFullPath(const char* pszFilePath)
{
if(strchr(pszFilePath,':'))
{
return pszFilePath;
}
char szMod[MAX_PATH+64]={0,};
::GetModuleFileName(0,szMod,sizeof(szMod));
char *pchToken=strrchr(szMod,'\\');
if(pchToken)
{
*(pchToken+1)=0;
}
return szMod+(std::string)pszFilePath;
}
std::string FormatString(
const char* const pszFmt,
...
)
{
if(!pszFmt)
{
return "";
}
char szBuf[16*1024]={0,};
int iRet = 0;
iRet=_vsnprintf( szBuf,sizeof(szBuf), pszFmt, (char*) ((&pszFmt)+1) );
return szBuf;
};
void MultiSZToStringList(
const char *const pszMultiSZ,
STRING_LIST& StrList
)
{
StrList.clear();
char *pchCurrent=(char*)pszMultiSZ;
int iLen=-1;
while((iLen=strlen(pchCurrent))>0)
{
StrList.push_back(pchCurrent);
pchCurrent = pchCurrent + iLen + 1;
}
};
BOOL MakeSureFilePathExists(
const char* pszFilePath,
const bool bFileNameIncluded
)
{
std::string strDir;
std::string str=pszFilePath;
int Pos=0;
while((Pos=str.find('\\',Pos+1))!=-1)
{
strDir=str.substr(0,Pos).c_str();
CreateDirectory(strDir.c_str(),NULL);
}
if(bFileNameIncluded)
{
CreateDirectory(pszFilePath,NULL);
return -1!=_access(pszFilePath,0);
}
else
{
return -1!=_access(strDir.c_str(),0);
}
}
BOOL MyMoveFile(std::string strFromDir,std::string strToDir,std::string strFilePath)
{
std::string strFilePathPartial;
if(0==strcmpi(strFilePath.substr(0,strFromDir.size()).c_str(),strFromDir.c_str()))
{
strFilePathPartial=strFilePath.substr(strFromDir.size(),strFilePath.size()-strFromDir.size());
}
else
{
WRITE_EXCEPTION_LOG("MyMoveFile: strFilePath not part of from dir.FromDir:%s strToDir:%s strFilePath:%s",
strFromDir.c_str(),strToDir.c_str(),strFilePath.c_str());
return FALSE;
}
std::string strNewFile;
if((strFilePathPartial[0]!='\\')&&(strFilePathPartial[0]!='/')
&&(strFromDir[strFromDir.size()-1]!='\\')&&(strFromDir[strFromDir.size()-1]!='/'))
{
strNewFile=strToDir+"\\"+strFilePathPartial;
}
else
{
strNewFile=strToDir+strFilePathPartial;
}
if(!MakeSureFilePathExists(strNewFile.c_str()))
{
WRITE_EXCEPTION_LOG("MyMoveFile: MakeSureFilePathExists failed.FromDir:%s strToDir:%s strFilePath:%s",
strFromDir.c_str(),strToDir.c_str(),strFilePath.c_str());
return FALSE;
};
if(!CopyFile(
strFilePath.c_str(),
strNewFile.c_str(),
FALSE
))
{
WRITE_EXCEPTION_LOG("MyMoveFile: CopyFile failed.strFilePath:%s strNewFile:%s lasterror:%d ",
strFilePath.c_str(),strNewFile.c_str(),GetLastError());
return FALSE;
};
if(!DeleteFile(strFilePath.c_str()))
{
WRITE_EXCEPTION_LOG("MyMoveFile: DeleteFile failed.strFilePath:%s lasterror:%d",
strFilePath.c_str(),GetLastError());
};
return TRUE;
}
static int CALLBACK BrowseCallbackProc(HWND hwnd, UINT msg, LPARAM lp, LPARAM pData)
{
if (msg == BFFM_INITIALIZED )
{
::SendMessage(hwnd,BFFM_SETSELECTION,TRUE,pData);
}
return 0;
}
BOOL ChooseFolder(
const char* pszTitle,
HWND hWnd,
std::string strInitFolder,
std::string& strFolder,
const char* pszRootRestrict
)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -