vendor.cpp 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #include "vendor.h"
  2. #include "vendor_global.h"
  3. #include <cstdio>
  4. #include <event.h>
  5. #include <fstream>
  6. #include <functional>
  7. #include <getopt.h>
  8. #include <iostream>
  9. #include <memory>
  10. #include <mutex>
  11. #include <signal.h>
  12. #include <sstream>
  13. #include <string>
  14. #include <sys/stat.h>
  15. #include <sys/time.h>
  16. #include <thread>
  17. #include <unistd.h>
  18. #include <utils/logger.h>
  19. #include <vector>
  20. static vendor::MyOpt myOpts_[] = {
  21. {"help", no_argument, 'h', "帮助"}
  22. };
  23. extern void signal_receive(int fd, short event, void *arg);
  24. extern void signal_handle(int signal);
  25. Vendor::Vendor()
  26. {
  27. HTELINK_LOG_ENABLE(true);
  28. eventBase_ = event_base_new();
  29. if (eventBase_ == nullptr) {
  30. HTELINK_LOG_ERR("event new failed, %s");
  31. }
  32. }
  33. Vendor::~Vendor()
  34. {
  35. char dumpFile[VENDOR_MAX_PATH_SIZE];
  36. sprintf("%s/log/event_stat_%s.txt", getenv(VENDOR_RUN_PATH_ENV), utils::nowtostr().c_str());
  37. FILE *fp = fopen(dumpFile, "a");
  38. event_base_dump_events(eventBase_, fp);
  39. fclose(fp);
  40. event_base_free(eventBase_);
  41. }
  42. int Vendor::ParseCmdline(const std::string &optname, const std::string &optarg)
  43. {
  44. return 0;
  45. }
  46. vendor::MyOpt *Vendor::GetOpts()
  47. {
  48. return myOpts_;
  49. }
  50. int Vendor::GetOptSize()
  51. {
  52. return sizeof(myOpts_) / sizeof(myOpts_[0]);
  53. }
  54. int Vendor::Run()
  55. {
  56. // 注册信号事件
  57. event_add(evsignal_new(eventBase_, SIGSEGV, signal_receive, NULL), NULL);
  58. event_add(evsignal_new(eventBase_, SIGFPE, signal_receive, NULL), NULL);
  59. event_add(evsignal_new(eventBase_, SIGINT, signal_receive, NULL), NULL);
  60. // 其他业务
  61. return event_base_dispatch(eventBase_);
  62. }
  63. void signal_receive(int fd, short event, void *arg)
  64. {
  65. if (event != EV_SIGNAL) {
  66. HTELINK_LOG_ERR("fd: %d, event: %d occur", fd, event);
  67. return;
  68. }
  69. vendor::VendorBase::signal_handle(fd);
  70. }