lichao
2021-05-06 0117d5f8ff386075b0c4cbec0cbe460fe3cfa680
src/bh_api.cpp
@@ -1,6 +1,7 @@
#include "bh_api.h"
#include "defs.h"
#include "topic_node.h"
#include <cstdio>
#include <memory>
using namespace bhome_shm;
@@ -8,9 +9,36 @@
namespace
{
std::string GetProcExe()
{
   auto f = fopen("/proc/self/stat", "rb");
   if (f) {
      DEFER1(fclose(f));
      char buf[100] = {0};
      int n = fread(buf, 1, sizeof(buf), f);
      if (n > 0) {
         std::string s(buf, n);
         auto start = s.find('(');
         if (start != std::string::npos) {
            ++start;
            auto end = s.find(')', start);
            return s.substr(start, end - start);
         }
      }
   }
   return std::to_string(getpid());
}
std::unique_ptr<TopicNode> &ProcNodePtr()
{
   static bool init = GlobalInit(BHomeShm());
   auto InitLog = []() {
      auto id = GetProcExe();
      char path[200] = {0};
      sprintf(path, "/tmp/bhshmq_node_%s.log", id.c_str());
      ns_log::AddLog(path);
      return true;
   };
   static bool init_log = InitLog();
   static std::unique_ptr<TopicNode> ptr(new TopicNode(BHomeShm()));
   return ptr;
}