编程知识 cdmana.com

Method of creating dump crash log in QT windows application

Debug.h file

#pragma once 

#include <iostream>
#include <string>

#include <QApplication>
#include <QString>
#include <QDir>
#include <QDateTime>

#if defined(Q_OS_WIN32)   // Q_OS_WIN32
#include <windows.h>
#endif

namespace ysm{
   
   

	class Debug {
   
   

	public:
#if defined(Q_OS_WIN32) // 		#if defined(_MSC_VER) && _MSC_VER >= 1400
	//  Run back 
	static LONG WINAPI  crashStackCallback(struct _EXCEPTION_POINTERS* exceptionInfo);
#endif

	};
}

Debug.cpp file

#include "debug.h"

#include <QApplication>
#include <QString>
#include <QDir>
#include <QDateTime>

#if defined(Q_OS_WIN32)   // Q_OS_WIN32
#include <Dbghelp.h>
//#pragma comment(lib, "Dbghelp.lib")
#endif

namespace ysm {
   
   



#if defined(Q_OS_WIN32)

	LONG WINAPI Debug::crashStackCallback(struct _EXCEPTION_POINTERS* exceptionInfo) {
   
   
		//QCoreApplication::applicationDirPath();
		//QApplication *app = QCoreApplication::app();
		QString savePath = QCoreApplication::applicationDirPath() + "/dump/";
		QDir dir(savePath);
		if(!dir.exists() && !dir.mkpath(savePath)) {
   
   
			//app->exit(E_UNEXPECTED);
			return EXCEPTION_EXECUTE_HANDLER;
		}

		savePath.append("assit_");
		savePath.append(QDateTime::currentDateTime().toString("yyyyMMddhhmmsszzz"));
		savePath.append(".dmp");

		HANDLE dump = CreateFileW(savePath.toStdWString().c_str(), GENERIC_WRITE,
								  0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
		if(INVALID_HANDLE_VALUE == dump) {
   
   
			//app->exit(E_UNEXPECTED);
			return EXCEPTION_EXECUTE_HANDLER;
		}

		MINIDUMP_EXCEPTION_INFORMATION miniDumpExceptionInfo;
		miniDumpExceptionInfo.ExceptionPointers = exceptionInfo;
		miniDumpExceptionInfo.ThreadId = GetCurrentThreadId();
		miniDumpExceptionInfo.ClientPointers = TRUE;
		DWORD idProcess = GetCurrentProcessId();
		MiniDumpWriteDump(GetCurrentProcess(), idProcess, dump,
						  MiniDumpWithFullMemory, &miniDumpExceptionInfo, NULL, NULL);

		CloseHandle(dump);

		//app->exit(E_UNEXPECTED);
		return EXCEPTION_EXECUTE_HANDLER;
	}
	
#endif


}

MainWindow.cpp

#include "Debug.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
   
   
    ui->setupUi(this);

#if defined(Q_OS_WIN32)
	SetUnhandledExceptionFilter(Debug::crashStackCallback);
#endif

}

版权声明
本文为[osc_ boqyoaed]所创,转载请带上原文链接,感谢
https://cdmana.com/2020/12/20201224082723255d.html

Scroll to Top