一些写log文件的类

wangbo7801 2009-10-30 04:25:52
最近由于调试程序,所以写了些关于log的类。主要功能为输出调试信息到log文件,该类能按照一定的缩进格式输出程序运行轨迹,在VC6下调试通过,代码如下:

.h文件

class NElapseTime
{
public:
NElapseTime(bool bStart = true)
{
m_begin = 0;
m_end = 0;
if (bStart) Start();
}

~NElapseTime(){}
public:
void Start()
{
m_begin = clock();
m_end = m_begin;
}

void Stop()
{
m_end = clock();
}

long GetTime()
{
return (long)(m_end-m_begin);
}

private:
clock_t m_begin;
clock_t m_end;
};

class NLogFile
{
public:
NLogFile();
NLogFile(const char * strFileName, int nMaxChar = 1024, bool bInitFile = false);
virtual ~NLogFile();

void Initialize(const char * strFileName, int nMaxChar = 1024, bool bInitFile = false);
bool Print(const char * strFormat, ...);
bool vPrint(const char * strFormat, va_list vl);
void Indent();
void UnIndent();

protected:
inline bool Create();
inline bool Open();
inline bool Close();
inline void Destory();

protected:
int m_hFile;
CRITICAL_SECTION m_CritivalSection;
char m_strFileName[_MAX_PATH];
char* m_strContent;
int m_nMaxChar;
bool m_bReady;

static unsigned int m_nIndent;
};

class NGlobalLogFile : public NLogFile
{
public:
virtual ~NGlobalLogFile(){}

static NGlobalLogFile* instance()
{
static NGlobalLogFile logfile;
return &logfile;
}
protected:
NGlobalLogFile(){}
};

class NFunctionLog
{
public:
NFunctionLog(const char * strFunName);
NFunctionLog(NLogFile* pLogFile,const char * strFunName);
virtual ~NFunctionLog();

void Mark(const char* strFormat,...);

protected:
void Init(NLogFile* pLogFile,const char * strFunName);
protected:
NElapseTime m_elapseTime;
char m_strFunName[_MAX_PATH];
NLogFile * m_pLogFile;
};




.cpp文件

#include <fcntl.h>
#include <sys/stat.h>
#include <io.h>
#include <share.h>
#include <stdio.h>

unsigned int NLogFile::m_nIndent = 0;

NLogFile::NLogFile()
:m_hFile(-1),m_bReady(false),m_strContent(NULL)
{
InitializeCriticalSection(&m_CritivalSection);
}

NLogFile::NLogFile(const char * strFileName, int nMaxChar/* = 1024*/, bool bInitFile/* = false*/)
:m_hFile(-1),m_bReady(false),m_strContent(NULL)
{
InitializeCriticalSection(&m_CritivalSection);
}

NLogFile::~NLogFile()
{
EnterCriticalSection(&m_CritivalSection);
Close();
LeaveCriticalSection(&m_CritivalSection);

DeleteCriticalSection(&m_CritivalSection);
delete []m_strContent;
}

void NLogFile::Initialize(const char * strFileName, int nMaxChar/* = 1024*/, bool bInitFile/* = false*/)
{
ASSERT(strFileName && nMaxChar > 0);

EnterCriticalSection(&m_CritivalSection);

Destory();

m_strContent = new char[nMaxChar];
m_nMaxChar = nMaxChar;
strcpy(m_strFileName,strFileName);

if(bInitFile)
{
char str[_MAX_PATH] = { 0 };
sprintf(str,"del %s",strFileName);
system(str);
}

m_bReady = true;

LeaveCriticalSection(&m_CritivalSection);
}

inline void NLogFile::Destory()
{
if (NULL != m_strContent)
{
delete []m_strContent;
m_strContent = NULL;
}

m_hFile = -1;
m_nMaxChar = 0;
m_bReady = false;
memset(m_strFileName,0,_MAX_PATH);
}

inline bool NLogFile::Create()
{
if (!m_bReady)
return false;

Close();

m_hFile = _sopen(m_strFileName, _O_TEXT | _O_RDWR | _O_APPEND | _O_CREAT, _SH_DENYNO, _S_IREAD | _S_IWRITE);

return (m_hFile != -1);
}

inline bool NLogFile::Open()
{
if (!m_bReady)
return false;

Close();

m_hFile = _sopen(m_strFileName, _O_TEXT | _O_RDWR | _O_APPEND, _SH_DENYNO, _S_IREAD | _S_IWRITE);

if (m_hFile == -1)
{
return Create();
}

return true;
}

inline bool NLogFile::Close()
{
if (!m_bReady)
return false;

bool bRet = true;

if (m_hFile != -1)
{
bRet = (_close(m_hFile) == 0);
m_hFile = -1;
}

return bRet;
}

bool NLogFile::Print(const char * strFormat, ...)
{
if (!m_bReady)
return false;

bool bRet = false;

va_list v;
va_start(v, strFormat);
bRet = vPrint(strFormat,v);
va_end(v);

return bRet;
}

