MFC 에서 파일로 날짜별 로그를 기록하는 방법 > 개발팁

본문 바로가기

사이트 내 전체검색

뒤로가기 개발팁

윈도우 MFC 에서 파일로 날짜별 로그를 기록하는 방법

본문

MFC 에서 개발할때, 로그 기록을 남겨야할 때가 있습니다. 

프로젝트 폴더의 log 폴더에 날짜별 로그기록을 파일로 남기는 방법은 아래와 같습니다.


함수 사용법은 아래와 같습니다.

클래스::fnLogData(_T("파일명"), _T("로그 메시지"));


함수는 다음과 같습니다.

void 클래스::fnLogData(CString strPart,CString strFmt, ...)

{

CFileStatus Status;

CFile file;

CString  strFileName = _T("");

CString  strLog = _T("");

CString  strMsg = _T("");

CTime time = CTime::GetCurrentTime();


strFileName.Format(_T("../Log/%s_%02d.txt"), strPart, time.GetDay());

if(CFile::GetStatus(strFileName, Status)) // File Exist

{   

if(Status.m_mtime.GetMonth() != time.GetMonth()) // Is not same, delete and create

{

if( !file.Open(strFileName,CFile::modeCreate | CFile::modeWrite) )

return;

#ifdef _UNICODE

BYTE UnicodeFlag[2] = {0xFF,0xFE};

file.Write(UnicodeFlag , 2 );

#endif

}

else

{

if( !file.Open(strFileName,CFile::modeWrite) )

return;

}


}

else // File not found

{

if( !file.Open(strFileName,CFile::modeCreate | CFile::modeWrite) )

return;

#ifdef _UNICODE

BYTE UnicodeFlag[2] = {0xFF,0xFE};

file.Write(UnicodeFlag , 2 );

#endif

}


va_list args;

va_start(args, strFmt);

strMsg.FormatV(strFmt, args);

va_end(args);

strMsg += _T("\r\n");

strLog = time.Format(_T("%Y-%m-%d %H:%M:%S"));

strLog += _T("  ") + strMsg;


file.SeekToEnd();

#ifdef _UNICODE

file.Write(strLog.GetBuffer(strLog.GetLength()) , strLog.GetLength()*2 );

#else

file.Write(strLog.GetBuffer(strLog.GetLength()) , strLog.GetLength() );

#endif

file.Close();

}





추천0 비추천 0

댓글목록 0

등록된 댓글이 없습니다.

Copyright © 소유하신 도메인. All rights reserved.

사이트 정보

회사명 : 회사명 / 대표 : 대표자명
주소 : OO도 OO시 OO구 OO동 123-45
사업자 등록번호 : 123-45-67890
전화 : 02-123-4567 팩스 : 02-123-4568
통신판매업신고번호 : 제 OO구 - 123호
개인정보관리책임자 : 정보책임자명

PC 버전으로 보기