c++ 多线程写日志的一个很实用的日志类源码(支持 c++ builder)
2011-11-05 11:04
316 查看
1.日志基类
.h文件基类cpp文件
2.日志派生类
.h文件#ifndefLogFileExH
#defineLogFileExH
#include<assert.h>
#include"UnitLogWriter.h"
//---------------------------------------------------------------------------
classLogFileEx:publicLogFile
{
protected:
char*_szPath;
char_szLastDate[9];
int_iType;
voidSetPath(constchar*szPath);
public:
enumLOG_TYPE{YEAR=0,MONTH=1,DAY=2};
LogFileEx(constchar*szPath=".",LOG_TYPEiType=MONTH);
~LogFileEx();
constchar*GetPath();
voidLog(LPCVOIDlpBuffer,DWORDdwLength);
voidLog(constchar*szText);
voidLog(constAnsiString&szText);
private://屏蔽函数
LogFileEx(constLogFileEx&);
LogFileEx&operator=(constLogFileEx&);
};
#endif
#pragmahdrstop
#include"LogFileEx.h"
//---------------------------------------------------------------------------
#pragmapackage(smart_init)
//-------------------------------------------------------------------------
voidLogFileEx::SetPath(constchar*szPath)
{
assert(szPath);
WIN32_FIND_DATAwfd;
chartemp[MAX_PATH+1]={0};
if(FindFirstFile(szPath,&wfd)==INVALID_HANDLE_VALUE&&CreateDirectory(szPath,NULL)==0)
{
strcat(strcpy(temp,szPath),"CreateFail.ExitNow!ErrorID:");
ltoa(GetLastError(),temp+strlen(temp),10);
MessageBox(NULL,temp,"ClassLogFileEx",MB_OK);
exit(1);
}
else
{
GetFullPathName(szPath,MAX_PATH,temp,NULL);
_szPath=newchar[strlen(temp)+1];
assert(_szPath);
strcpy(_szPath,temp);
}
}
//-------------------------------------------------------------------------
LogFileEx::LogFileEx(constchar*szPath,LOG_TYPEiType)
{
_szPath=NULL;
SetPath(szPath);
_iType=iType;
memset(_szLastDate,0,9);
}
//-------------------------------------------------------------------------
LogFileEx::~LogFileEx()
{
if(_szPath)
delete[]_szPath;
}
//-------------------------------------------------------------------------
constchar*LogFileEx::GetPath()
{
return_szPath;
}
//-------------------------------------------------------------------------
voidLogFileEx::Log(LPCVOIDlpBuffer,DWORDdwLength)
{
assert(lpBuffer);
chartemp[10];
staticconstcharformat[3][10]={"%Y","%Y-%m","%Y%m%d"};
__try
{
Lock();
time_tnow=time(NULL);
strftime(temp,9,format[_iType],localtime(&now));
if(strcmp(_szLastDate,temp)!=0)//更换文件名
{
strcat(strcpy(_szFileName,_szPath),"\\");
strcat(strcat(_szFileName,temp),".log");
strcpy(_szLastDate,temp);
Close();
}
if(!OpenFile())
return;
WriteLog(lpBuffer,dwLength);
}
__finally
{
Unlock();
}
}
//-------------------------------------------------------------------------
voidLogFileEx::Log(constchar*szText)
{
Log(szText,strlen(szText));
}
//-------------------------------------------------------------------------
voidLogFileEx::Log(constAnsiString&szText)
{
Log(szText.c_str(),szText.Length());
}
3.随便测试的代码
#include<vcl.h>
#include<conio.h>
#include"LogFileEx.h"
#pragmahdrstop
//---------------------------------------------------------------------------
#pragmaargsused
intmain(intargc,char*argv[])
{
LogFileExlog;
log.Log("哈哈");
AnsiStringtemp="adsfsadfsadfsaf";
log.Log(temp);
log.Log(temp);
getch();
return0;
}
//---------------------------------------------------------------------------
相关文章推荐
- 【源码下载】分享一个支持自安装自卸载的Windows服务
- 使用不同的C++支持库的模块混合开发时,引发异常展开不正常,抛异常竟引出一个SIGSEGV
- 笔试题11:编写一个生产者与消费者的多线程例子(源码)
- C++多线程框架(一)--------- new一下就启动一个线程
- 一个简单的C/C++多线程
- VC-Logger 是一个简单易用的 C++ 程序通用日志组件
- 一个c++拼图游戏源码
- 单例模式的多线程安全支持(C++)
- 多线程写到一个日志文件中
- C#写文本日志帮助类(支持多线程)改进版(不适用于ASP.NET程序)
- 一个AJAX自动完成功能的js封装源码[支持中文]第1/2页
- 使用Xcode阅读Makefile管理的C/C++项目源码(支持调试,符号跳转,自动补全)
- c++日志输出库 spdlog 简介(3)多线程控制台输出日志
- C++的一个日志类,和log4cplus很像
- 支持多线程的日志记录类实现
- log4net写入日志文件示例时一个奇怪的错误(附源码下载)
- 用Winform 利用多线程做的一个网站压力测试小工具(带源码)
- 一个C++实现的md5源码(转)
- VS2010 + .net4.0 一个小程序锁屏软件 支持开机自启动 源码 免积分下载
- 为什么C++没有内建支持多线程