LockHandle

来源:互联网 发布:心目中最好av作品知乎 编辑:程序博客网 时间:2024/06/05 02:38

////LockHandle.h

// LockHandle.h: interface for the CLockHandle class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_LOCKHANDLE_H__71E66089_A1D2_4460_8FFE_A146A8C9557E__INCLUDED_)
#define AFX_LOCKHANDLE_H__71E66089_A1D2_4460_8FFE_A146A8C9557E__INCLUDED_

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

#include <afxmt.h>

//加锁句柄,在该对象结束时自动解锁
class CLockHandle
{
public:
 CLockHandle(CCriticalSection *plock);
 virtual ~CLockHandle();
private:
 CLockHandle(const CLockHandle&);
 CCriticalSection *m_lock;
 CLockHandle & operator = (const CLockHandle &);
};


template<typename T> class SmartPtr
{
public:
 SmartPtr(T *p=0):ptr(p),pUse(new size_t(1)){}
 SmartPtr(const SmartPtr &src):ptr(src.ptr),pUse(src.pUse)
 {
  ++*pUse;
 }
 SmartPtr& operator=(const SmartPtr &rhs)
 {
  ++*rhs.pUse;
  decrUse();
  ptr=rhs.ptr;
  pUse=rhs.pUse;
  return* this;
 }
 T* operator->()
 {
  if(ptr)
   return ptr;
  throw CMyException("access through NULL pointer");
 }
 const T* operator->()const
 {
  if(ptr)
   return ptr;
  throw CMyException("access through NULL pointer");
 }
 T& operator*()
 {
  if(ptr)
   return*ptr;
  throw CMyException("dereference of NULL pointer");
 }
 const T& operator*() const
 {
  if(ptr)
   return *ptr;
  throw CMyException("dereference of NULL pointer");
 }
 ~SmartPtr()
 {
  decrUse();
 }
private:
 void decrUse()
 {
  if(--*pUse==0)
  {
   delete ptr;
   delete pUse;
  }
 }
 T* ptr;
 size_t* pUse;
};
#endif // !defined(AFX_LOCKHANDLE_H__71E66089_A1D2_4460_8FFE_A146A8C9557E__INCLUDED_)

 

 

 

////LockHandle.cpp

// LockHandle.cpp: implementation of the CLockHandle class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "LockHandle.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

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

CLockHandle::CLockHandle(CCriticalSection *plock)
{
 m_lock=plock;
 m_lock->Lock();//加锁
}

CLockHandle::~CLockHandle()
{
 m_lock->Unlock();//解锁
}

 ////LogFile.h

 

#ifndef LOGFILE_H
#define LOGFILE_H
#include "LockHandle.h"
#pragma once
//一次写入日志文件允许的最大字符数
const UINT WRITE_MAX_SIZE = 1024;

class CLogFile
{
public:
 CLogFile(void);
 ~CLogFile(void);

public:
 //写入日志
 void WriteLog(const CString strData);

private:
 //获得执行文件路径
 CString GetExecutePath();

 //创建日志文件
 void CreateLogFile();

 //关闭日志文件
 void CloseLogFile();

 long  GetFileLength(CString strpath);

private:
 //日志文件指针
 FILE *pLogFile;

 //日志文件路径
 CString m_logFileName;


 //日志锁
 CCriticalSection m_criticalLogFile;

};
#endif

//外部写日志文件
void WriteFileLog(const CString strWrite);

#define WRITELOG WriteFileLog

 

 

////LogFile.cpp

#include "StdAfx.h"
#include "LogFile.h"
#include <Windows.h>
#include "LockHandle.h"
#include "shlwapi.h"
#define  FILE_SIZE 10485760
#define  FILE_AMOUNT 1024
CLogFile::CLogFile(void)
{
 pLogFile = NULL;
 CreateLogFile();
}

CLogFile::~CLogFile(void)
{
 CloseLogFile();
}

/************************************************************************
Description : 获得执行文件路径
InputParam  : void
OutPutParam : void
Return      : CString执行文件出去文件名部分路径
************************************************************************/
CString CLogFile::GetExecutePath()
{
 char chTempPath[MAX_PATH] = {0};
 GetModuleFileName(NULL, chTempPath,MAX_PATH);

 CString strTempPath = chTempPath;
 strTempPath.MakeReverse();
 //取子串
 strTempPath = strTempPath.Mid(strTempPath.Find(TEXT("\\")) + 1);
 strTempPath.MakeReverse();

 //追加日志文件名
 strTempPath += TEXT("\\");
 strTempPath += TEXT("logs");
 if(!PathFileExists(strTempPath))
 {
  CreateDirectory(strTempPath, NULL);
 }

 return strTempPath;
}

