win下生成core文件

#ifndef __CCOREMGR__H_
#define __CCOREMGR__H_
#include "singleton.h"
#include "CLogmanager.h"

#if defined(WIN32) || defined(_WIN32)
#include<Windows.h>  
#include<DbgHelp.h>  
#pragma comment(lib,"DbgHelp.lib") 
#elif  __linux__
#endif

class CCoreMgr : public CSingleton<CCoreMgr>{
    SINGLE_CLASS_INITIAL(CCoreMgr);
public:
    ~CCoreMgr();

#if defined(WIN32) || defined(_WIN32)
    static void CreateDumpFile(LPCWSTR lpstrDumpFilePathName, EXCEPTION_POINTERS *pException);
    static LONG ApplicationCrashHandler(EXCEPTION_POINTERS *pException);
#endif

public:
    int Init();
    int UnInit();

private:
    bool mbInit;
};

#define sCCoreMgr CCoreMgr::Instance()
#endif


#include "CoreMgr.h"

CCoreMgr::CCoreMgr(){
    mbInit = false;
}

CCoreMgr::~CCoreMgr(){
}

#if defined(WIN32) || defined(_WIN32)
void CCoreMgr::CreateDumpFile(LPCWSTR lpstrDumpFilePathName, EXCEPTION_POINTERS *pException) {
    fprintf(stderr, "Create CoreDump\n");
    HANDLE hDumpFile = CreateFile(lpstrDumpFilePathName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
    // Dump信息  
    MINIDUMP_EXCEPTION_INFORMATION dumpInfo;
    dumpInfo.ExceptionPointers = pException;
    dumpInfo.ThreadId = GetCurrentThreadId();
    dumpInfo.ClientPointers = TRUE;
    // 寫入Dump文件內容  
    MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), hDumpFile, MiniDumpNormal, &dumpInfo, NULL, NULL);
    CloseHandle(hDumpFile);
}

LONG CCoreMgr::ApplicationCrashHandler(EXCEPTION_POINTERS *pException) {
    struct systemtime_t stNow = get_now_time();
    char szFileName[1000];
    safe_snprintf(szFileName, 1000, _TRUNCATE, "%02d-%02d-%02d-%02d-%02d-%02d.dmp", stNow.tmyear, stNow.tmmon, stNow.tmmday, stNow.tmhour, stNow.tmmin, stNow.tmsec);
    CCoreMgr::CreateDumpFile(L"AnalySisSvr.dmp", pException);
    return EXCEPTION_EXECUTE_HANDLER;
}
#endif

int CCoreMgr::Init() {
#if defined(WIN32) || defined(_WIN32)
    fprintf(stderr, "Init Core\n");
    SetUnhandledExceptionFilter((LPTOP_LEVEL_EXCEPTION_FILTER)CCoreMgr::ApplicationCrashHandler);
    mbInit = true;
#elif  __linux__
#endif
    return 0;
}

int CCoreMgr::UnInit() {
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章