뭐... 설명은.. 소스에 있는 코멘트로..
VS 2010 으로 했읍니다.
=====================
#include "stdafx.h"
static FILE* fP; //파일 포인터 추가
void CStr2Char(CString f, char *t){ // CString 을 char * 로 바꾸기
CStringA p2(f);
const char * p3=p2;
strcpy(t, (char *) p3);
}
void makePath(CString & s) { // 경로 만들고, 파일명 만들기(파일을 실제 만들지는 않고 이름만 리턴)
TCHAR path[_MAX_PATH];
GetModuleFileName(NULL, path, sizeof(path)); // 실행파일 경로 ?어 오기
s = path;
int i = s.ReverseFind('\\'); // 실행파일 이름을 지우기 위해서 왼쪽에 있는 '/'를 찾는다.
s = s.Left(i); // 뒤에 있는 현재 실행파일 이름을 지운다.
s.Format(_T("%s\\Log"), s); // AfxMessageBox(s);
CreateDirectory(s, NULL ); // 기본 경로
SYSTEMTIME st;
GetLocalTime(&st); // 시간, 날자 ?어 오기
s.Format(_T("%s\\%04d%02d"), s, st.wYear, st.wMonth); // AfxMessageBox(s);
CreateDirectory(s, NULL ); // 추가 경로
s.Format(_T("%s\\%02d%02d%02d.txt"),s, st.wYear, st.wMonth, st.wDay ); // Log File 이름 => 반환 할 거..
// AfxMessageBox(s);
}
bool OpenFile()
{
char strBuf[250];
CString strPath;
errno_t err;
makePath(strPath);
CStr2Char(strPath, strBuf);
if((err = fopen_s(&fP, strBuf, "a+")) != 0)
{
fP = NULL;
return FALSE;
}
else
{
ASSERT(fP != NULL);
setvbuf(fP, NULL, _IONBF, 0 );
}
return TRUE;
}
void CloseFile()
{
if(fP == NULL)
return;
ASSERT(fP != NULL);
fclose(fP);
fP=NULL;
}
void AddLog(char *LogType, char* format, ...)
{
char buf[512];
va_list va;
SYSTEMTIME st;
GetLocalTime(&st); // 시간, 날자 ?어 오기
// Excel 에서 볼 수 있도록 "," 를 구분자 사용. 파일명을 ~.csv 로 바꾸면 excel 에서 뜬다.
// LogType 이 있을때만 시간 정보 넣는다.
if(strlen(LogType)) {
sprintf(buf,"%02d:%02d:%02d:%03ld, %s,",
st.wHour, st.wMinute, st.wSecond, st.wMilliseconds, LogType);
} else {
sprintf(buf,",,");
}
va_start(va, format);
vsprintf_s(buf+strlen(buf), 512-strlen(buf), format, va);
va_end(va);
OpenFile();
if(fP!=NULL)
{
fprintf(fP, "%s\n", buf); // log 이니까 항상 끝에 줄바꿈 넣어줌.
}
CloseFile();
}
int Test_Log()
{
char tt[]="Level3";
int a=1, b=2;
AddLog("L1", "%d + %d = %d", a,b,a+b);
AddLog("", "%d + %d = %d", a,b,a+b);
AddLog(tt, "%d + %d = %d", a,b,a+b);
return 0;
}
=====================
'개발 > mfc' 카테고리의 다른 글
MFC vs2010 : Ontimer와 SetTimer, KillTimer (0) | 2023.09.06 |
---|---|
mfc , 자식 Dialog 만들고 Class 추가하기 - sample source (0) | 2023.09.03 |
mfc thread 만들기 - vs 2010, sample source (0) | 2023.09.03 |
mfc AfxMessageBox() 로, 문자, 숫자 표시 하기, sample source (0) | 2023.08.24 |
mfc vs2010 ini 파일 읽기 쓰기 간단 보기, sample source (0) | 2023.08.21 |