Skip to content

Commit e3f2a8e

Browse files
authored
Merge pull request #26 from lkpworkspace/dev
v0.8.4
2 parents a053730 + 3559aaa commit e3f2a8e

21 files changed

+121
-62
lines changed

CMakeLists.txt

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
cmake_minimum_required(VERSION 3.10)
2-
project(myframe VERSION 0.8.3)
2+
project(myframe VERSION 0.8.4)
33

44
### option
55
option(MYFRAME_USE_CV "Using conditional variables for thread communication" OFF)
6+
option(MYFRAME_INSTALL_DEPS "Install deps" ON)
67
option(MYFRAME_GENERATE_EXAMPLE "Generate example library" ON)
78
option(MYFRAME_GENERATE_TEST "Generate test executable program" ON)
89

@@ -82,6 +83,12 @@ install(CODE "
8283
\"${${PROJECT_NAME}_VERSION_MAJOR}.${${PROJECT_NAME}_VERSION_MINOR}.${${PROJECT_NAME}_VERSION_PATCH}\"
8384
)
8485
")
86+
if (MYFRAME_INSTALL_DEPS)
87+
if (${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.21")
88+
install(IMPORTED_RUNTIME_ARTIFACTS jsoncpp_lib)
89+
install(IMPORTED_RUNTIME_ARTIFACTS glog::glog)
90+
endif()
91+
endif()
8592

8693
### package
8794
include(InstallRequiredSystemLibraries)

doc/development_guide.md

+6-3
Original file line numberDiff line numberDiff line change
@@ -64,18 +64,21 @@ python3 ~/myframe/tools/gen_mod_proj.py --dir="/path/to/proj_dir/" --name="mod_n
6464

6565
### 组件工程构建安装
6666
```sh
67-
cmake -S . -B build -DCMAKE_INSTALL_PREFIX=${myframe目录}
67+
cmake -S . -B build \
68+
-DCMAKE_INSTALL_PREFIX="/path/to/myframe" \
69+
-DCMAKE_PREFIX_PATH="/path/to/jsoncpp;/path/to/glog"
70+
6871
make -C build -j "$(nproc)" install
6972
```
7073

7174
### 运行组件
7275
```sh
73-
cd ${myframe目录}/bin
76+
cd /path/to/myframe/bin
7477
./launcher -p app ${组件名}.json
7578
```
7679

7780
### 查看运行日志
7881
```sh
79-
cd ${myframe目录}/log
82+
cd /path/to/myframe/log
8083
vi app.INFO
8184
```

launcher/launcher.cpp

+34-19
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,38 @@ int main(int argc, char** argv) {
3131
myframe::ModuleArgument module_args(MYFRAME_CONF_DIR);
3232
module_args.ParseArgument(argc, argv);
3333

34-
// 初始化日志系统
35-
myframe::InitLog(MYFRAME_LOG_DIR, module_args.GetProcessName());
36-
LOG(INFO) << "launch command: " << module_args.GetCmd();
34+
// 初始化日志和参数
3735
auto root_dir = myframe::Common::GetWorkRoot();
38-
auto lib_dir = myframe::Common::GetAbsolutePath(MYFRAME_LIB_DIR);
39-
auto service_dir = myframe::Common::GetAbsolutePath(MYFRAME_SERVICE_DIR);
40-
auto log_dir = myframe::Common::GetAbsolutePath(MYFRAME_LOG_DIR);
41-
auto conf_dir = myframe::Common::GetAbsolutePath(MYFRAME_CONF_DIR);
36+
stdfs::path log_dir;
37+
stdfs::path lib_dir;
38+
stdfs::path service_dir;
39+
stdfs::path conf_dir;
40+
41+
if (module_args.GetLogDir().empty()) {
42+
log_dir = MYFRAME_LOG_DIR;
43+
} else {
44+
log_dir = module_args.GetLogDir();
45+
}
46+
log_dir = myframe::Common::GetAbsolutePath(log_dir.string());
47+
myframe::InitLog(log_dir, module_args.GetProcessName());
48+
LOG(INFO) << "launch command: " << module_args.GetCmd();
49+
50+
if (module_args.GetLibDir().empty()) {
51+
lib_dir = MYFRAME_LIB_DIR;
52+
} else {
53+
lib_dir = module_args.GetLibDir();
54+
}
55+
lib_dir = myframe::Common::GetAbsolutePath(lib_dir.string());
56+
57+
if (module_args.GetConfDir().empty()) {
58+
service_dir = MYFRAME_SERVICE_DIR;
59+
} else {
60+
service_dir = module_args.GetConfDir();
61+
}
62+
service_dir = myframe::Common::GetAbsolutePath(service_dir.string());
63+
64+
conf_dir = myframe::Common::GetAbsolutePath(MYFRAME_CONF_DIR);
65+
4266
LOG(INFO) << "root dir: " << root_dir.string();
4367
LOG(INFO) << "default lib dir: " << lib_dir.string();
4468
LOG(INFO) << "default service dir: " << service_dir.string();
@@ -73,18 +97,9 @@ int main(int argc, char** argv) {
7397
}
7498
}
7599
} else {
76-
std::string abs_service_dir;
77-
if (!module_args.GetConfDir().empty()) {
78-
if (myframe::Common::IsAbsolutePath(module_args.GetConfDir())) {
79-
abs_service_dir = module_args.GetConfDir();
80-
} else {
81-
abs_service_dir = (root_dir / module_args.GetConfDir()).string();
82-
}
83-
} else {
84-
abs_service_dir = service_dir;
85-
}
86-
if (g_app->LoadServiceFromDir(abs_service_dir) <= 0) {
87-
LOG(ERROR) << "Load service from " << abs_service_dir << " failed, exit";
100+
if (g_app->LoadServiceFromDir(service_dir.string()) <= 0) {
101+
LOG(ERROR) << "Load service from " << service_dir.string()
102+
<< " failed, exit";
88103
g_app->Quit();
89104
}
90105
}

launcher/module_argument.cpp

+32-3
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ Author: 李柯鹏 <[email protected]>
1111
namespace myframe {
1212

1313
ModuleArgument::ModuleArgument(
14-
const std::string& sys_conf_dir) {
15-
sys_conf_dir_ = myframe::Common::GetAbsolutePath(sys_conf_dir);
14+
const std::string& default_sys_conf_dir) {
15+
default_sys_conf_dir_ = myframe::Common::GetAbsolutePath(
16+
default_sys_conf_dir);
1617
parser_.add<std::string>("process_name", 'p',
1718
"The name of this launcher process, "
1819
"and it is also the name of log, "
@@ -24,6 +25,12 @@ ModuleArgument::ModuleArgument(
2425
parser_.add<std::string>("dir", 'd',
2526
"module config dir",
2627
false, "");
28+
parser_.add<std::string>("log_dir", 0,
29+
"framework log dir",
30+
false, "");
31+
parser_.add<std::string>("lib_dir", 0,
32+
"framework lib dir",
33+
false, "");
2734
parser_.footer("module_config_file ...");
2835
}
2936

@@ -65,14 +72,24 @@ void ModuleArgument::ParseArgument(
6572
for (size_t i = 0; i < parser_.rest().size(); i++) {
6673
conf_list_.emplace_back(parser_.rest()[i]);
6774
}
75+
76+
auto log_dir = parser_.get<std::string>("log_dir");
77+
if (!log_dir.empty()) {
78+
log_dir_ = log_dir;
79+
}
80+
81+
auto lib_dir = parser_.get<std::string>("lib_dir");
82+
if (!lib_dir.empty()) {
83+
lib_dir_ = lib_dir;
84+
}
6885
}
6986

7087
bool ModuleArgument::ParseSysConf(const std::string& sys_conf) {
7188
std::string full_sys_conf;
7289
if (Common::IsAbsolutePath(sys_conf)) {
7390
full_sys_conf = sys_conf;
7491
} else {
75-
full_sys_conf = (sys_conf_dir_ / sys_conf).string();
92+
full_sys_conf = (default_sys_conf_dir_ / sys_conf).string();
7693
}
7794
auto root = Common::LoadJsonFromFile(full_sys_conf);
7895
if (root.isNull()
@@ -97,6 +114,18 @@ bool ModuleArgument::ParseSysConf(const std::string& sys_conf) {
97114
&& root["warning_msg_size"].asInt() < 1024) {
98115
warning_msg_size_ = root["warning_msg_size"].asInt();
99116
}
117+
if (root.isMember("log_dir")
118+
&& root["log_dir"].isString()) {
119+
log_dir_ = root["log_dir"].asString();
120+
}
121+
if (root.isMember("lib_dir")
122+
&& root["lib_dir"].isString()) {
123+
lib_dir_ = root["lib_dir"].asString();
124+
}
125+
if (root.isMember("service_dir")
126+
&& root["service_dir"].isString()) {
127+
conf_dir_ = root["service_dir"].asString();
128+
}
100129
return true;
101130
}
102131

launcher/module_argument.h

+10-4
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@ namespace myframe {
1414

1515
class ModuleArgument final {
1616
public:
17-
ModuleArgument(const std::string& sys_conf_dir);
17+
ModuleArgument(const std::string& default_sys_conf_dir);
1818
~ModuleArgument() = default;
1919

2020
void ParseArgument(const int argc, char** argv);
2121
inline std::list<std::string> GetConfList() const { return conf_list_; }
2222
inline std::string GetConfDir() const { return conf_dir_; }
23+
inline std::string GetLogDir() const { return log_dir_; }
24+
inline std::string GetLibDir() const { return lib_dir_; }
2325
inline std::string GetBinaryName() const { return binary_name_; }
2426
inline std::string GetProcessName() const { return process_name_; }
2527
inline std::string GetCmd() const { return cmd_; }
@@ -33,12 +35,16 @@ class ModuleArgument final {
3335
int thread_poll_size_{4};
3436
int conn_event_size_{2};
3537
int warning_msg_size_{10};
38+
std::string log_dir_;
39+
std::string lib_dir_;
40+
std::string conf_dir_;
41+
std::list<std::string> conf_list_;
42+
stdfs::path default_sys_conf_dir_;
43+
3644
std::string cmd_;
3745
std::string binary_name_;
3846
std::string process_name_;
39-
std::string conf_dir_;
40-
stdfs::path sys_conf_dir_;
41-
std::list<std::string> conf_list_;
47+
4248
cmdline::parser parser_;
4349
};
4450

myframe/actor.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ class MYFRAME_EXPORT Actor {
120120
std::string mod_name_;
121121
std::string actor_name_;
122122
std::string instance_name_;
123-
Json::Value config_{ Json::Value::null };
123+
Json::Value config_;
124124
std::weak_ptr<ActorContext> ctx_;
125125

126126
DISALLOW_COPY_AND_ASSIGN(Actor)

myframe/app.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,9 @@ bool App::LoadServiceFromJson(const Json::Value& service) {
134134
return false;
135135
}
136136
lib_name = service["lib"].asString();
137-
auto lib_dir = Common::GetAbsolutePath(lib_dir_);
138-
if (!mods_->LoadMod((lib_dir / lib_name).string())) {
137+
if (!mods_->LoadMod((lib_dir_ / lib_name).string())) {
139138
LOG(ERROR) << "load lib "
140-
<< (lib_dir / lib_name).string() << " failed, skip";
139+
<< (lib_dir_ / lib_name).string() << " failed, skip";
141140
return false;
142141
}
143142
}

myframe/app.h

+7-4
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,16 @@ Author: 李柯鹏 <[email protected]>
1212
#include <unordered_map>
1313
#include <vector>
1414
#include <string>
15+
#include <filesystem>
1516

1617
#include <json/json.h>
1718

1819
#include "myframe/macros.h"
1920
#include "myframe/event.h"
2021
#include "myframe/export.h"
2122

23+
namespace stdfs = std::filesystem;
24+
2225
namespace myframe {
2326

2427
class Msg;
@@ -61,12 +64,12 @@ class MYFRAME_EXPORT App final : public std::enable_shared_from_this<App> {
6164
const std::string& inst_name,
6265
const std::string& params,
6366
std::shared_ptr<Actor> actor,
64-
const Json::Value& config = Json::Value::null);
67+
const Json::Value& config = Json::Value::nullSingleton());
6568

6669
bool AddWorker(
6770
const std::string& inst_name,
6871
std::shared_ptr<Worker> worker,
69-
const Json::Value& config = Json::Value::null);
72+
const Json::Value& config = Json::Value::nullSingleton());
7073

7174
int Send(
7275
const std::string& dst,
@@ -88,7 +91,7 @@ class MYFRAME_EXPORT App final : public std::enable_shared_from_this<App> {
8891
const std::string& actor_name,
8992
const std::string& inst_name,
9093
const std::string& params,
91-
const Json::Value& config = Json::Value::null);
94+
const Json::Value& config);
9295
bool CreateActorContext(
9396
std::shared_ptr<Actor> inst,
9497
const std::string& params);
@@ -123,7 +126,7 @@ class MYFRAME_EXPORT App final : public std::enable_shared_from_this<App> {
123126
void ProcessMain(std::shared_ptr<Msg>);
124127
void GetAllUserModAddr(std::string* info);
125128

126-
std::string lib_dir_;
129+
stdfs::path lib_dir_;
127130
/// node地址
128131
std::string node_addr_;
129132
std::atomic<std::size_t> warning_msg_size_{10};

myframe/common.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@ std::vector<stdfs::path> Common::GetDirFiles(const std::string& conf_path) {
3434
Json::Value Common::LoadJsonFromFile(const std::string& json_file) {
3535
std::ifstream ifs(json_file);
3636
if (!ifs.is_open()) {
37-
return Json::Value::null;
37+
return Json::Value::nullSingleton();
3838
}
3939
Json::Value root;
4040
Json::Reader reader(Json::Features::strictMode());
4141
if (!reader.parse(ifs, root)) {
4242
ifs.close();
43-
return Json::Value::null;
43+
return Json::Value::nullSingleton();
4444
}
4545
ifs.close();
4646
return root;

myframe/event_conn.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ Author: 李柯鹏 <[email protected]>
88

99
#include <glog/logging.h>
1010

11-
#include "myframe/common.h"
1211
#include "myframe/msg.h"
1312

1413
namespace myframe {

myframe/log.cpp

+6-9
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ Author: 李柯鹏 <[email protected]>
1212

1313
#include <glog/logging.h>
1414

15-
#include "myframe/common.h"
16-
1715
static void signal_handler(const char *data, size_t size) {
1816
std::string str = std::string(data, size);
1917
std::cerr << str;
@@ -22,19 +20,18 @@ static void signal_handler(const char *data, size_t size) {
2220

2321
namespace myframe {
2422

25-
void InitLog(const std::string& log_dir, const std::string& bin_name) {
23+
void InitLog(const stdfs::path& log_dir, const std::string& bin_name) {
2624
google::InitGoogleLogging(bin_name.c_str());
2725

2826
FLAGS_logbufsecs = 0;
2927
FLAGS_max_log_size = 100;
3028
FLAGS_stop_logging_if_full_disk = true;
3129

32-
auto full_log_dir = Common::GetAbsolutePath(log_dir);
33-
std::string dst_str = (full_log_dir / bin_name).string();
34-
google::SetLogDestination(google::ERROR, "");
35-
google::SetLogDestination(google::WARNING, "");
36-
google::SetLogDestination(google::FATAL, "");
37-
google::SetLogDestination(google::INFO, dst_str.c_str());
30+
std::string dst_str = (log_dir / bin_name).string();
31+
google::SetLogDestination(google::GLOG_ERROR, "");
32+
google::SetLogDestination(google::GLOG_WARNING, "");
33+
google::SetLogDestination(google::GLOG_FATAL, "");
34+
google::SetLogDestination(google::GLOG_INFO, dst_str.c_str());
3835

3936
google::InstallFailureSignalHandler();
4037
google::InstallFailureWriter(&signal_handler);

myframe/log.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@ Author: 李柯鹏 <[email protected]>
66
****************************************************************************/
77
#pragma once
88
#include <string>
9+
#include <filesystem>
910
#include "myframe/export.h"
1011

12+
namespace stdfs = std::filesystem;
13+
1114
namespace myframe {
1215

1316
MYFRAME_EXPORT void InitLog(
14-
const std::string& log_dir,
17+
const stdfs::path& log_dir,
1518
const std::string& bin_name);
1619

1720
MYFRAME_EXPORT void ShutdownLog();

myframe/platform/cmd_channel_generic.h

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ Author: 李柯鹏 <[email protected]>
1313

1414
#include <glog/logging.h>
1515

16-
#include "myframe/common.h"
1716
#include "myframe/export.h"
1817
#include "myframe/macros.h"
1918
#include "myframe/event.h"

myframe/platform/cmd_channel_linux.h

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ Author: 李柯鹏 <[email protected]>
1313

1414
#include <glog/logging.h>
1515

16-
#include "myframe/common.h"
1716
#include "myframe/export.h"
1817
#include "myframe/macros.h"
1918
#include "myframe/event.h"

myframe/worker.cpp

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ Author: 李柯鹏 <[email protected]>
1010

1111
#include <glog/logging.h>
1212

13-
#include "myframe/common.h"
1413
#include "myframe/msg.h"
1514
#include "myframe/worker_context.h"
1615
#include "myframe/app.h"

0 commit comments

Comments
 (0)