Skip to content

Commit 2dcb95b

Browse files
committed
use std::filesystem::path
1 parent 299991c commit 2dcb95b

15 files changed

+73
-54
lines changed

launcher/launcher.cpp

+9-9
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,21 @@ int main(int argc, char** argv) {
3434
// 初始化日志系统
3535
myframe::InitLog(MYFRAME_LOG_DIR, module_args.GetProcessName());
3636
LOG(INFO) << "launch command: " << module_args.GetCmd();
37-
std::string root_dir = myframe::Common::GetWorkRoot();
37+
auto root_dir = myframe::Common::GetWorkRoot();
3838
auto lib_dir = myframe::Common::GetAbsolutePath(MYFRAME_LIB_DIR);
3939
auto service_dir = myframe::Common::GetAbsolutePath(MYFRAME_SERVICE_DIR);
4040
auto log_dir = myframe::Common::GetAbsolutePath(MYFRAME_LOG_DIR);
4141
auto conf_dir = myframe::Common::GetAbsolutePath(MYFRAME_CONF_DIR);
42-
LOG(INFO) << "root dir: " << root_dir;
43-
LOG(INFO) << "default lib dir: " << lib_dir;
44-
LOG(INFO) << "default service dir: " << service_dir;
45-
LOG(INFO) << "default log dir: " << log_dir;
46-
LOG(INFO) << "default conf dir: " << conf_dir;
42+
LOG(INFO) << "root dir: " << root_dir.string();
43+
LOG(INFO) << "default lib dir: " << lib_dir.string();
44+
LOG(INFO) << "default service dir: " << service_dir.string();
45+
LOG(INFO) << "default log dir: " << log_dir.string();
46+
LOG(INFO) << "default conf dir: " << conf_dir.string();
4747

4848
// 初始化并启动线程
4949
g_app = std::make_shared<myframe::App>();
5050
if (false == g_app->Init(
51-
lib_dir,
51+
lib_dir.string(),
5252
module_args.GetThreadPoolSize(),
5353
module_args.GetConnEventSize(),
5454
module_args.GetWarningMsgSize())) {
@@ -64,7 +64,7 @@ int main(int argc, char** argv) {
6464
if (myframe::Common::IsAbsolutePath(conf)) {
6565
abs_conf_file = conf;
6666
} else {
67-
abs_conf_file = service_dir + conf;
67+
abs_conf_file = (service_dir / conf).string();
6868
}
6969
if (!g_app->LoadServiceFromFile(abs_conf_file)) {
7070
LOG(ERROR) << "Load " << abs_conf_file << " failed, exit";
@@ -78,7 +78,7 @@ int main(int argc, char** argv) {
7878
if (myframe::Common::IsAbsolutePath(module_args.GetConfDir())) {
7979
abs_service_dir = module_args.GetConfDir();
8080
} else {
81-
abs_service_dir = root_dir + "/" + module_args.GetConfDir() + "/";
81+
abs_service_dir = (root_dir / module_args.GetConfDir()).string();
8282
}
8383
} else {
8484
abs_service_dir = service_dir;

launcher/module_argument.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ Author: 李柯鹏 <[email protected]>
77
#include "module_argument.h"
88
#include <unistd.h>
99
#include <iostream>
10-
#include "myframe/common.h"
1110

1211
namespace myframe {
1312

@@ -73,7 +72,7 @@ bool ModuleArgument::ParseSysConf(const std::string& sys_conf) {
7372
if (Common::IsAbsolutePath(sys_conf)) {
7473
full_sys_conf = sys_conf;
7574
} else {
76-
full_sys_conf = sys_conf_dir_ + sys_conf;
75+
full_sys_conf = (sys_conf_dir_ / sys_conf).string();
7776
}
7877
auto root = Common::LoadJsonFromFile(full_sys_conf);
7978
if (root.isNull()

launcher/module_argument.h

+6-5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Author: 李柯鹏 <[email protected]>
88
#include <string>
99
#include <list>
1010
#include "cmdline.h"
11+
#include "myframe/common.h"
1112

1213
namespace myframe {
1314

@@ -32,11 +33,11 @@ class ModuleArgument final {
3233
int thread_poll_size_{4};
3334
int conn_event_size_{2};
3435
int warning_msg_size_{10};
35-
std::string cmd_{""};
36-
std::string binary_name_{""};
37-
std::string process_name_{""};
38-
std::string conf_dir_{""};
39-
std::string sys_conf_dir_{"conf"};
36+
std::string cmd_;
37+
std::string binary_name_;
38+
std::string process_name_;
39+
std::string conf_dir_;
40+
stdfs::path sys_conf_dir_;
4041
std::list<std::string> conf_list_;
4142
cmdline::parser parser_;
4243
};

myframe/app.cpp

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

myframe/common.cpp

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

1111
#include "myframe/platform.h"
1212
#if defined(MYFRAME_OS_LINUX) || defined(MYFRAME_OS_ANDROID)
13-
#include <dirent.h>
1413
#include <unistd.h>
1514
#else
1615
#error "Platform not supported"
@@ -19,26 +18,16 @@ Author: 李柯鹏 <[email protected]>
1918
#include <fstream>
2019
#include <sstream>
2120

22-
2321
namespace myframe {
2422

25-
std::vector<std::string> Common::GetDirFiles(const std::string& conf_path) {
26-
std::vector<std::string> res;
27-
#if defined(MYFRAME_OS_LINUX) || defined(MYFRAME_OS_ANDROID)
28-
DIR* dir = opendir(conf_path.c_str());
29-
if (dir == nullptr) {
30-
return res;
31-
}
32-
struct dirent* entry = nullptr;
33-
while (nullptr != (entry = readdir(dir))) {
34-
if (entry->d_type == DT_REG) {
35-
res.emplace_back(conf_path + entry->d_name);
23+
std::vector<stdfs::path> Common::GetDirFiles(const std::string& conf_path) {
24+
std::vector<stdfs::path> res;
25+
stdfs::path path(conf_path);
26+
for (auto const& dir_entry : stdfs::directory_iterator{path}) {
27+
if (dir_entry.is_regular_file()) {
28+
res.emplace_back(dir_entry.path());
3629
}
3730
}
38-
closedir(dir);
39-
#else
40-
#error "Platform not supported"
41-
#endif
4231
return res;
4332
}
4433

@@ -81,18 +70,16 @@ stdfs::path Common::GetWorkRoot() {
8170
#endif
8271
}
8372

84-
std::string Common::GetAbsolutePath(const std::string& flag_path) {
73+
stdfs::path Common::GetAbsolutePath(const std::string& flag_path) {
8574
stdfs::path p(flag_path);
8675
if (p.is_absolute()) {
8776
return flag_path;
8877
}
89-
p += "/";
9078
auto root = GetWorkRoot();
9179
if (root.empty()) {
9280
return flag_path;
9381
}
94-
root += "/";
95-
root += p;
82+
root /= p;
9683
return root;
9784
}
9885

myframe/common.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ namespace myframe {
2424

2525
class MYFRAME_EXPORT Common final {
2626
public:
27-
static std::vector<std::string> GetDirFiles(const std::string& conf_path);
27+
static std::vector<stdfs::path> GetDirFiles(const std::string& conf_path);
2828
static Json::Value LoadJsonFromFile(const std::string& json_file);
2929

3030
static stdfs::path GetWorkRoot();
31-
static std::string GetAbsolutePath(const std::string& flag_path);
31+
static stdfs::path GetAbsolutePath(const std::string& flag_path);
3232
static bool IsAbsolutePath(const std::string& path);
3333

3434
template <typename T>

myframe/log.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ void InitLog(const std::string& log_dir, const std::string& bin_name) {
3030
FLAGS_stop_logging_if_full_disk = true;
3131

3232
auto full_log_dir = Common::GetAbsolutePath(log_dir);
33-
std::string dst_str = full_log_dir + bin_name;
33+
std::string dst_str = (full_log_dir / bin_name).string();
3434
google::SetLogDestination(google::ERROR, "");
3535
google::SetLogDestination(google::WARNING, "");
3636
google::SetLogDestination(google::FATAL, "");

test/CMakeLists.txt

+6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ cmake_minimum_required(VERSION 3.10)
44
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/performance_test_config.h.in ${CMAKE_CURRENT_SOURCE_DIR}/performance_test_config.h @ONLY)
55

66
### test bin
7+
add_executable(common_test common_test.cpp)
8+
target_link_libraries(common_test
9+
myframe
10+
)
11+
712
add_executable(app_send_test app_send_test.cpp)
813
target_link_libraries(app_send_test
914
myframe
@@ -36,6 +41,7 @@ target_link_libraries(performance_trans100_fullspeed_test
3641

3742
### install
3843
INSTALL(TARGETS
44+
common_test
3945
app_send_test
4046
performance_trans1_cost_test
4147
performance_trans10_cost_test

test/app_send_test.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ class EchoActorTest : public myframe::Actor {
4444

4545
int main() {
4646
auto lib_dir =
47-
myframe::Common::GetAbsolutePath(MYFRAME_LIB_DIR);
47+
myframe::Common::GetAbsolutePath(MYFRAME_LIB_DIR).string();
4848
auto log_dir =
49-
myframe::Common::GetAbsolutePath(MYFRAME_LOG_DIR);
49+
myframe::Common::GetAbsolutePath(MYFRAME_LOG_DIR).string();
5050

5151
myframe::InitLog(log_dir, "app_send_test");
5252

test/common_test.cpp

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/****************************************************************************
2+
Copyright (c) 2019, 李柯鹏
3+
All rights reserved.
4+
5+
Author: 李柯鹏 <[email protected]>
6+
****************************************************************************/
7+
#include <glog/logging.h>
8+
9+
#include "myframe/common.h"
10+
#include "myframe/log.h"
11+
12+
int main() {
13+
auto root = myframe::Common::GetWorkRoot();
14+
LOG(INFO) << "work root is " << root.string();
15+
16+
auto lib_path = myframe::Common::GetAbsolutePath("lib");
17+
LOG(INFO) << "lib path is " << lib_path.string();
18+
19+
auto root_files = myframe::Common::GetDirFiles(root);
20+
LOG(INFO) << "root dir files:";
21+
for (size_t i = 0; i < root_files.size(); ++i) {
22+
LOG(INFO) << " " << root_files[i].string();
23+
}
24+
return 0;
25+
}

test/performance_trans100_fullspeed_test.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@ std::atomic_bool FullSpeed100ActorTransTest::is_send_{false};
8787

8888
int main() {
8989
auto lib_dir =
90-
myframe::Common::GetAbsolutePath(MYFRAME_LIB_DIR);
90+
myframe::Common::GetAbsolutePath(MYFRAME_LIB_DIR).string();
9191
auto log_dir =
92-
myframe::Common::GetAbsolutePath(MYFRAME_LOG_DIR);
92+
myframe::Common::GetAbsolutePath(MYFRAME_LOG_DIR).string();
9393

9494
myframe::InitLog(log_dir, "performance_trans100_fullspeed_test");
9595

test/performance_trans10_cost_test.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,9 @@ std::vector<int> Trans10ActorCostTest::cost_us_list_;
9797

9898
int main() {
9999
auto lib_dir =
100-
myframe::Common::GetAbsolutePath(MYFRAME_LIB_DIR);
100+
myframe::Common::GetAbsolutePath(MYFRAME_LIB_DIR).string();
101101
auto log_dir =
102-
myframe::Common::GetAbsolutePath(MYFRAME_LOG_DIR);
102+
myframe::Common::GetAbsolutePath(MYFRAME_LOG_DIR).string();
103103

104104
myframe::InitLog(log_dir, "performance_trans10_cost_test");
105105

test/performance_trans1_cost_test.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@ class TransMsgCostTest : public myframe::Actor {
7575

7676
int main() {
7777
auto lib_dir =
78-
myframe::Common::GetAbsolutePath(MYFRAME_LIB_DIR);
78+
myframe::Common::GetAbsolutePath(MYFRAME_LIB_DIR).string();
7979
auto log_dir =
80-
myframe::Common::GetAbsolutePath(MYFRAME_LOG_DIR);
80+
myframe::Common::GetAbsolutePath(MYFRAME_LOG_DIR).string();
8181

8282
myframe::InitLog(log_dir, "performance_trans1_cost_test");
8383

test/performance_trans1_fullspeed_test.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,9 @@ class FullSpeedTransTest : public myframe::Actor {
8181

8282
int main() {
8383
auto lib_dir =
84-
myframe::Common::GetAbsolutePath(MYFRAME_LIB_DIR);
84+
myframe::Common::GetAbsolutePath(MYFRAME_LIB_DIR).string();
8585
auto log_dir =
86-
myframe::Common::GetAbsolutePath(MYFRAME_LOG_DIR);
86+
myframe::Common::GetAbsolutePath(MYFRAME_LOG_DIR).string();
8787

8888
myframe::InitLog(log_dir, "performance_trans1_fullspeed_test");
8989

test/performance_trans20_fullspeed_test.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,9 @@ std::atomic_bool FullSpeed20ActorTransTest::is_send_{false};
8686

8787
int main() {
8888
auto lib_dir =
89-
myframe::Common::GetAbsolutePath(MYFRAME_LIB_DIR);
89+
myframe::Common::GetAbsolutePath(MYFRAME_LIB_DIR).string();
9090
auto log_dir =
91-
myframe::Common::GetAbsolutePath(MYFRAME_LOG_DIR);
91+
myframe::Common::GetAbsolutePath(MYFRAME_LOG_DIR).string();
9292

9393
myframe::InitLog(log_dir, "performance_trans20_fullspeed_test");
9494

0 commit comments

Comments
 (0)