C++ 一个简单的log类的实现
2018-01-23 17:59
495 查看
c++有好多日志类库,今天在网上学习了一个简单的日志类库的写法。
直接看代码 :
Log.h文件:
Log.cpp文件:
#include "Log.h"
Log::Log()
:m_bEnabled(true)
{
}
Log::~Log()
{
}
bool Log::Open(string sFileName)
{
m_tOLogFile.open(sFileName.c_str(), ios_base::out | ios_base::app);
if (!m_tOLogFile)
{
return false;
}
return true;
}
void Log::Close()
{
if (m_tOLogFile.is_open())
{
m_tOLogFile.close();
}
}
bool Log::CommonLogInit(string name)
{
time_t tNowTime;
time(&tNowTime);
tm* tLocalTime = localtime(&tNowTime);
//得到日期的字符串
string sDateStr = ValueToStr(tLocalTime->tm_year + 1900) + "-" +
ValueToStr(tLocalTime->tm_mon + 1) + "-" +
ValueToStr(tLocalTime->tm_mday);
return Open( "Log\\"+name + sDateStr + ".txt");
}
void Log::Enable()
{
m_bEnabled = true;
}
void Log::Disable()
{
m_bEnabled = false;
}
//得到当前时间的字符串
string Log::GetTimeStr()
{
time_t tNowTime;
time(&tNowTime);
tm* tLocalTime = localtime(&tNowTime);
//"2011-07-18 23:03:01 ";
string strFormat = "%Y-%m-%d %H:%M:%S ";
char strDateTime[30] = { '\0' };
strftime(strDateTime, 30, strFormat.c_str(), tLocalTime);
string strRes = strDateTime;
return strRes;
}
使用实例main.cpp
#include "Log.h"
int main()
{
Log mainLog;
string a = "log";
mainLog.CommonLogInit(a);
mainLog << mainLog.GetTimeStr() << "这里是需要自己输入的信息" << endl;
}
直接看代码 :
Log.h文件:
#ifndef LOG_H #define LOG_H #include <fstream> #include <string> #include <sstream> #include <ctime> using namespace std; /** * 用于输出log文件的类. */ class Log { public: Log(); ~Log(); bool Open(string strFileName); void Close(); bool CommonLogInit(string name); //打开默认的log 文件 void Enable(); void Disable(); string GetTimeStr(); template <typename T> void LogOut(const T& value) { if (m_bEnabled) { m_tOLogFile << value; } } template <typename T> void LogOutLn(const T& value) { if (m_bEnabled) { m_tOLogFile << value << endl; } } void LogOutLn() { if (m_bEnabled) { m_tOLogFile << endl; } } template <typename T> Log& operator<<(const T& value) { if (m_bEnabled) { m_tOLogFile << value; } return (*this); } Log& operator<<(ostream& (*_Pfn)(ostream&)) { if (m_bEnabled) { (*_Pfn)(m_tOLogFile); } return (*this); } private: template<typename T> string ValueToStr(T value) { ostringstream ost; ost << value; return ost.str(); } private: ofstream m_tOLogFile; bool m_bEnabled; }; #endif
Log.cpp文件:
#include "Log.h"
Log::Log()
:m_bEnabled(true)
{
}
Log::~Log()
{
}
bool Log::Open(string sFileName)
{
m_tOLogFile.open(sFileName.c_str(), ios_base::out | ios_base::app);
if (!m_tOLogFile)
{
return false;
}
return true;
}
void Log::Close()
{
if (m_tOLogFile.is_open())
{
m_tOLogFile.close();
}
}
bool Log::CommonLogInit(string name)
{
time_t tNowTime;
time(&tNowTime);
tm* tLocalTime = localtime(&tNowTime);
//得到日期的字符串
string sDateStr = ValueToStr(tLocalTime->tm_year + 1900) + "-" +
ValueToStr(tLocalTime->tm_mon + 1) + "-" +
ValueToStr(tLocalTime->tm_mday);
return Open( "Log\\"+name + sDateStr + ".txt");
}
void Log::Enable()
{
m_bEnabled = true;
}
void Log::Disable()
{
m_bEnabled = false;
}
//得到当前时间的字符串
string Log::GetTimeStr()
{
time_t tNowTime;
time(&tNowTime);
tm* tLocalTime = localtime(&tNowTime);
//"2011-07-18 23:03:01 ";
string strFormat = "%Y-%m-%d %H:%M:%S ";
char strDateTime[30] = { '\0' };
strftime(strDateTime, 30, strFormat.c_str(), tLocalTime);
string strRes = strDateTime;
return strRes;
}
使用实例main.cpp
#include "Log.h"
int main()
{
Log mainLog;
string a = "log";
mainLog.CommonLogInit(a);
mainLog << mainLog.GetTimeStr() << "这里是需要自己输入的信息" << endl;
}
相关文章推荐
- binder 一个简单的c++服务的实现,与callback实现
- Linux C++ 一个线程池的简单实现(附代码)
- c++程序中写测试log到文件的简单实现
- C++学习:一个简单适用的跨平台Log类(1)
- C++实现一个简单的异常日志记录类
- 一个简单的游戏引擎核心状态机的C++实现
- 一个简单的C++的RTTI实现
- [转贴]从零开始学C++之STL(二):实现一个简单容器模板类Vec(模仿VC6.0 中 vector 的实现、vector 的容量capacity 增长问题)
- 【C++】一个简单栈的实现
- 一个简单的C++智能指针的实现
- C++自己实现一个简单地Vector
- 一个简单的多叉树C++实现
- 二路归并的一个C++最简单实现
- C++ 使用模版范式写一个单例模式的类--简单实现单例模式
- C++实现一个简单的异常日志记录类
- c++实现一个简单的空间配置器allocator
- 一个简单的C++的RTTI实现
- C++实现一个简单的双线程MVC框架
- C++实现的一个简单两个大数相加程序!
- 从零开始学C++之重载 operator new 和 operator delete 实现一个简单内存泄漏跟踪器