带缓存的log写出类

      感觉CStdioFile的写文件很慢,工作中我经常需要写log文件,就写了个带buffer的文件写出类,直接调用_write等系统函数;

但根据实际测试结果,竟然和cstdiofile效率差不多,伤心!

 

     代码列如下,哪位大虾能指导下小弟,怎样能提高效率?谢谢啦!

  1. // BufferLog.h: interface for the CBufferLog class.
  2. /
  3. #if !defined(AFX_BUFFERLOG_H__0F83C182_4C70_44B0_9F53_D66F43E55FD8__INCLUDED_)
  4. #define AFX_BUFFERLOG_H__0F83C182_4C70_44B0_9F53_D66F43E55FD8__INCLUDED_
  5. #if _MSC_VER > 1000
  6. #pragma once
  7. #endif // _MSC_VER > 1000
  8. #include <io.h>
  9. const DWORD BIT_1MB = 1024 * 1024; //1MB
  10. const UCHAR CHAR_LN[] = {'/r''/n'}; //换行符
  11. class CBufferLog  
  12. {
  13. public
  14.     CBufferLog(DWORD dwBufferSize = BIT_1MB);//默认缓存1024个字符
  15.     virtual ~CBufferLog();
  16.     virtual void WriteLog( const void* lpBuf, DWORD dwCount );//写log到缓存
  17.     inline virtual void WriteLogLn( const void* lpBuf, DWORD dwCount )//写一行(自动加换行符)到缓存
  18.     { WriteLog(lpBuf, dwCount); WriteLog(CHAR_LN, 2); }
  19.     inline virtual void WriteLogLn( )//写入一空行
  20.     { WriteLog(CHAR_LN, 2); }
  21.     inline void WriteString(const CString& sz){this->WriteLog((LPCTSTR)sz, sz.GetLength());}
  22.     BOOL Open( LPCTSTR lpszFileName, bool Apped = falsebool Create = true );//打开log文件,Append是否覆盖已有内容,Create不存在时是否创建
  23.     void Close( ); //关闭log文件
  24.     DWORD GetPosition();//获取文件指针位置
  25.     inline LPCTSTR GetFilePath() const { return this->m_Path; }
  26.     inline LONG SeekToEnd(){return _lseek( this->m_Handle, 0L, SEEK_END ); }
  27.     inline LONG SeekToBegin(){return _lseek( this->m_Handle, 0L, SEEK_SET); }
  28. protected:
  29.     inline virtual void Flush(){ this->Write(this->m_dwCurPos); }//输出缓冲区内容
  30.     
  31.     inline virtual void Write(DWORD dwLen) //写缓冲区内容到磁盘
  32.     { _write(this->m_Handle, this->m_Buffer, dwLen); }
  33.     DWORD m_BufferSize;             //缓存尺寸
  34.     char* m_Buffer;                 //缓存
  35.     DWORD m_dwCurPos;               //当前缓冲区指针位置
  36.     DWORD m_dwFreeLen;              //可用缓冲数 
  37.     int   m_Handle;                 //file handle
  38.     CHAR  m_Path[MAX_PATH];         //file path 
  39. };
  40. #endif // !defined(AFX_BUFFERLOG_H__0F83C182_4C70_44B0_9F53_D66F43E55FD8__INCLUDED_)
