首页 > 代码库 > C++ 写Log
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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。