bool NLogFile::vPrint(const char * strFormat, va_list vl)
{
if (!m_bReady)
return false;

bool bRet = false;

EnterCriticalSection(&m_CritivalSection);

memset(m_strContent,0,m_nMaxChar);
sprintf(m_strContent,"%*s",4*m_nIndent,"");
vsprintf(m_strContent+strlen(m_strContent),strFormat,vl);

if (Open())
{
bRet = (0 ==
_write(m_hFile,(const void*)m_strContent,strlen(m_strContent)));

Close();
}

LeaveCriticalSection(&m_CritivalSection);

return bRet;
}
void NLogFile::Indent()
{
m_nIndent++;
}

void NLogFile::UnIndent()
{
if(m_nIndent > 0) m_nIndent--;
}


NFunctionLog::NFunctionLog(const char * strFunName)
:m_pLogFile(NULL)
{
NLogFile* pLogFile = static_cast<NLogFile*>(NGlobalLogFile::instance());
Init(pLogFile,strFunName);
}

NFunctionLog::NFunctionLog(NLogFile* pLogFile,const char * strFunName)
:m_pLogFile(NULL)
{
Init(pLogFile,strFunName);
}

NFunctionLog::~NFunctionLog()
{
m_elapseTime.Stop();
m_pLogFile->UnIndent();
m_pLogFile->Print("<-- %s : %d (ms)\n",m_strFunName,m_elapseTime.GetTime());
}

void NFunctionLog::Init(NLogFile* pLogFile,const char * strFunName)
{
ASSERT(pLogFile && strFunName);

m_pLogFile = pLogFile;
strcpy(m_strFunName,strFunName);
m_elapseTime.Start();
m_pLogFile->Print("--> %s\n",m_strFunName);
m_pLogFile->Indent();
}

void NFunctionLog::Mark(const char* strFormat,...)
{
va_list v;
va_start(v, strFormat);
m_pLogFile->vPrint(strFormat,v);
va_end(v);
}

用法:

1.可以用NGlobalLogFile类,全局只有一个!
NGlobalLogFile::instance()Initialize("c:\\globallogfile.txt");

void fun()
{
NFunctionLog fl("fun");
fl.Mark("test\n");
}

输出文件格式为
--> fun
test
<-- fun : 0 (ms)

2.可以用NLogFile类
NLogFile log("c:\\logfile.txt");

void fun()
{
NFunctionLog fl(&log,"fun");
fl.Mark("test\n");
}




...全文
114 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
wangbo7801 2009-11-02
  • 打赏
  • 举报
回复
修改下就可以跨平台了!
老邓 2009-10-30
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 pcboyxhy 的回复:]
标准C或者标准C++可以很好完成的事情,又变成VC only了
[/Quote]
线程安全,没必要跨平台吧。
pcboyxhy 2009-10-30
  • 打赏
  • 举报
回复
标准C或者标准C++可以很好完成的事情,又变成VC only了
xingzhe2001 2009-10-30
  • 打赏
  • 举报
回复
jf
wanghao111 2009-10-30
  • 打赏
  • 举报
回复
谢谢分享
为什么要学习这门课程?·新一代流式数据湖技术组件深入讲解,帮助你快速构造数据湖知识体系。·为构建湖仓一体架构提供底层技术支撑。本课程将从原理、架构、底层存储细节、性能优化、管理等层面对Paimon流式数据湖组件进行详细讲解,原理+实战,帮助你快速上手使用数据湖技术。讲师介绍华为HCIP认证大数据高级工程师北京猎豹移动大数据技术专家中科院大数据研究院大数据技术专家51CTO企业IT学院优秀讲师电子工业出版社2022年度优秀作者出版书籍:《Flink入门与实战》、《大数据技术及架构图解实战派》。本课程提供配套课件、软件、试题、以及源码。课程内容介绍:1、什么是Apache Paimon2、Paimon的整体架构3、Paimon的核心特点4、Paimon支持的生态5、基于Flink SQL操作Paimon6、基于Flink DataStream API 操作Paimon7、Paimon中的内部表和外部表8、Paimon中的分区表和临时表9、Paimon中的Primary Key表(主键表)10、Paimon中的Append Only表(仅追加表)11、Changelog Producers原理及案例实战12、Merge Engines原理及案例实战13、Paimon中的Catalog详解14、Paimon中的Table详解15、Paimon之Hive Catalog的使用16、动态修改Paimon表属性17、查询Paimon系统表18、批量读取Paimon表19、流式读取Paimon表20、流式读取高级特性Consumer ID21、Paimon CDC数据摄取功能22、CDC之MySQL数据同步到Paimon23、CDC之Kafka数据同步到Paimon24、CDC高级特性之Schema模式演变25、CDC高级特性之计算列26、CDC高级特性之特殊的数据型映射27、CDC高级特性之中文乱码28、Hive引擎集成Paimon29、在Hive中配置Paimon依赖30、在Hive中读Paimon表31、在Hive中创建Paimon表32、Hive和Paimon数据型映射关系33、Paimon底层文件基本概念34、Paimon底层文件布局35、Paimon底层文件操作详解36、Flink流式入Paimon表过程分析37、读性能优化详细分析38、Paimon中快照、分区、小文件的管理39、管理标签(自动管理+手工管理)40、管理Bucket(创建+删除+回滚)

64,416

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