boost stacktrace堆栈打印

最近3年一直在做手游开发, cocos+lua跨平台win,安卓,ios

在windows下最方便的是minidump,其他2个平台麻烦不少,google-breakpad使用起来又太麻烦.

最近boost1.65版本出了个stacktrace使用起来简单方便,只是无法看实际数据,对于快速定位BUG还是很有帮助的.

要注意的是异常的处理需要写文件,应用重启之后再读取查看~  用其他应用读取或者修改应用之后读取都会无法正确显示!!!

 1 #pragma once
 2 //异常生成dump时立刻查看会死锁,只能重启应用后才能查看
 3 
 4 #ifndef BOOST_ENABLE_ASSERT_DEBUG_HANDLER
 5 #define BOOST_ENABLE_ASSERT_DEBUG_HANDLER
 6 #endif
 7 
 8 #include <string>
 9 #include <boost/noncopyable.hpp>
10 #include <boost/function.hpp>
11 #include <boost/stacktrace.hpp>
12 
13 class plugin_dump : 
14     private boost::noncopyable
15 {
16 public:
17     plugin_dump();
18     ~plugin_dump();
19 
20     //显示dump信息回调
21     typedef boost::function1<void, const boost::stacktrace::stacktrace&> ON_DUMP;
22 
23     void set_handler(ON_DUMP _handler);
24 
25     static std::string gen_filename();
26 private:
27     void show_last_dump();
28     ON_DUMP m_handler;
29 };
plugin_dump.h
 1 #include "stdafx.h"
 2 #include "plugin_dump.h"
 3 
 4 #include <signal.h>     // ::signal, ::raise
 5 #include <strstream>
 6 #include <stdexcept>    // std::logic_error
 7 #include <iostream>     // std::cerr
 8 #include <boost/filesystem.hpp>
 9 #include <enable_process_info.hpp>
10 
11 void g_signal_handler(int signum) {
12     ::signal(signum, SIG_DFL);    
13     std::string filename = plugin_dump::gen_filename();
14     if (boost::filesystem::exists(filename.c_str()))
15         boost::filesystem::remove(filename.c_str());
16     boost::stacktrace::safe_dump_to(3, boost::stacktrace::detail::max_frames_dump, filename.c_str());
17     ::raise(SIGABRT);
18 }
19 
20 plugin_dump::plugin_dump()
21 {
22     ::signal(SIGSEGV, &g_signal_handler);
23     ::signal(SIGABRT, &g_signal_handler);
24     if (!boost::filesystem::exists("./dumps"))
25         boost::filesystem::create_directory("./dumps");
26 }
27 
28 plugin_dump::~plugin_dump()
29 {    
30     
31 }
32 
33 void plugin_dump::set_handler(ON_DUMP _handler)
34 {
35     m_handler = _handler;
36     show_last_dump();
37 }
38 
39 void plugin_dump::show_last_dump()
40 {
41     std::string filename = plugin_dump::gen_filename();
42     if (boost::filesystem::exists(filename.c_str())) {
43 
44         std::ifstream ifs(filename.c_str());
45         boost::stacktrace::stacktrace st = boost::stacktrace::stacktrace::from_dump(ifs);
46 
47         if (!m_handler.empty())
48             m_handler(st);
49         else
50             std::cout << st;
51 
52         ifs.close();
53         std::getchar();
54     }
55 }
56 
57 std::string plugin_dump::gen_filename()
58 {
59     std::string file = "./dumps/" + enable_process_info::get_processname() + ".dump";
60     return file;
61 }
62 
63 //////////////////////////////////////////////////////////////////////////
64 // BOOST_ENABLE_ASSERT_DEBUG_HANDLER is defined for the whole project
65 namespace boost {
66     inline void assertion_failed_msg(char const* expr, char const* msg, char const* function, char const* /*file*/, long /*line*/) {
67         std::cerr << "Expression '" << expr << "' is false in function '" << function << "': " << (msg ? msg : "<...>") << ".\n"
68             << "Backtrace:\n" << boost::stacktrace::stacktrace() << '\n';
69         std::abort();
70     }
71 
72     inline void assertion_failed(char const* expr, char const* function, char const* file, long line) {
73         ::boost::assertion_failed_msg(expr, 0 /*nullptr*/, function, file, line);
74     }
75 } // namespace boost
plugin_dump.cpp

 

 

 

 

posted @ 2017-11-01 16:41  飞鱼云  阅读(3295)  评论(0编辑  收藏  举报