main.cpp 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #include <QCoreApplication>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <signal.h>
  5. #include <unistd.h>
  6. #include <string.h>
  7. #include "AudioService.h"
  8. #include <iostream>
  9. #include <csignal>
  10. #include <execinfo.h>
  11. #include <dlfcn.h> // 用于dladdr
  12. static void printStackTrace() {
  13. void *callstack[128];
  14. int frames = backtrace(callstack, 128);
  15. char **strs = backtrace_symbols(callstack, frames);
  16. QString str_hour = QDateTime::currentDateTime().toString("hh");
  17. QString str_min = QDateTime::currentDateTime().toString("mm");
  18. QString str_sec = QDateTime::currentDateTime().toString("ss");
  19. QString str_curTime = str_hour + str_min + str_sec;
  20. QString fileName = QString("/data/crash_audioService") + str_curTime + QString(".log");
  21. FILE *log = fopen(fileName.toStdString().c_str(), "a");
  22. for (int i = 0; i < frames; ++i) {
  23. fprintf(log,"%s\n", strs[i]);
  24. }
  25. fclose(log);
  26. free(strs);
  27. }
  28. static AudioService* _audioService = NULL;
  29. static void handler(int sig, siginfo_t *info, void *ucontext) {
  30. (void)ucontext;
  31. printf("AudioService 捕获到信号: %d, 发送进程PID: %d processName:%s\n", sig, info->si_pid, strsignal(sig)) ;
  32. switch(sig) {
  33. case SIGINT:
  34. case SIGILL:
  35. case SIGTERM:
  36. case SIGSEGV:
  37. case SIGFPE:
  38. case SIGABRT:
  39. printStackTrace();
  40. if(_audioService != NULL) {
  41. emit _audioService->onAudioServiceRestart();
  42. }
  43. break;
  44. default:
  45. break;
  46. }
  47. usleep(100 * 1000);
  48. _exit(1);
  49. }
  50. static void setTextCodec();
  51. int main(int argc, char *argv[])
  52. {
  53. freopen("/dev/ttyS0", "w", stdout);
  54. freopen("/dev/ttyS0", "w", stderr);
  55. printf(">>>>>>>>>>>>pid:%d\n",getpid());
  56. struct sigaction sa;
  57. // 清空结构体
  58. memset(&sa, 0, sizeof(sa));
  59. // 指定信号处理函数(使用更强大的 sa_sigaction)
  60. sa.sa_sigaction = handler;
  61. // 设置 SA_SIGINFO 标志以使用 sa_sigaction
  62. sa.sa_flags = SA_SIGINFO;
  63. // 可选:在处理此信号时,阻塞其他信号(如SIGTERM)
  64. sigemptyset(&sa.sa_mask);
  65. if (sigaction(SIGINT, &sa, NULL) == -1) {
  66. perror("sigaction 设置失败");
  67. return 1;
  68. } else if (sigaction(SIGTERM, &sa, NULL) == -1) {
  69. perror("sigaction 设置失败");
  70. return 1;
  71. } else if (sigaction(SIGSEGV, &sa, NULL) == -1) {
  72. perror("sigaction 设置失败");
  73. return 1;
  74. } else if (sigaction(SIGABRT, &sa, NULL) == -1) {
  75. perror("sigaction 设置失败");
  76. return 1;
  77. } else if (sigaction(SIGILL, &sa, NULL) == -1) {
  78. perror("sigaction 设置失败");
  79. return 1;
  80. } else if (sigaction(SIGFPE, &sa, NULL) == -1) {
  81. perror("sigaction 设置失败");
  82. return 1;
  83. }
  84. QCoreApplication app(argc, argv);
  85. setTextCodec();
  86. _audioService = new AudioService(&app);
  87. return app.exec();
  88. }
  89. void setTextCodec() {
  90. // 设置默认编码为UTF-8
  91. QTextCodec *codec = QTextCodec::codecForName("UTF-8");
  92. QTextCodec::setCodecForLocale(codec);
  93. }