writelog 打log

来源:互联网 发布:数控编程视频教学视频 编辑:程序博客网 时间:2024/05/29 03:23

我会打log了,自己写个writelog方法,然后在希望调用的地方writelog一下,参数是你想要传达的文字,跟print类似。

调用CStdioFile类是比较简单的方法,自定义WriteLog方法,再调用CStdioFile方法即可。

void WriteLog(LPCTSTR logName, CString msg)
{
    try
    {
        //设置文件的打开参数
        CStdioFile outFile(logName, CFile::modeNoTruncate | CFile::modeCreate | CFile::modeWrite | CFile::typeText);
        CString msLine;
        CTime tt = CTime::GetCurrentTime();

        //作为Log文件,经常要给每条Log打时间戳,时间格式可自由定义,
        //这里的格式如:2010-June-10 Thursday, 15:58:12

        msLine = tt.Format("[%Y-%B-%d %A, %H:%M:%S] ") + msg;
        msLine += "\n";

        //在文件末尾插入新纪录
        outFile.SeekToEnd();
        outFile.WriteString( msLine );
        outFile.Close();
    }
    catch(CFileException *fx)
    {
        fx->Delete();
    }
}

http://blog.163.com/tangzch_t/blog/static/4307443520105103595142/