您的位置:首页 > 其它

ziji

2015-08-18 16:14 260 查看
#pragma once#include #include #include #include #include #include using namespace std;#define AGENTLOG(format,...) CLog::getInstance()->AgentLog(format,__VA_ARGS__);#define AGENTLOG2(format,...) CLog::getInstance()->test(format,__VA_ARGS__);#define AGENTLOG1(format,...)
{\char theloggg[4096]; \memset(theloggg, 0, 4096); \char formatbuf[1024]; \memset(formatbuf, 0, 1024); \sprintf_s(formatbuf,"%s:","%s"); \strcat_s(formatbuf, sizeof(formatbuf), format); \sprintf_s(theloggg, formatbuf, /*__DATE__,*/ /*__TIME__*/ __FUNCTION__,
##__VA_ARGS__); \MessageBoxA(NULL,theloggg,NULL,NULL); \}class CLog{public:void test(TCHAR * format ,...){TCHAR destBuf[1024];va_list arg_ptr = NULL; va_start(arg_ptr, format); //wprintf(formatBuf,format);wvsprintf(destBuf,format, arg_ptr);wprintf(destBuf);va_end(arg_ptr);}void
AgentLog(TCHAR* format,...){TCHAR theLoggg[2048];TCHAR formatBuf[1024];memset(formatBuf, 0, 1024*sizeof(wchar_t));wsprintf(formatBuf,_T("%s:"),__FUNCTIONW__);//wcscat_s(formatBuf,sizeof(formatBuf),format);_tcscat_s(formatBuf,1024,format);va_list arg_ptr =
NULL; va_start(arg_ptr, format); wvsprintf(theLoggg, formatBuf, arg_ptr); CLog::outPut(theLoggg);va_end(arg_ptr);}private:CLog(void);//单例模式的唯一对象static CLog* g_Instance;//文件输出流wofstream* m_pFout;//msg计数int m_iMsgCount;//应用程序目录wstring m_strCurrentExePath;CRITICAL_SECTION
m_cs;public:wstring m_strLogEdit;private://新建日志文件void createNewLog();void theOutPut(wstring msg);public:~CLog(void);//唯一实例生成函数static CLog* getInstance();//实例删除static void DeleteInstance();static void outPut(wstring msg);LPCTSTR getCurrentEXEPath();};#include
"StdAfx.h"#include "Log.h"#define MAX_NEWFILE 2000#define MAX_FLUSHFILE 5CLog* CLog::g_Instance = 0;CLog::CLog(void){m_pFout=0;InitializeCriticalSection(&m_cs);CLog::getCurrentEXEPath();//创建Log目录wstring logPath=m_strCurrentExePath+_T("\\log");CreateDirectory(logPath.c_str(),NULL);createNewLog();}CLog::~CLog(void){if(m_pFout==0)return;m_pFout->flush();m_pFout->close();delete
m_pFout;m_pFout = 0;}CLog* CLog::getInstance(){if(g_Instance==0)g_Instance = new CLog();return g_Instance;}void CLog::DeleteInstance(){if(g_Instance==0)return;//判断有没有打开的ostream指针if (g_Instance->m_pFout!=0){g_Instance->m_pFout->flush();g_Instance->m_pFout->close();delete
g_Instance->m_pFout;g_Instance->m_pFout=0;}delete g_Instance;g_Instance = 0;}void CLog::createNewLog(){try{if (m_pFout!=0){m_pFout->flush();m_pFout->close();delete m_pFout;m_pFout=0;}m_iMsgCount=0;time_t tt;struct tm *p;time(&tt);p=localtime(&tt);TCHAR filename[260];memset(filename,0,260);wsprintf(filename,_T("%s\\log\\%d-%d-%d_%d-%d-%d_nbox.log\0"),m_strCurrentExePath.c_str(),p->tm_year+1900,p->tm_mon+1,p->tm_mday,p->tm_hour,p->tm_min,p->tm_sec);m_pFout=new
wofstream();locale::global(locale(""));//将全局区域设为操作系统默认区域m_pFout->open(filename,ios::out);locale::global(locale("C"));//还原{return ;}}catch (exception e){}}LPCTSTR CLog::getCurrentEXEPath(){try{if (m_strCurrentExePath.length() > 0)return m_strCurrentExePath.c_str();TCHAR
szEexFullpath[MAX_PATH];//得到EXE所有目录(包含"\")//得到程序的模块句柄HMODULE hMyModule = GetModuleHandle(NULL);//得到程序的路径DWORD dwLen = GetModuleFileName(hMyModule, szEexFullpath, MAX_PATH);//计算程序的路径(倒数查找最后一个"\")TCHAR * pPatch = &szEexFullpath[dwLen]; //szexepatch + dwLen;while
(pPatch != NULL && *pPatch != '\\'){pPatch--;}*pPatch=0;m_strCurrentExePath = wstring(szEexFullpath);}catch (...){}return m_strCurrentExePath.c_str();}void CLog::outPut(wstring msg){//std::cout << msg;EnterCriticalSection(&getInstance()->m_cs);getInstance()->theOutPut(msg);LeaveCriticalSection(&getInstance()->m_cs);}void
CLog::theOutPut(wstring msg){if(m_pFout==0)return;time_t timep;struct tm *p;time(&timep);p=localtime(&timep);TCHAR filename[260];memset(filename,0,260);wsprintf(filename,_T("%d:%d:%d -- "),p->tm_hour,p->tm_min , p->tm_sec);msg = wstring(filename) + msg;//add
chenshuaigetInstance()->m_strLogEdit = getInstance()->m_strLogEdit + _T("\r\n") + msg;m_iMsgCount++;*m_pFout << msg << _T("\r\n");if(m_iMsgCount % MAX_FLUSHFILE ==0)m_pFout->flush();if(m_iMsgCount % MAX_NEWFILE ==0)createNewLog();}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: