본문 바로가기
IT/C++

[MFC] 심플한 로그 함수

by Spring Up!! 2016. 10. 18.
반응형
#include "stdafx.h"

namespace DEBUGLOG
{
    CString LogPath;
}

int WriteLog(LPCTSTR pFormat, ...)
{
    TCHAR Buff[1024] = {0,};
    va_list arg;
    va_start(arg, pFormat);
    _vstprintf_s(Buff, _countof(Buff) - 1, pFormat, arg);
    va_end(arg);

    SYSTEMTIME lpSystemTime;
    GetLocalTime(&lpSystemTime);
    COleDateTime CurSysTime(lpSystemTime);
   
    CString TimedLog;
    TimedLog.Format(_T("[%s] ", CurSysTime.Format(_T("%Y%m%d %H:%M:%S")));
    TimedLog.AppendFormat(_T("%s\r\n"), Buff);
   
    CStdioFile StdFile;
    StdFile.Open(DEBUGLOG::LogPath, CFile::modeCreate | CFile::modeNoTruncate | CFile::modeWrite);
    StdFile.SeekToEnd();
    StdFile.WriteString(TimedLog);
    StdFile.Close();

    return 0;
}

int CreateLog()
{
    SYSTEMTIME lpSystemTime;
    GetLocalTime(&lpSystemTime);
    COleDateTime CurSysTime(lpSystemTime);

    DEBUGLOG::LogPath.Format(_T("%s\\myapp.log"), GetAppPath(), CurSysTime.Format(_T("%Y%m%d")));
    CStdioFile StdFile;
    StdFile.Open(DEBUGLOG::LogPath, CFile::modeCreate | CFile::modeNoTruncate | CFile::modeWrite);
    StdFile.SeekToEnd();
    StdFile.WriteString(CString(_T("Logging Start")));
    StdFile.Close();
}

CString GetAppPath()
{
    TCHAR szTemp[256] = {0,};
    ::GetModuleFileName(NULL, szTemp, 256);
    CString Path = szTemp;
    if (0 < Path.ReverseFind('\\'))
    {
        Path = Path.Left(Path.ReversFind('\\'));
    }
}
반응형

댓글