感觉CStdioFile的写文件很慢,工作中我经常需要写log文件,就写了个带buffer的文件写出类,直接调用_write等系统函数;
但根据实际测试结果,竟然和cstdiofile效率差不多,伤心!
代码列如下,哪位大虾能指导下小弟,怎样能提高效率?谢谢啦!
- // BufferLog.h: interface for the CBufferLog class.
- /
- #if !defined(AFX_BUFFERLOG_H__0F83C182_4C70_44B0_9F53_D66F43E55FD8__INCLUDED_)
- #define AFX_BUFFERLOG_H__0F83C182_4C70_44B0_9F53_D66F43E55FD8__INCLUDED_
- #if _MSC_VER > 1000
- #pragma once
- #endif // _MSC_VER > 1000
- #include <io.h>
- const DWORD BIT_1MB = 1024 * 1024; //1MB
- const UCHAR CHAR_LN[] = {'/r', '/n'}; //换行符
- class CBufferLog
- {
- public:
- CBufferLog(DWORD dwBufferSize = BIT_1MB);//默认缓存1024个字符
- virtual ~CBufferLog();
- virtual void WriteLog( const void* lpBuf, DWORD dwCount );//写log到缓存
- inline virtual void WriteLogLn( const void* lpBuf, DWORD dwCount )//写一行(自动加换行符)到缓存
- { WriteLog(lpBuf, dwCount); WriteLog(CHAR_LN, 2); }
- inline virtual void WriteLogLn( )//写入一空行
- { WriteLog(CHAR_LN, 2); }
- inline void WriteString(const CString& sz){this->WriteLog((LPCTSTR)sz, sz.GetLength());}
- BOOL Open( LPCTSTR lpszFileName, bool Apped = false, bool Create = true );//打开log文件,Append是否覆盖已有内容,Create不存在时是否创建
- void Close( ); //关闭log文件
- DWORD GetPosition();//获取文件指针位置
- inline LPCTSTR GetFilePath() const { return this->m_Path; }
- inline LONG SeekToEnd(){return _lseek( this->m_Handle, 0L, SEEK_END ); }
- inline LONG SeekToBegin(){return _lseek( this->m_Handle, 0L, SEEK_SET); }
- protected:
- inline virtual void Flush(){ this->Write(this->m_dwCurPos); }//输出缓冲区内容
- inline virtual void Write(DWORD dwLen) //写缓冲区内容到磁盘
- { _write(this->m_Handle, this->m_Buffer, dwLen); }
- DWORD m_BufferSize; //缓存尺寸
- char* m_Buffer; //缓存
- DWORD m_dwCurPos; //当前缓冲区指针位置
- DWORD m_dwFreeLen; //可用缓冲数
- int m_Handle; //file handle
- CHAR m_Path[MAX_PATH]; //file path
- };
- #endif // !defined(AFX_BUFFERLOG_H__0F83C182_4C70_44B0_9F53_D66F43E55FD8__INCLUDED_)
- // BufferLog.cpp: implementation of the CBufferLog class.
- //
- //
- #include "stdafx.h"
- #include "BufferLog.h"
- #include <fcntl.h>
- #include <sys/stat.h>
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- //
- // Construction/Destruction
- //
- CBufferLog::CBufferLog(DWORD dwBufferSize)
- {
- ASSERT(dwBufferSize != 0);
- this->m_BufferSize = dwBufferSize;
- this->m_dwCurPos = 0;
- this->m_dwFreeLen = dwBufferSize;
- this->m_Buffer = new char[m_BufferSize + 1];//最后位0保持不变
- memset(this->m_Buffer, 0, m_BufferSize + 1);
- memset(m_Path, 0, MAX_PATH);
- this->m_Handle = 0;
- }
- BOOL CBufferLog::Open( LPCTSTR lpszFileName,
- bool Apped /*= false*/,
- bool Create /*= true*/ )
- {
- if (this->m_Handle > 0)
- {
- _ASSERTE(("The file isn't closed, can't open two times!/n", NULL));
- return FALSE;
- }
- int nFlag = _O_TEXT | _O_RDWR;
- int nMode = _S_IREAD | _S_IWRITE;
- nFlag |= Apped ? _O_APPEND : _O_TRUNC;
- nFlag |= Create ? _O_CREAT : 0;
- if (-1 ==
- (this->m_Handle = _open(lpszFileName, nFlag, nMode)) )//文件打开失败
- {
- this->m_Handle = 0;
- return FALSE;
- }
- else
- {
- strncpy(this->m_Path, lpszFileName, MAX_PATH - 1);//store the file path
- return TRUE;
- }
- }
- void CBufferLog::Close( )
- {
- if (this->m_Buffer && this->m_dwCurPos > 0)
- {
- memset(this->m_Buffer + this->m_dwCurPos, 0, this->m_dwFreeLen);//剩余空间用0替补
- this->Flush();
- this->m_dwCurPos = 0;
- this->m_dwFreeLen = this->m_BufferSize;
- memset(this->m_Buffer, 0, this->m_BufferSize);
- this->Write(1);//写个0
- }
- if (this->m_Handle > 0)
- {
- _close(this->m_Handle);
- this->m_Handle = 0;
- }
- }
- CBufferLog::~CBufferLog()
- {
- //关闭文件
- this->Close();
- if (this->m_Buffer)
- {
- delete []m_Buffer;
- this->m_Buffer = NULL;
- }
- }
- void CBufferLog::WriteLog(const void *lpBuf, DWORD dwCount)
- {
- if (m_dwFreeLen >= dwCount)
- {
- memcpy(this->m_Buffer + this->m_dwCurPos, lpBuf, dwCount);
- this->m_dwCurPos += dwCount;
- this->m_dwFreeLen -= dwCount;
- }
- else//比缓冲区剩余空间大
- {
- DWORD dwLeft = dwCount; //剩余数量
- while (dwLeft > 0)
- {
- if (m_dwFreeLen >= dwLeft)//缓冲区够用
- {
- memcpy(this->m_Buffer + this->m_dwCurPos,
- (BYTE*)lpBuf + dwCount - dwLeft, dwLeft);
- this->m_dwCurPos += dwLeft;
- this->m_dwFreeLen -= dwLeft;
- dwLeft = 0;
- break;
- }
- else//缓冲区不够
- {
- memcpy(this->m_Buffer + this->m_dwCurPos,
- (BYTE*)lpBuf + dwCount - dwLeft, m_dwFreeLen);
- this->m_dwCurPos += m_dwFreeLen;
- this->Flush();//缓冲区已满,输出
- dwLeft -= m_dwFreeLen; //未处理数量
- this->m_dwCurPos = 0;
- this->m_dwFreeLen = this->m_BufferSize;//缓冲区全部可用
- }
- }
- }
- }
- DWORD CBufferLog::GetPosition()
- {
- this->Flush();
- this->m_dwCurPos = 0;
- this->m_dwFreeLen = this->m_BufferSize;//缓冲区全部可用
- return _tell(this->m_Handle);
- }