From 365c864a587365fe443b11cc0cd7cfc8f8f8eb81 Mon Sep 17 00:00:00 2001
From: lichao <lichao@aiotlink.com>
Date: 星期二, 01 六月 2021 11:19:22 +0800
Subject: [PATCH] refactor, clean up useless code.
---
box/center_main.cc | 36 +++++++++++++++++++++++++++++-------
1 files changed, 29 insertions(+), 7 deletions(-)
diff --git a/box/center_main.cc b/box/center_main.cc
index fdda2cd..e7715f8 100644
--- a/box/center_main.cc
+++ b/box/center_main.cc
@@ -19,6 +19,7 @@
#include "box.h"
#include "center.h"
#include "defs.h"
+#include "log.h"
#include "signalhandle.h"
#include <boost/interprocess/sync/named_mutex.hpp>
#include <chrono>
@@ -50,7 +51,7 @@
if (mtx && time_stamp) {
Guard lock(*mtx);
auto now = NowSec();
- // printf("old: %ld, now: %ld\n", *time_stamp, now);
+ LOG_DEBUG() << "old: " << *time_stamp << ", now: " << now;
if (now > *time_stamp + 10) {
*time_stamp = now;
auto UpdateTime = [this, time_stamp]() {
@@ -85,18 +86,35 @@
} // namespace
int center_main(int argc, const char *argv[])
{
- auto &shm = BHomeShm();
- GlobalInit(shm);
-
AppArg args(argc, argv);
if (args.Has("remove")) {
- shm.Remove();
+ SharedMemory::Remove(BHomeShmName());
return 0;
}
+ ns_log::AddLog(BHLogDir() + "bhshmq_center.log");
+ auto lvl = args.Get("log", "info");
+ if (strcasecmp(lvl.c_str(), "trace") == 0) { ns_log::ResetLogLevel(ns_log::LogLevel::trace); }
+ if (strcasecmp(lvl.c_str(), "debug") == 0) { ns_log::ResetLogLevel(ns_log::LogLevel::debug); }
+ if (strcasecmp(lvl.c_str(), "info") == 0) { ns_log::ResetLogLevel(ns_log::LogLevel::info); }
+ if (strcasecmp(lvl.c_str(), "warning") == 0) { ns_log::ResetLogLevel(ns_log::LogLevel::warning); }
+ if (strcasecmp(lvl.c_str(), "error") == 0) { ns_log::ResetLogLevel(ns_log::LogLevel::error); }
+ if (strcasecmp(lvl.c_str(), "fatal") == 0) { ns_log::ResetLogLevel(ns_log::LogLevel::fatal); }
+
+ auto &shm = BHomeShm();
+ if (!CenterInit(shm)) {
+ auto msg = "init memory error.";
+ LOG_FATAL() << msg;
+ printf("%s\n", msg);
+ exit(0);
+ }
+ GlobalInit(shm);
+
InstanceFlag inst(shm, kCenterRunningFlag);
if (!inst.TryStartAsFirstInstance()) {
- printf("another instance is running, exit.\n");
+ auto msg = "another instance is running, exit.";
+ LOG_INFO() << msg;
+ printf("%s\n", msg);
return 0;
}
@@ -107,8 +125,12 @@
BHCenter center(shm);
center.Start();
- printf("center started ...\n");
+ auto msg = "center started ...";
+ LOG_INFO() << msg;
+ printf("%s\n", msg);
WaitForSignals({SIGINT, SIGTERM});
+ center.Stop();
+ LOG_INFO() << "center stopped.";
return 0;
}
--
Gitblit v1.8.0