现在的位置: 首页 > 自动控制 > 工业·编程 > 正文

VC中一个输出日志的工具类LogFile

2013-12-19 11:15 工业·编程 ⁄ 共 3149字 ⁄ 字号 暂无评论

使用方法:包含头文件后调用静态成员即可,支持unicode。

// LogFile.h: interface for the CLogFile class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_LOGFILE_H__288388CA_9A3E_4F3D_A2B8_F1078E1F6A6B__INCLUDED_)
#define AFX_LOGFILE_H__288388CA_9A3E_4F3D_A2B8_F1078E1F6A6B__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

class CLogFile 
{
public:
    CLogFile();
    ~CLogFile();
private:
    static CString GetFileName();
    static CString GetFilePath();
public:
    static bool WriteLog(CString LogText);
};

#endif // !defined(AFX_LOGFILE_H__288388CA_9A3E_4F3D_A2B8_F1078E1F6A6B__INCLUDED_)

// LogFile.cpp: implementation of the CLogFile class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "LogFile.h"
//
//#ifdef _DEBUG
//#undef THIS_FILE
//static char THIS_FILE[]=__FILE__;
//#define new DEBUG_NEW
//#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CLogFile::CLogFile()
{

}

CLogFile::~CLogFile()
{

}
//获取文件名称
CString CLogFile::GetFileName()
{
    CString sFileName;

    sFileName = CTime::GetCurrentTime().Format("%Y-%m-%d") + TEXT(".log");
   
    return sFileName;
}
//获取应用程序所在路径
CString CLogFile::GetFilePath()
{  
    CString m_FilePath;

    GetModuleFileName(NULL,m_FilePath.GetBufferSetLength(MAX_PATH+1),MAX_PATH);

    m_FilePath.ReleaseBuffer();

    int m_iPosIndex;

    m_iPosIndex = m_FilePath.ReverseFind('\\');

    m_FilePath = m_FilePath.Left(m_iPosIndex) + TEXT("\\Log");

    return m_FilePath;
}

bool CLogFile::WriteLog(CString LogText)
{
    try
    {
        //CFile m_File;
        CFile m_SFile;
        CFileFind m_FileFind;
        CString m_sErrorMessage;
        CString m_sFileName = GetFileName();
        CString m_sFilePath = GetFilePath();
        CString m_sCurrentTime = CTime::GetCurrentTime().Format("%Y-%m-%d %X");
       
       
        if(!m_FileFind.FindFile(m_sFilePath))
        {
            CreateDirectory(m_sFilePath,NULL);
        }
       
        m_SFile.Open(m_sFilePath + TEXT("\\") +m_sFileName,CFile::modeCreate |CFile::modeNoTruncate| CFile::modeReadWrite |CFile::shareDenyWrite);
        /*{
            m_SFile.Open(m_sFilePath + TEXT("\\") + m_sFileName,CFile::modeCreate | CFile::modeReadWrite |CFile::shareDenyWrite| CFile::typeText);
        }*/
       
        m_SFile.SeekToEnd();

        if(sizeof(TCHAR)==sizeof(WCHAR))
        {
            WORD wSignature = 0xFEFF;
        m_SFile.Write(&wSignature, 2);
        }       

        //TCHAR* m_szMessage;

        //m_szMessage=m_sErrorMessage.GetBuffer();

        m_sErrorMessage = TEXT("*******************") + m_sCurrentTime + TEXT("*******************")+TEXT("\r\n") ;
        m_SFile.Write(m_sErrorMessage,m_sErrorMessage.GetLength()*sizeof(TCHAR));

        //m_sErrorMessage = LogText ;
        LogText+=TEXT("\r\n");
        m_SFile.Write(LogText,LogText.GetLength()*sizeof(TCHAR));
        //m_sErrorMessage = TEXT("*******************") + m_sCurrentTime + TEXT("*******************") ;
        m_SFile.Write(m_sErrorMessage,m_sErrorMessage.GetLength()*sizeof(TCHAR));

        //m_SFile.Write(m_sErrorMessage.GetBuffer(),m_sErrorMessage.GetLength());  

        m_SFile.Close();
    }
    catch(CFileException fileException)
    {
        return false;
    }

    return true;
}

给我留言

留言无头像?