标签:c++ 写log
class MYLog { public: ~MYLog(void); static MYLog * Log(); bool OpenLogFile(const string &sFilePath); void Message(const string &sFileName,const string &sFunc,const long &lLine,const string &sMessage); private: static MYLog *m_pInstance; MYLog(void); void GetNowTime(); ofstream m_fout; string m_sFilePath; string m_sNowTiem; string m_sFileSavePath; string m_LastDate; }; #include "stdafx.h" #include "MYLog.h" //单例静态指针初始化 MYLog * MYLog:: m_pInstance = new MYLog(); /*********************************** 此函数不会被调用 ***********************************/ MYLog::MYLog(void) { } /*********************************** 析构单例指针 ***********************************/ MYLog::~MYLog(void) { if(NULL != m_pInstance) { delete m_pInstance; } m_fout.close(); } /*********************************** 获得单例实例 ***********************************/ MYLog * MYLog:: Log() { return m_pInstance; } /*********************************** 打开Log文件 ***********************************/ bool MYLog::OpenLogFile(const string &sFilePath) { if(true == m_fout.is_open()) { m_fout.close(); } m_sFileSavePath = sFilePath; if(m_sFileSavePath.find_last_of("\\") != m_sFileSavePath.length() -1 && m_sFileSavePath.find_last_of("/") != m_sFileSavePath.length() -1) { m_sFileSavePath += "/"; } time_t t = time(0); char tmp[64]; strftime( tmp, sizeof(tmp), "%Y-%m-%d",localtime(&t) ); string sTemp = tmp; m_LastDate = tmp; m_sFilePath = m_sFileSavePath + sTemp + ".txt" ; m_fout.open(m_sFilePath.c_str(),ios::app); if(NULL == m_fout) { return false; } return true; } /********************************** 获取当前时间 **********************************/ void MYLog::GetNowTime() { time_t t = time(0); char tmp[64]; strftime( tmp, sizeof(tmp), "%Y/%m/%d %X ",localtime(&t) ); m_sNowTiem = tmp; strftime( tmp, sizeof(tmp), "%Y-%m-%d",localtime(&t) ); string sData = tmp; if(sData != m_LastDate) { OpenLogFile(m_sFileSavePath); } } /*********************************** 将信息写入Log文件 ***********************************/ void MYLog::Message(const string &sFile,const string &sFunc,const long &lLine,const string &sMessage) { GetNowTime(); string sFileName = sFile; sFileName = sFileName.substr(sFileName.find_last_of("\\")+1,sFileName.length()-sFileName.find_last_of("\\")-1); m_fout<<m_sNowTiem.c_str()<<" FILE: "<<sFileName.c_str()<<" FUNC: "<<sFunc.c_str()<<" LINE: "<<lLine<<" MES: "<<sMessage.c_str()<<endl; m_fout.flush(); } //DLL 导出函数 extern "C" _declspec(dllexport) bool LogPath(const string &sFilePath) { bool isSucess = MYLog::Log()->OpenLogFile(sFilePath); return isSucess; } extern "C" _declspec(dllexport) void WRITELOG(const string &sFile,const string &sFunc,const long &lLine,const string &sMessage) { MYLog::Log()->Message(sFile,sFunc,lLine,sMessage); }
调用
#include "stdafx.h" typedef void (*LOGFUN)(const string &sFile,const string &sFunc,const long &lLine,const string &sMessage); LOGFUN MYLOG; //typedef void (*LOGFUN)(const string &sFunc,const string &sMessage); //extern LOGFUN MYLOG; void FuncB() { MYLOG(__FILE__,__FUNCTION__,__LINE__,"It is in FuncB"); } int _tmain(int argc, _TCHAR* argv[]) { char c; HINSTANCE hInput; hInput = LoadLibrary(_T("LogDll.dll")); if(NULL == hInput) { cout<<"load failed"<<endl; c = getchar(); return -1; } typedef bool (*FUNC)(const string &sFilePath); FUNC fun = (FUNC)GetProcAddress(hInput,"LogPath"); if(NULL == fun) { cout<<"load function failed"<<endl; c = getchar(); return -1; } bool iResult = fun("c://"); cout<<"result is"<<iResult<<endl; MYLOG = (LOGFUN)GetProcAddress(hInput,"WRITELOG"); if(NULL == MYLOG) { cout<<"load function failed"<<endl; c = getchar(); return -1; } while(true) { cout<<"wrtite..."<<endl; MYLOG(__FILE__,__FUNCTION__,__LINE__,"This is a test"); FuncB(); Sleep(5000); } c = getchar(); return 0; return 0; }
本文出自 “风清扬song” 博客,请务必保留此出处http://2309998.blog.51cto.com/2299998/1423797
标签:c++ 写log
原文地址:http://2309998.blog.51cto.com/2299998/1423797