windows 如何让 QT 应用程序写入崩溃日志

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6978675/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 17:44:15  来源:igfitidea点击:

How to make QT Application write a crash log

c++windowsqt

提问by maxchirag

i am working on project developed in Qt, Windows, i use Qt Version 4.1.4 and VC6 IDE for compiling project, most of usage of product are on WindowsXp.

我正在从事在 Qt、Windows 中开发的项目,我使用 Qt 版本 4.1.4 和 VC6 IDE 来编译项目,产品的大部分使用都在 WindowsXp 上。

i frequently receive crash reports from testers and customers which does not have fix pattern and does not reproduce in my development environment.

我经常收到来自测试人员和客户的崩溃报告,这些报告没有修复模式,也不会在我的开发环境中重现。

so i come to the conclusion that i need to write a crash log from my program so that i always get the call stack when program is crashed.

所以我得出的结论是,我需要从我的程序中写入一个崩溃日志,以便在程序崩溃时我总能得到调用堆栈。

in linux i know its possible to do it with some signal handling of segmentation fault. but how to do the same thing in windows/C++?

在 linux 中,我知道可以通过一些分段错误的信号处理来做到这一点。但是如何在 windows/C++ 中做同样的事情?

i tried to search on Google to find some ready made solution but without any luck.

我试图在 Google 上搜索以找到一些现成的解决方案,但没有任何运气。

Suggestion regarding any third party tools/libraries (free or licensed) are also accepted, but please do not suggest any method where i have to modify each function in my module to get log, because the project is huge and i can not afford that.

也接受有关任何第三方工具/库(免费或许可)的建议,但请不要建议我必须修改模块中的每个功能以获取日志的任何方法,因为项目很大,我负担不起。

thanks in advance.

提前致谢。

updates. I have attached StackWalker to my Code now (with bit of hassle of _MBCS & UNICODE) but atleast managed to attach it.

更新。我现在已经将 StackWalker 附加到我的代码中(有点麻烦 _MBCS 和 UNICODE),但至少设法附加了它。

my main file looks like below,

我的主文件如下所示,

class MyStackWalker : public StackWalker
{  
   FILE *sgLogFile;
   public:
   MyStackWalker() : StackWalker() 
   {
     sgLogFile = fopen("ThisIsWhatIcallLog.log", "w");
   }
   MyStackWalker(DWORD dwProcessId, HANDLE hProcess) : StackWalker(dwProcessId, hProcess) 
   {
   }
   virtual void OnOutput(LPCSTR szText) 
   { 
      printf(szText); 
      fprintf(sgLogFile, "%s",szText);
      fclose(sgLogFile);
      StackWalker::OnOutput(szText); 
   }
};

LONG Win32FaultHandler(struct _EXCEPTION_POINTERS *  ExInfo)
{
    MyStackWalker sw;   
    sw.ShowCallstack();
    return EXCEPTION_EXECUTE_HANDLER;
}

void InstallFaultHandler()
{
    SetUnhandledExceptionFilter((LPTOP_LEVEL_EXCEPTION_FILTER) Win32FaultHandler);
}

But i get linker error now that symbols are not found,

但是由于找不到符号,我收到链接器错误,

main.obj : error LNK2001: unresolved external symbol "public: int __thiscall StackWalker::ShowCallstack(void *,struct _CONTEXT const *,int (__stdcall*)(void *,unsigned __int64,void *,int,unsigned long *,void *),void *)" (?ShowCallstack@StackWalker@@QAEHPAXPBU_CONTEXT@@P6GH0_K0HPAK0@Z0@Z)
main.obj : error LNK2001: unresolved external symbol "protected: virtual void __thiscall StackWalker::OnDbgHelpErr(char const *,int,unsigned __int64)(?OnDbgHelpErr@StackWalker@@MAEXPBDH_K@Z)
main.obj : error LNK2001: unresolved external symbol "protected: virtual void __thiscall StackWalker::OnLoadModule(char const *,char const *,unsigned __int64,int,int,char const *,char const *,unsigned __int64)" (?OnLoadModule@StackWalker@@MAEXPBD0_KHH001@Z)
main.obj : error LNK2001: unresolved external symbol "protected: virtual void __thiscall StackWalker::OnSymInit(char const *,int,char const *)" (?OnSymInit@StackWalker@@MAEXPBDH0@Z)
main.obj : error LNK2001: unresolved external symbol "public: __thiscall StackWalker::StackWalker(int,char const *,int,void *)" (??0StackWalker@@QAE@HPBDHPAX@Z)
release/Prog.exe : fatal error LNK1120: 5 unresolved externals
Error executing link.exe.

I have modified the StackWalker.cpp file to adapt to the UNICODE support, replaced "_tcscat_s" with "wcscat".

我修改了 StackWalker.cpp 文件以适应 UNICODE 支持,将“_tcscat_s”替换为“wcscat”。

can you see, what's going wrong here?

你能看到这里有什么问题吗?

采纳答案by elevener

For cross-platform application Breakpad is probably better solution. But easy way for Windows -is to set unhandled exception handler via SetUnhandledExceptionFilter and write minidumps (with configuration you need) with WriteMiniDumpEx in handler.

对于跨平台应用程序 Breakpad 可能是更好的解决方案。但 Windows 的简单方法是通过 SetUnhandledExceptionFilter 设置未处理的异常处理程序,并在处理程序中使用 WriteMiniDumpEx 编写小型转储(带有您需要的配置)。

example

例子

回答by Macke

Google Breakpadis a cross-platform crash-report library that seems useful.

Google Breakpad是一个看起来很有用的跨平台崩溃报告库。

See also the StackWalkerfor some windows-only code for getting a stack trace.

另请参阅StackWalker以获取一些用于获取堆栈跟踪的仅限 Windows 的代码。

Also, installing an event filter by overriding QCoreApplication::notifyand catch all exceptions (including asyncronous ones such as stack overflow and access violation on Windows) is a good idea.

此外,通过覆盖QCoreApplication::notify和捕获所有异常(包括异步异常,例如 Windows 上的堆栈溢出和访问冲突)来安装事件过滤器是一个好主意。