/************************************************************************
Description : 创建日志文件
InputParam  : void
OutPutParam : void
Return      : void
************************************************************************/
void CLogFile::CreateLogFile()
{
 CString strPath = GetExecutePath();
 //创建日志文件夹
 //CreateDirectory(strPath, NULL);

 //获得当前时间
 //SYSTEMTIME st;
 //GetLocalTime(&st);
 //CString strTime;
 //strTime.Format(TEXT("%2u %2u %2u %2u %2u %2u"), st.wYear, st.wMonth, st.wDay, st.wHour,
 // st.wMinute, st.wSecond);

 //创建日志文件
 strPath += TEXT("\\");

 /*strPath += strTime;*/
 //CTime m_currentTime=CTime::GetCurrentTime();
 //CString m_CurrentTime;
 //m_CurrentTime.Format(_T("%04d%02d%02d%02d%02d%02d"),
 // m_currentTime.GetYear(),
 // m_currentTime.GetMonth(),
 // m_currentTime.GetDay(),
 // m_currentTime.GetHour(),
 // m_currentTime.GetMinute(),
 // m_currentTime.GetSecond()); 

 strPath += TEXT("FileWatcher_"); 
 for(int i=0;i<FILE_AMOUNT;i++)
 {
  CString tmpStrPath = strPath;
  char buf[10];
  itoa(i,buf,10);
  tmpStrPath += buf;
  tmpStrPath += ".log";
  if(PathFileExists(tmpStrPath))
  {
   if(GetFileLength(tmpStrPath) < FILE_SIZE)
   {
    strPath = tmpStrPath;
    break;
   }
  }
  else
  {
   strPath = tmpStrPath;
   break;
  }
  //tmpStrPath = strPath;
 }
 //strPath += m_CurrentTime;
 m_logFileName = strPath;
 fopen_s(&pLogFile, strPath, "a+");
 if (NULL != pLogFile)
 {
  fclose(pLogFile);
  pLogFile = NULL;
 }
 
}

/************************************************************************
Description : 写入文件
InputParam  : const CString strData写入内容
OutPutParam : void
Return      : void
************************************************************************/
void CLogFile::WriteLog(const CString strData)
{
 CLockHandle lockLogFile(&m_criticalLogFile);
 if(GetFileLength(m_logFileName) > FILE_SIZE)
 {
  CreateLogFile();
 }
 //写入文件
 fopen_s(&pLogFile, m_logFileName, "a+");  
 if (NULL != pLogFile)
 {
  char buff[WRITE_MAX_SIZE] = {0};
  strcpy_s(buff, strData.GetLength()+1, strData);
  fwrite(buff, strData.GetLength(), 1, pLogFile);
  //写入换行
  strcpy_s(buff, sizeof(TEXT("\n")), TEXT("\n"));
  fwrite(buff, sizeof("\n"), 1, pLogFile);
  fclose(pLogFile);
  pLogFile = NULL;
 }
}

/************************************************************************
Description : 关闭日志文件
InputParam  : void
OutPutParam : void
Return      : void
************************************************************************/
void CLogFile::CloseLogFile()
{
 if (NULL != pLogFile)
 {
  fclose(pLogFile);
  pLogFile = NULL;
 }
}

long CLogFile::GetFileLength(CString strpath)
{
 FILE *fp;
 if((fp=fopen(strpath,"r"))==NULL)
  return 0;
 fseek(fp,0,SEEK_END);
 long FileSize = ftell(fp);
 fclose(fp);
 return FileSize; 
}

//日志文件对象
CLogFile logFile;

/************************************************************************
Description : 外部写日志文件
InputParam  : const CString strWrite要写入的内容
OutPutParam : void
Return      : void
************************************************************************/
void WriteFileLog(const CString strWrite)
{
 try
 {
  logFile.WriteLog(strWrite);
 }
 catch (...)
 {
  return;
 }
}

 

 

 

 

 

原创粉丝点击