VC 中使用Log 记录文件

一、log.h 源码

#pragma once

//===============================================================
// LOG::简单的日志工具
// 日志功能包应该包括以下两块功能:
// 1. 每个日志文本串
// 目前日志文本串包括 [prefix][datetime][type][info...]
// a.多个进程输出到同一个日志文件,可通过prefix来区分每个进程; 可以忽略
// b.datetime可以用来测试时间
// c.类型可以设定[error|warn|debug],
// 由使用者任意使用,并不做明确标记; 也可以忽略
// d.info,可以根据实际情况任意格式化输出,类似printf
// 2. 日志文件
// a. 可将日志串保存到文本,或者自己处理
// b. 支持并发写操作
// c. 日志文件的管理
//===============================================================

class LOG
{
public:
	LOG();
	virtual ~LOG();

public:
	//-日志文件-
	//----如果没有指定,则为exe所在路径下的log.log文件----
	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;
};


二、log.cpp 源码

#include "StdAfx.h"
#include "LOG.h"

// 得到可执行程序所在目录
// BOOL bIncludeSep -- 是否包含最后的分隔符"\"
CString GetExePath(BOOL bIncludeSep)
{
	// 得到当前的文件名
	CString strFileName;
	GetModuleFileName(AfxGetInstanceHandle(), 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(_T("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 vc 记录
个人分类: VC/C++
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