ivbacktrace.h 2.5 KB

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