From 77a6c3512a44dfe6540dde71946e6484fe4f173f Mon Sep 17 00:00:00 2001
From: lichao <lichao@aiotlink.com>
Date: 星期一, 10 五月 2021 16:05:28 +0800
Subject: [PATCH] test lock code.

---
 src/bh_api.cpp |   44 +++++++++++++++++++++++++++++++++++++++++---
 1 files changed, 41 insertions(+), 3 deletions(-)

diff --git a/src/bh_api.cpp b/src/bh_api.cpp
index 7e7b2e9..c9ceb20 100644
--- a/src/bh_api.cpp
+++ b/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,11 +9,42 @@
 
 namespace
 {
-TopicNode &ProcNode()
+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());
-	static TopicNode node(BHomeShm());
-	return node;
+	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;
+}
+TopicNode &ProcNode()
+{
+	return *ProcNodePtr();
 }
 
 class TmpPtr : private boost::noncopyable
@@ -320,6 +352,12 @@
 	free(data);
 }
 
+int BHCleanup()
+{
+	ProcNodePtr().reset();
+	return 0;
+}
+
 int BHGetLastError(void **msg, int *msg_len)
 {
 	int ec = 0;

--
Gitblit v1.8.0