Browse Source

#3 C4产品

Signed-off-by: wlxuz <myxuan475@126.com>
Change-Id: I0cad955cf1dd120a333587c8da0fb9e8d0952857
wlxuz 10 months ago
parent
commit
775fe3d40d
4 changed files with 130 additions and 0 deletions
  1. 24 0
      C4/main.cpp
  2. 81 0
      C4/vendor.cpp
  3. 19 0
      C4/vendor.h
  4. 6 0
      C4/vendor_global.h

+ 24 - 0
C4/main.cpp

@@ -0,0 +1,24 @@
+#include "vendor.h"
+#include <condition_variable>
+#include <cstdio>
+#include <fstream>
+#include <functional>
+#include <getopt.h>
+#include <iostream>
+#include <memory>
+#include <mutex>
+#include <sstream>
+#include <string.h>
+#include <string>
+#include <sys/stat.h>
+#include <sys/time.h>
+#include <thread>
+#include <unistd.h>
+#include <vector>
+
+int main(int argc, char *argv[])
+{
+    vendor::VendorBase *vdor = new Vendor();
+    vendor::VendorBase::ParseOptionArgs(vdor, argc, argv);
+    return vdor->Run();
+}

+ 81 - 0
C4/vendor.cpp

@@ -0,0 +1,81 @@
+#include "vendor.h"
+#include "vendor_global.h"
+#include <cstdio>
+#include <event.h>
+
+#include <fstream>
+#include <functional>
+#include <getopt.h>
+#include <iostream>
+#include <memory>
+#include <mutex>
+#include <signal.h>
+#include <sstream>
+#include <string>
+#include <sys/stat.h>
+#include <sys/time.h>
+#include <thread>
+#include <unistd.h>
+#include <utils/logger.h>
+#include <vector>
+
+static vendor::MyOpt myOpts_[] = {
+    {"help", no_argument, 'h', "帮助"}
+};
+extern void signal_receive(int fd, short event, void *arg);
+extern void signal_handle(int signal);
+
+Vendor::Vendor()
+{
+    HTELINK_LOG_ENABLE(true);
+    eventBase_ = event_base_new();
+    if (eventBase_ == nullptr) {
+        HTELINK_LOG_ERR("event new failed, %s");
+    }
+}
+
+Vendor::~Vendor()
+{
+    char dumpFile[VENDOR_MAX_PATH_SIZE];
+    sprintf("%s/log/event_stat_%s.txt", getenv(VENDOR_RUN_PATH_ENV), utils::nowtostr().c_str());
+    FILE *fp = fopen(dumpFile, "a");
+    event_base_dump_events(eventBase_, fp);
+    fclose(fp);
+
+    event_base_free(eventBase_);
+}
+
+int Vendor::ParseCmdline(const std::string &optname, const std::string &optarg)
+{
+    return 0;
+}
+
+vendor::MyOpt *Vendor::GetOpts()
+{
+    return myOpts_;
+}
+
+int Vendor::GetOptSize()
+{
+    return sizeof(myOpts_) / sizeof(myOpts_[0]);
+}
+
+int Vendor::Run()
+{
+    // 注册信号事件
+    event_add(evsignal_new(eventBase_, SIGSEGV, signal_receive, NULL), NULL);
+    event_add(evsignal_new(eventBase_, SIGFPE, signal_receive, NULL), NULL);
+    event_add(evsignal_new(eventBase_, SIGINT, signal_receive, NULL), NULL);
+
+    // 其他业务
+    return event_base_dispatch(eventBase_);
+}
+
+void signal_receive(int fd, short event, void *arg)
+{
+    if (event != EV_SIGNAL) {
+        HTELINK_LOG_ERR("fd: %d, event: %d occur", fd, event);
+        return;
+    }
+    vendor::VendorBase::signal_handle(fd);
+}

+ 19 - 0
C4/vendor.h

@@ -0,0 +1,19 @@
+#ifndef __VENDOR_C4_VENDOR_H__
+#define __VENDOR_C4_VENDOR_H__
+#include "vendor_base.h"
+#include <event.h>
+
+class Vendor : public vendor::VendorBase {
+public:
+    Vendor();
+    virtual ~Vendor();
+    virtual int ParseCmdline(const std::string &optname, const std::string &optarg);
+    virtual vendor::MyOpt *GetOpts();
+    virtual int GetOptSize();
+    virtual int Run();
+
+private:
+    struct event_base *eventBase_;
+};
+
+#endif // __VENDOR_C4_VENDOR_H__

+ 6 - 0
C4/vendor_global.h

@@ -0,0 +1,6 @@
+#ifndef __VENDOR_GLOBAL_H__
+#define __VENDOR_GLOBAL_H__
+
+#define VENDOR_MAX_PATH_SIZE 256
+#define VENDOR_RUN_PATH_ENV  "DAS_CONFIG_DIR"
+#endif // __VENDOR_GLOBAL_H__