ivbacktrace.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #ifndef IVBACKTRACE_H
  2. #define IVBACKTRACE_H
  3. #include <QtCore/qglobal.h>
  4. //#define USE_BOOSTBACKTRACE //if USE_BOOSTBACKTRACE LIBS += -ldl LIBS += -lboost_system -lboost_filesystem -lbacktrace
  5. #if defined(IVBACKTRACE_LIBRARY)
  6. # define IVBACKTRACE_EXPORT Q_DECL_EXPORT
  7. #else
  8. # define IVBACKTRACE_EXPORT Q_DECL_IMPORT
  9. #endif
  10. #ifndef USE_BOOSTBACKTRACE
  11. void RegisterIVBackTrace();
  12. #else
  13. #ifdef Q_OS_LINUX
  14. #ifndef BOOST_STACKTRACE_USE_BACKTRACE
  15. #define BOOST_STACKTRACE_USE_BACKTRACE
  16. #endif
  17. #endif
  18. #include <string>
  19. #include <boost/noncopyable.hpp>
  20. #include <boost/function.hpp>
  21. #include <boost/stacktrace.hpp>
  22. #include <signal.h> // ::signal, ::raise
  23. // #include <strstream>
  24. #include <stdexcept> // std::logic_error
  25. #include <iostream> // std::cerr
  26. #include <boost/filesystem.hpp>
  27. #include <strstream>
  28. #include <iostream>
  29. #include <QFile>
  30. #include <QDateTime>
  31. #include <QDir>
  32. /*
  33. size_t get_executable_path( char* processdir,char* processname, size_t len)
  34. {
  35. char* path_end;
  36. int nsize;
  37. if((nsize =readlink("/proc/self/exe", processdir,len)) <=0)
  38. return -1;
  39. processdir[nsize] = '\0';
  40. path_end = strrchr(processdir, '/');
  41. if(path_end == NULL)
  42. return -1;
  43. ++path_end;
  44. strcpy(processname, path_end);
  45. *path_end = '\0';
  46. return (size_t)(path_end - processdir);
  47. }
  48. */
  49. void x_signal_handler(int signum) {
  50. ::signal(signum, SIG_DFL);
  51. std::ostrstream ostr;
  52. ostr<<boost::stacktrace::stacktrace()<<std::endl;
  53. // std::cout << boost::stacktrace::stacktrace()<<std::endl;
  54. char strpath[1024];
  55. QDateTime dt = QDateTime::currentDateTime();
  56. char path[1000];
  57. char processname[1024];
  58. // get_executable_path(path, processname, 1000);
  59. snprintf(strpath,255,"%s/log/%s-%d-%s.log",getenv("HOME"),processname, getpid(),dt.toString("yyyyMMddhhmmsszzz").toLatin1().data());
  60. char strdir[1024];
  61. snprintf(strdir,255,"%s/log",getenv("HOME"));
  62. QDir xdir;
  63. xdir.setPath(strdir);
  64. if(!xdir.exists())
  65. {
  66. qDebug("create dir %s",strdir);
  67. xdir.mkdir(strdir);
  68. }
  69. QFile xFile;
  70. xFile.setFileName(strpath);
  71. if(xFile.open(QIODevice::ReadWrite))
  72. {
  73. xFile.write(ostr.str());
  74. }
  75. else
  76. {
  77. std::cout<<ostr.str()<<std::endl;
  78. }
  79. xFile.close();
  80. ::raise(SIGABRT);
  81. }
  82. void RegisterIVBackTrace()
  83. {
  84. std::ostrstream ostr;
  85. ostr << boost::stacktrace::stacktrace()<<std::endl;
  86. ::signal(SIGSEGV, &x_signal_handler);
  87. // ::signal(SIGABRT, &x_signal_handler);
  88. }
  89. #endif
  90. #endif // IVBACKTRACE_H