日志类

.h文件

#pragma once
#ifndef _RCLOGINFO_H
#define _RCLOGINFO_H


#include <fstream>
#include <string>
#include <sstream>
#include <ctime>


using namespace std;
class __declspec(dllexport) Log
{
public:
Log();
~Log();


bool Open(string sFileName);
void Close();


bool CommonLogInit(string path, wstring EvidName = NULL); //打开默认的log 文件


void Enable();
void Disable();
string GetTimeStr();
string GetTimeName();
wstring s2ws(const std::string& s);
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();
}
string ValueToStr(int value);
private:
ofstream m_tOLogFile;//写操作(输出)的文件类 


bool m_bEnabled;
};




#endif






.CPP文件

#include "RcLogInfo.h"

Log::Log()
:m_bEnabled(true)
{
}


Log::~Log()
{
}
std::string ws2s(const std::wstring& ws)
{
std::string curLocale = setlocale(LC_ALL, NULL);        // curLocale = "C";
setlocale(LC_ALL, "chs");
const wchar_t* _Source = ws.c_str();
size_t _Dsize = 2 * ws.size() + 1;
char *_Dest = new char[_Dsize];
memset(_Dest, 0, _Dsize);
wcstombs(_Dest, _Source, _Dsize);
std::string result = _Dest;
delete[]_Dest;
setlocale(LC_ALL, curLocale.c_str());
return result;
}
wstring Log::s2ws(const std::string& s)
{


setlocale(LC_ALL, "chs");
const char* _Source = s.c_str();
size_t _Dsize = s.size() + 1;
wchar_t *_Dest = new wchar_t[_Dsize];
wmemset(_Dest, 0, _Dsize);
mbstowcs(_Dest, _Source, _Dsize);
std::wstring result = _Dest;
delete[]_Dest;
setlocale(LC_ALL, "C");
return result;
}


string Log::ValueToStr(int value)
{
ostringstream ost;
if (10>value)
{
ost << "0" << value;//补0
}
else
{
ost << value;
}
return ost.str();
}
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 path,string EvidName)
{
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)+"_"+ValueToStr(tLocalTime->tm_hour)+
ValueToStr(tLocalTime->tm_min)+ValueToStr(tLocalTime->tm_sec);
if (EvidName.empty())
{ //path指定的文件夹必须存在,不然无法输出日志
return Open(path + sDateStr );//如果文件名带".txt",则补0会被自动删除
}
else
{
return Open(path + EvidName);//指定生成文件放置路径
}

}


void Log::Enable()
{
m_bEnabled = true;
}


void Log::Disable()
{
m_bEnabled = false;
}


//得到当前时间的字符串
string Log::GetTimeStr()
{
time_t tNowTime;
time(&tNowTime);


tm* tLocalTime = localtime(&tNowTime);


//得到日期的字符串
string strDateTime = ValueToStr(tLocalTime->tm_year + 1900) + "-" +
ValueToStr(tLocalTime->tm_mon + 1) + "-" +
ValueToStr(tLocalTime->tm_mday) + " " +
ValueToStr(tLocalTime->tm_hour) + ":" +
ValueToStr(tLocalTime->tm_min) + ":" +
ValueToStr(tLocalTime->tm_sec);
return strDateTime;
}
string Log::GetTimeName()
{
time_t tNowTime;
time(&tNowTime);


tm* tLocalTime = localtime(&tNowTime);


//得到日期的字符串
string strDateTime = ValueToStr(tLocalTime->tm_year + 1900) +
ValueToStr(tLocalTime->tm_mon + 1)  +
ValueToStr(tLocalTime->tm_mday) + "-"+
ValueToStr(tLocalTime->tm_hour) +
ValueToStr(tLocalTime->tm_min)  +
ValueToStr(tLocalTime->tm_sec) + " ";
return strDateTime;
}

注释:由于iostream的安全性问题,该方法并不适用多线程;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值