CPP:
  1. // BufferLog.cpp: implementation of the CBufferLog class.
  2. //
  3. //
  4. #include "stdafx.h"
  5. #include "BufferLog.h"
  6. #include <fcntl.h>
  7. #include <sys/stat.h>
  8. #ifdef _DEBUG
  9. #undef THIS_FILE
  10. static char THIS_FILE[]=__FILE__;
  11. #define new DEBUG_NEW
  12. #endif
  13. //
  14. // Construction/Destruction
  15. //
  16. CBufferLog::CBufferLog(DWORD dwBufferSize)
  17. {
  18.     ASSERT(dwBufferSize != 0);
  19.     this->m_BufferSize = dwBufferSize;
  20.     this->m_dwCurPos = 0;
  21.     this->m_dwFreeLen = dwBufferSize;
  22.     this->m_Buffer = new char[m_BufferSize + 1];//最后位0保持不变
  23.     memset(this->m_Buffer, 0, m_BufferSize + 1);
  24.     memset(m_Path, 0, MAX_PATH);
  25.     this->m_Handle = 0;
  26. }
  27. BOOL CBufferLog::Open( LPCTSTR lpszFileName, 
  28.                       bool Apped /*= false*/
  29.                       bool Create /*= true*/ )
  30. {
  31.     if (this->m_Handle > 0)
  32.     {
  33.         _ASSERTE(("The file isn't closed, can't open two times!/n", NULL));
  34.         return FALSE;
  35.     }
  36.     int nFlag = _O_TEXT | _O_RDWR;
  37.     int nMode = _S_IREAD | _S_IWRITE;
  38.     nFlag |= Apped ? _O_APPEND : _O_TRUNC;
  39.     nFlag |= Create ? _O_CREAT : 0;
  40.     if (-1 == 
  41.         (this->m_Handle = _open(lpszFileName, nFlag, nMode)) )//文件打开失败
  42.     {
  43.         this->m_Handle = 0;
  44.         return FALSE;
  45.     }
  46.     else
  47.     {
  48.         strncpy(this->m_Path, lpszFileName, MAX_PATH - 1);//store the file path
  49.         return TRUE;
  50.     }
  51. }
  52. void CBufferLog::Close( )
  53. {
  54.     if (this->m_Buffer && this->m_dwCurPos > 0)
  55.     {
  56.         memset(this->m_Buffer + this->m_dwCurPos, 0, this->m_dwFreeLen);//剩余空间用0替补
  57.         this->Flush();      
  58.         
  59.         this->m_dwCurPos = 0;
  60.         this->m_dwFreeLen = this->m_BufferSize;
  61.         
  62.         memset(this->m_Buffer, 0, this->m_BufferSize);
  63.         this->Write(1);//写个0
  64.     }
  65.     if (this->m_Handle > 0)
  66.     {
  67.         _close(this->m_Handle);
  68.         this->m_Handle = 0;
  69.     }
  70. }
  71. CBufferLog::~CBufferLog()
  72. {
  73.     //关闭文件
  74.     this->Close();
  75.     if (this->m_Buffer)
  76.     {
  77.         delete []m_Buffer;
  78.         this->m_Buffer = NULL;
  79.     }
  80. }
  81. void CBufferLog::WriteLog(const void *lpBuf, DWORD dwCount)
  82. {
  83.     if (m_dwFreeLen >= dwCount)
  84.     {
  85.         memcpy(this->m_Buffer + this->m_dwCurPos, lpBuf, dwCount);
  86.         this->m_dwCurPos += dwCount;
  87.         this->m_dwFreeLen -= dwCount;
  88.     }
  89.     else//比缓冲区剩余空间大
  90.     {
  91.         DWORD dwLeft = dwCount; //剩余数量
  92.         while (dwLeft > 0)
  93.         {   
  94.             if (m_dwFreeLen >= dwLeft)//缓冲区够用
  95.             {
  96.                 memcpy(this->m_Buffer + this->m_dwCurPos, 
  97.                     (BYTE*)lpBuf + dwCount - dwLeft, dwLeft);
  98.                 this->m_dwCurPos += dwLeft;
  99.                 this->m_dwFreeLen -= dwLeft;
  100.                 dwLeft = 0;
  101.                 break;
  102.             }
  103.             else//缓冲区不够
  104.             {
  105.                 memcpy(this->m_Buffer + this->m_dwCurPos, 
  106.                     (BYTE*)lpBuf + dwCount - dwLeft, m_dwFreeLen);              
  107.                 
  108.                 this->m_dwCurPos += m_dwFreeLen;
  109.                 this->Flush();//缓冲区已满,输出
  110.                 dwLeft -= m_dwFreeLen;  //未处理数量
  111.                 this->m_dwCurPos = 0;
  112.                 this->m_dwFreeLen = this->m_BufferSize;//缓冲区全部可用
  113.             }
  114.         }
  115.     }
  116. }
  117. DWORD CBufferLog::GetPosition()
  118. {
  119.     this->Flush();
  120.     this->m_dwCurPos = 0;
  121.     this->m_dwFreeLen = this->m_BufferSize;//缓冲区全部可用
  122.     return _tell(this->m_Handle);
  123. }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值