标签:02d Code FNAME MAX st strFilePath Way Output LogFile
Code-Output File-Two Way
July 9, 2020 9:25 PM
1.使用ofstream 输出
#include <fstream>
SYSTEMTIME st;
GetLocalTime(&st);
CString strTime;
strTime.Format(_T(" %d-%02d-%02d %02d:%02d:%02d.%03d"),st.wYear,st.wMonth,st.wDay,st.wHour,st.wMinute,st.wSecond,st.wMilliseconds);
string strIPAddr="192.168.0.1"
string temp = "D://mess//"+strIPAddr + ".txt";
ofstream outfile(temp.c_str(),std::ios::app|std::ios::out);
outfile<<strIPAddr<<"-"<<GetCurrentThreadId()<<"-"<<"TryConnect"<<"-"<<"errorNum"<<m_ierrNum<<strTime.GetBuffer()<<endl;
outfile.close();
strTime.ReleaseBuffer();
2.使用Class 输出
class CExportLog
{
public:
CExportLog()
{
InitializeCriticalSection(&m_csLock);
//获取模块运行路径
wchar_t wFilePath[_MAX_FNAME] = {0};
wchar_t wDrive[_MAX_FNAME] = {0};
wchar_t wDir[_MAX_FNAME] = {0};
wchar_t wFileName[_MAX_FNAME] = {0};
wchar_t wExe[_MAX_FNAME] = {0};
GetModuleFileName(NULL, wFilePath, _MAX_FNAME);
_tsplitpath(wFilePath, wDrive, wDir, wFileName, wExe);
std::wstring strFilePath;
strFilePath.append(wDrive);
strFilePath.append(wDir);
strFilePath.append(L"Log\\log.log");
m_strFilePath = strFilePath.c_str();
}
virtual ~CExportLog()
{
DeleteCriticalSection(&m_csLock);
}
void ExportMsg(CString strMsg)
{
CStdioFile LogFile;
setlocale( LC_CTYPE, "chs" );
EnterCriticalSection(&m_csLock);
if (LogFile.Open(m_strFilePath, CFile::modeCreate|CFile::modeReadWrite|CFile::typeText|CFile::modeNoTruncate))
{
LogFile.SeekToEnd();
SYSTEMTIME st;
GetLocalTime(&st);
CString strText, strBackupTime;
strBackupTime.Format(_T("%04d%02d%02d%02d%02d%02d%03d"), st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);
strText.Format(_T("%04d-%02d-%02d %02d:%02d:%02d.%03d "), st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);
strText += strMsg;
strText += _T("\n");
LogFile.WriteString(strText);
ULONGLONG dFileSize = LogFile.GetLength();
if (dFileSize > 20*1024*1024)
{
LogFile.Close();
CString strNewFile;
strNewFile = m_strFilePath;
strBackupTime += _T(".log");
strNewFile.Replace(_T(".log"), strBackupTime.GetString());
CopyFile(m_strFilePath.GetString(), strNewFile.GetString(), FALSE);
DeleteFile(m_strFilePath.GetString());
}
else
{
LogFile.Close();
}
}
LeaveCriticalSection(&m_csLock);
}
private:
CRITICAL_SECTION m_csLock;
CString m_strFilePath;
};
标签:02d,Code,FNAME,MAX,st,strFilePath,Way,Output,LogFile 来源: https://www.cnblogs.com/yongchao/p/13276336.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。