对轻量级C++日志类的增强
这是网上找到的一个轻量级C++日志类的实现,主要应用了Singleton模式,我增加一个函数
tmWrite
,主要用来进行前面带时间的行输出。
//log.h
//hujinshan@2004.1.3 Airforce Engineering University
/*
//CLog* CLog::_instance = NULL;
CLog::GetObj().Write( "*******
初始化完成
*******" ) <<endl;
CLog::GetObj().tmWrite( "*******
开始渲染
No1. *******" ) <<endl ;
CLog::GetObj()<< "*******
注销钩子
*******" <<endl;
在
Clog
文件中有
#define CLog /##/
的语句,
这是为了使日志在
release
版无效,
使用单行注释,所以如果调用时如果要换行,
请务必在换行最后加上
/
符号
,
*/
/*#ifndef _DEBUG
#define CLog /##/
#define _CLOG_H
#endif*/
#ifndef
_CLOG_H
#define
_CLOG_H
#pragma
once
#include
<
fstream
.
h
>
#include
<
ctime
>
class
CLog
{
CLog
()
{
pf
=
new
ofstream
(
"inmstrace.log"
,
ios
::app);
}
static
CLog
*
_instance
;
ofstream
*
pf
;
public:
~
CLog
()
{
_instance
=0;
pf
->
close
();
}
static
CLog
*
GetPtr
()
{
if(!
_instance
)
_instance
=
new
CLog
;
return(
_instance
);
}
static
CLog
&
GetObj
()
{
if(!
_instance
)
_instance
=
new
CLog
;
return(*
_instance
);
}
template<
class
T
> inline
CLog
&
Write
(
T
val
)
{
(*
pf
) <<
val
;
pf
->
flush
();
return *this;
}
template<
class
T
> inline
CLog
&
tmWrite
(
T
val
)
{
char
strBuf
[50];
time_t
ltime
;
time
( &
ltime
);
struct tm *
today
=
localtime
( &
ltime
);
sprintf
(
strBuf
,
"%4d-%2.2d-%2.2d %2.2d:%2.2d:%2.2d "
,
today
->
tm_year
+1900,
today
->
tm_mon
+1,
today
->
tm_mday
,
today
->
tm_hour
,
today
->
tm_min
,
today
->
tm_sec
);
(*
pf
) <<
strBuf
<<
val
<<
"/r/n"
;
pf
->
flush
();
return *this;
}
template<
class
T
> inline
CLog
& operator<< (
T
val
)
{
(*
pf
) <<
val
;
pf
->
flush
();
return *this;
}
};
#endif
//end of file
转自:http://blog.csdn.net/stephenxu111/archive/2008/05/13/2442825.aspx