C++下的简单Logtrace的编写

#include
#include

class LOG
{
public:
 LOG();
 virtual ~LOG();
public:
 static CString  GetLogFile();
 static short  SetLogFile(LPCTSTR strPath);
 static short  ViewLogFile();  

 static short  SetPrefix(LPCTSTR strPrefix);

 static CString  sOutV(LPCTSTR strType, LPCTSTR strFormat = NULL, va_list valist = NULL);
 static CString  sOut0(LPCTSTR strType, LPCTSTR strFormat = NULL,...);
 static CString  sOut ( LPCTSTR strFormat = NULL,...); 

 static short  OutV(LPCTSTR strType, LPCTSTR strFormat = NULL, va_list valist = NULL);
 static short  Out0(LPCTSTR strType, LPCTSTR strFormat = NULL,...);
 static short  Out (LPCTSTR strFormat = NULL,...);

protected:
 static CString  s_strLogFile;
 static CString  s_strLogPrefix;
 static HANDLE  s_hWriteEvent;
};

 

//

#include "LogTrace.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

 

// 得到可执行程序所在目录
//  BOOL bIncludeSep -- 是否包含最后的分隔符"/"

CString GetExePath(BOOL bIncludeSep)
{
 // 得到当前的文件名
 CString strFileName;   
 GetModuleFileName(GetModuleHandle(0) ,strFileName.GetBuffer(_MAX_PATH),_MAX_PATH);
 strFileName.ReleaseBuffer();   
 
 // 得到当前目录
 strFileName=strFileName.Left(strFileName.ReverseFind(_T('//'))+1); 
 
 if(bIncludeSep) 
  return strFileName;
 else  return strFileName.Left(strFileName.GetLength()-1);
}//-获取最后的文件名 如果给定文件不是全路径,就是相对于exe-

CString GetFileForExePath(LPCTSTR strCurFileName)
{
 CString strPath = strCurFileName;  
 if(!strPath.IsEmpty())  {
  //-相对路径-      
  if(strPath.Find(_T(":"))<=0)           
  {              
   strPath.Format(_T("%s%s"), GetExePath(FALSE), strCurFileName);
  }     
 }      
 return strPath; 
}  

#define LOG_EVENT _T("ChyLogWrite")
CString LOG::s_strLogFile = _T(""); 
CString LOG::s_strLogPrefix = _T(""); 
HANDLE LOG::s_hWriteEvent = NULL; 

LOG::LOG() 
{

}   

LOG::~LOG() 
{

}   

short LOG::SetLogFile(LPCTSTR strPath)
{
 if(strPath==NULL || strPath[0]==0)
  s_strLogFile = GetFileForExePath(L"log.log");
 else  s_strLogFile = GetFileForExePath(strPath);
 return 1;
}

CString LOG::GetLogFile()
{
 return s_strLogFile;
}

short LOG::ViewLogFile()
{
 CString strLogFile = GetLogFile();
 ShellExecute(NULL, _T("open"), strLogFile, NULL, NULL, SW_SHOW);
 return strLogFile.IsEmpty()?0:1;
}

short LOG::SetPrefix(LPCTSTR strPrefix)
{
 if(strPrefix && strPrefix[0])
 { 
  s_strLogPrefix = strPrefix;
 } 
 return 1;
}

CString LOG::sOutV(LPCTSTR strType, LPCTSTR strFormat, va_list valist)

 CString   strPart_Prefix;
 if(!s_strLogPrefix.IsEmpty())
 { 
  strPart_Prefix.Format(_T("[%s]"), s_strLogPrefix);
 } 
 CString   strPart_Time;
 {  SYSTEMTIME sysTime  = {0};
 GetLocalTime(&sysTime); 
 strPart_Time.Format(_T("[%2d-%2d %2d:%2d:%2d_%3d]"),   
  sysTime.wMonth, sysTime.wDay,    
  sysTime.wHour, sysTime.wMinute, sysTime.wSecond,   
  sysTime.wMilliseconds);  
 } 
 
 CString   strPart_Type;
 if(strType && strType[0])
 { 
  strPart_Type.Format(_T("[%s]"), strType);
 } 
 
 CString   strPart_Info;
 {    
  strPart_Info.FormatV(strFormat, valist);
 }  
 
 CString str = strPart_Prefix + strPart_Time + strPart_Type+ strPart_Info;
 
 return str;
}

CString LOG::sOut0(LPCTSTR strType, LPCTSTR strFormat,...)
{
 va_list  valist;
 va_start(valist, strFormat);    
 CString strInfo = sOutV(strType, strFormat, valist);
 va_end(valist);  return strInfo;
}

CString LOG::sOut(LPCTSTR strFormat,...)
{
 va_list  valist;
 va_start(valist, strFormat);
 CString strInfo = sOutV(NULL, strFormat, valist);
 va_end(valist); 
 
 return strInfo;
}

short LOG::OutV(LPCTSTR strType, LPCTSTR strFormat, va_list valist)

{
 //--
 if(s_hWriteEvent==NULL)
 { 
  s_hWriteEvent = OpenEvent(0, FALSE,LOG_EVENT); 
  if(s_hWriteEvent==NULL)  
   s_hWriteEvent = CreateEvent(NULL, FALSE, TRUE, LOG_EVENT); 
 }
 WaitForSingleObject(s_hWriteEvent, INFINITE);

 //-打开关闭文件-
 if(s_strLogFile.IsEmpty())  SetLogFile(NULL);
 CStdioFile file;
 if(file.Open(s_strLogFile, CFile::modeCreate|CFile::modeNoTruncate|CFile::modeWrite))
 {
  CString  strPart_NewLine = _T("/n"); 
  CString  strInfo = sOutV(strType, strFormat, valist);   
  CString  str = strPart_NewLine + strInfo; 
  file.SeekToEnd(); 
  file.WriteString(str); 
  file.Close();
 } 
 SetEvent(s_hWriteEvent);
 return 1;
}

short LOG::Out0(LPCTSTR strType, LPCTSTR strFormat,...)
{
 va_list  valist;
 va_start(valist, strFormat);    
 short rtn = OutV(strType, strFormat, valist);
 va_end(valist); 
 return rtn;
}

short LOG::Out(LPCTSTR strFormat,...)
{
 va_list  valist;
 va_start(valist, strFormat);
 short rtn = OutV(NULL, strFormat, valist);
 va_end(valist);
 return rtn;
}

 

/

 

使用,在要产生log的地方

 

#include "LogTrace.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif

 

然后使用:

   LOG::Out0(L"debug", L"hello");
   LOG::Out0(L"warn5", L"hello %d", 25);
   LOG::Out0(L"warn3", L"hello %s, you have %d apples!", L"libai", 10);
   LOG::Out (L"hello %s, you have %d apples!", L"libai", 10);

 

就可以了。

你可能感兴趣的:(C++下的简单Logtrace的编写)