From 2dcb95be47f4419b63ee4b184d16ec5e0738279f Mon Sep 17 00:00:00 2001 From: likepeng Date: Mon, 23 Oct 2023 14:17:17 +0800 Subject: [PATCH] use std::filesystem::path --- launcher/launcher.cpp | 18 ++++++------ launcher/module_argument.cpp | 3 +- launcher/module_argument.h | 11 ++++---- myframe/app.cpp | 5 ++-- myframe/common.cpp | 29 ++++++-------------- myframe/common.h | 4 +-- myframe/log.cpp | 2 +- test/CMakeLists.txt | 6 ++++ test/app_send_test.cpp | 4 +-- test/common_test.cpp | 25 +++++++++++++++++ test/performance_trans100_fullspeed_test.cpp | 4 +-- test/performance_trans10_cost_test.cpp | 4 +-- test/performance_trans1_cost_test.cpp | 4 +-- test/performance_trans1_fullspeed_test.cpp | 4 +-- test/performance_trans20_fullspeed_test.cpp | 4 +-- 15 files changed, 73 insertions(+), 54 deletions(-) create mode 100644 test/common_test.cpp diff --git a/launcher/launcher.cpp b/launcher/launcher.cpp index b1e8d40..950ae12 100644 --- a/launcher/launcher.cpp +++ b/launcher/launcher.cpp @@ -34,21 +34,21 @@ int main(int argc, char** argv) { // 初始化日志系统 myframe::InitLog(MYFRAME_LOG_DIR, module_args.GetProcessName()); LOG(INFO) << "launch command: " << module_args.GetCmd(); - std::string root_dir = myframe::Common::GetWorkRoot(); + auto root_dir = myframe::Common::GetWorkRoot(); auto lib_dir = myframe::Common::GetAbsolutePath(MYFRAME_LIB_DIR); auto service_dir = myframe::Common::GetAbsolutePath(MYFRAME_SERVICE_DIR); auto log_dir = myframe::Common::GetAbsolutePath(MYFRAME_LOG_DIR); auto conf_dir = myframe::Common::GetAbsolutePath(MYFRAME_CONF_DIR); - LOG(INFO) << "root dir: " << root_dir; - LOG(INFO) << "default lib dir: " << lib_dir; - LOG(INFO) << "default service dir: " << service_dir; - LOG(INFO) << "default log dir: " << log_dir; - LOG(INFO) << "default conf dir: " << conf_dir; + LOG(INFO) << "root dir: " << root_dir.string(); + LOG(INFO) << "default lib dir: " << lib_dir.string(); + LOG(INFO) << "default service dir: " << service_dir.string(); + LOG(INFO) << "default log dir: " << log_dir.string(); + LOG(INFO) << "default conf dir: " << conf_dir.string(); // 初始化并启动线程 g_app = std::make_shared(); if (false == g_app->Init( - lib_dir, + lib_dir.string(), module_args.GetThreadPoolSize(), module_args.GetConnEventSize(), module_args.GetWarningMsgSize())) { @@ -64,7 +64,7 @@ int main(int argc, char** argv) { if (myframe::Common::IsAbsolutePath(conf)) { abs_conf_file = conf; } else { - abs_conf_file = service_dir + conf; + abs_conf_file = (service_dir / conf).string(); } if (!g_app->LoadServiceFromFile(abs_conf_file)) { LOG(ERROR) << "Load " << abs_conf_file << " failed, exit"; @@ -78,7 +78,7 @@ int main(int argc, char** argv) { if (myframe::Common::IsAbsolutePath(module_args.GetConfDir())) { abs_service_dir = module_args.GetConfDir(); } else { - abs_service_dir = root_dir + "/" + module_args.GetConfDir() + "/"; + abs_service_dir = (root_dir / module_args.GetConfDir()).string(); } } else { abs_service_dir = service_dir; diff --git a/launcher/module_argument.cpp b/launcher/module_argument.cpp index 16194cb..eeffad0 100644 --- a/launcher/module_argument.cpp +++ b/launcher/module_argument.cpp @@ -7,7 +7,6 @@ Author: 李柯鹏 #include "module_argument.h" #include #include -#include "myframe/common.h" namespace myframe { @@ -73,7 +72,7 @@ bool ModuleArgument::ParseSysConf(const std::string& sys_conf) { if (Common::IsAbsolutePath(sys_conf)) { full_sys_conf = sys_conf; } else { - full_sys_conf = sys_conf_dir_ + sys_conf; + full_sys_conf = (sys_conf_dir_ / sys_conf).string(); } auto root = Common::LoadJsonFromFile(full_sys_conf); if (root.isNull() diff --git a/launcher/module_argument.h b/launcher/module_argument.h index 675f326..0c7bab7 100644 --- a/launcher/module_argument.h +++ b/launcher/module_argument.h @@ -8,6 +8,7 @@ Author: 李柯鹏 #include #include #include "cmdline.h" +#include "myframe/common.h" namespace myframe { @@ -32,11 +33,11 @@ class ModuleArgument final { int thread_poll_size_{4}; int conn_event_size_{2}; int warning_msg_size_{10}; - std::string cmd_{""}; - std::string binary_name_{""}; - std::string process_name_{""}; - std::string conf_dir_{""}; - std::string sys_conf_dir_{"conf"}; + std::string cmd_; + std::string binary_name_; + std::string process_name_; + std::string conf_dir_; + stdfs::path sys_conf_dir_; std::list conf_list_; cmdline::parser parser_; }; diff --git a/myframe/app.cpp b/myframe/app.cpp index 6c3035c..87fcf0c 100644 --- a/myframe/app.cpp +++ b/myframe/app.cpp @@ -135,8 +135,9 @@ bool App::LoadServiceFromJson(const Json::Value& service) { } lib_name = service["lib"].asString(); auto lib_dir = Common::GetAbsolutePath(lib_dir_); - if (!mods_->LoadMod(lib_dir + lib_name)) { - LOG(ERROR) << "load lib " << (lib_dir + lib_name) << " failed, skip"; + if (!mods_->LoadMod((lib_dir / lib_name).string())) { + LOG(ERROR) << "load lib " + << (lib_dir / lib_name).string() << " failed, skip"; return false; } } diff --git a/myframe/common.cpp b/myframe/common.cpp index b7544ea..33491f4 100644 --- a/myframe/common.cpp +++ b/myframe/common.cpp @@ -10,7 +10,6 @@ Author: 李柯鹏 #include "myframe/platform.h" #if defined(MYFRAME_OS_LINUX) || defined(MYFRAME_OS_ANDROID) -#include #include #else #error "Platform not supported" @@ -19,26 +18,16 @@ Author: 李柯鹏 #include #include - namespace myframe { -std::vector Common::GetDirFiles(const std::string& conf_path) { - std::vector res; -#if defined(MYFRAME_OS_LINUX) || defined(MYFRAME_OS_ANDROID) - DIR* dir = opendir(conf_path.c_str()); - if (dir == nullptr) { - return res; - } - struct dirent* entry = nullptr; - while (nullptr != (entry = readdir(dir))) { - if (entry->d_type == DT_REG) { - res.emplace_back(conf_path + entry->d_name); +std::vector Common::GetDirFiles(const std::string& conf_path) { + std::vector res; + stdfs::path path(conf_path); + for (auto const& dir_entry : stdfs::directory_iterator{path}) { + if (dir_entry.is_regular_file()) { + res.emplace_back(dir_entry.path()); } } - closedir(dir); -#else - #error "Platform not supported" -#endif return res; } @@ -81,18 +70,16 @@ stdfs::path Common::GetWorkRoot() { #endif } -std::string Common::GetAbsolutePath(const std::string& flag_path) { +stdfs::path Common::GetAbsolutePath(const std::string& flag_path) { stdfs::path p(flag_path); if (p.is_absolute()) { return flag_path; } - p += "/"; auto root = GetWorkRoot(); if (root.empty()) { return flag_path; } - root += "/"; - root += p; + root /= p; return root; } diff --git a/myframe/common.h b/myframe/common.h index f922699..88db336 100644 --- a/myframe/common.h +++ b/myframe/common.h @@ -24,11 +24,11 @@ namespace myframe { class MYFRAME_EXPORT Common final { public: - static std::vector GetDirFiles(const std::string& conf_path); + static std::vector GetDirFiles(const std::string& conf_path); static Json::Value LoadJsonFromFile(const std::string& json_file); static stdfs::path GetWorkRoot(); - static std::string GetAbsolutePath(const std::string& flag_path); + static stdfs::path GetAbsolutePath(const std::string& flag_path); static bool IsAbsolutePath(const std::string& path); template diff --git a/myframe/log.cpp b/myframe/log.cpp index a6306ea..46afa29 100644 --- a/myframe/log.cpp +++ b/myframe/log.cpp @@ -30,7 +30,7 @@ void InitLog(const std::string& log_dir, const std::string& bin_name) { FLAGS_stop_logging_if_full_disk = true; auto full_log_dir = Common::GetAbsolutePath(log_dir); - std::string dst_str = full_log_dir + bin_name; + std::string dst_str = (full_log_dir / bin_name).string(); google::SetLogDestination(google::ERROR, ""); google::SetLogDestination(google::WARNING, ""); google::SetLogDestination(google::FATAL, ""); diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index b541f70..a1c3c5e 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -4,6 +4,11 @@ cmake_minimum_required(VERSION 3.10) configure_file(${CMAKE_CURRENT_SOURCE_DIR}/performance_test_config.h.in ${CMAKE_CURRENT_SOURCE_DIR}/performance_test_config.h @ONLY) ### test bin +add_executable(common_test common_test.cpp) +target_link_libraries(common_test + myframe +) + add_executable(app_send_test app_send_test.cpp) target_link_libraries(app_send_test myframe @@ -36,6 +41,7 @@ target_link_libraries(performance_trans100_fullspeed_test ### install INSTALL(TARGETS + common_test app_send_test performance_trans1_cost_test performance_trans10_cost_test diff --git a/test/app_send_test.cpp b/test/app_send_test.cpp index cb1a203..d2dd49d 100644 --- a/test/app_send_test.cpp +++ b/test/app_send_test.cpp @@ -44,9 +44,9 @@ class EchoActorTest : public myframe::Actor { int main() { auto lib_dir = - myframe::Common::GetAbsolutePath(MYFRAME_LIB_DIR); + myframe::Common::GetAbsolutePath(MYFRAME_LIB_DIR).string(); auto log_dir = - myframe::Common::GetAbsolutePath(MYFRAME_LOG_DIR); + myframe::Common::GetAbsolutePath(MYFRAME_LOG_DIR).string(); myframe::InitLog(log_dir, "app_send_test"); diff --git a/test/common_test.cpp b/test/common_test.cpp new file mode 100644 index 0000000..39412ce --- /dev/null +++ b/test/common_test.cpp @@ -0,0 +1,25 @@ +/**************************************************************************** +Copyright (c) 2019, 李柯鹏 +All rights reserved. + +Author: 李柯鹏 +****************************************************************************/ +#include + +#include "myframe/common.h" +#include "myframe/log.h" + +int main() { + auto root = myframe::Common::GetWorkRoot(); + LOG(INFO) << "work root is " << root.string(); + + auto lib_path = myframe::Common::GetAbsolutePath("lib"); + LOG(INFO) << "lib path is " << lib_path.string(); + + auto root_files = myframe::Common::GetDirFiles(root); + LOG(INFO) << "root dir files:"; + for (size_t i = 0; i < root_files.size(); ++i) { + LOG(INFO) << " " << root_files[i].string(); + } + return 0; +} diff --git a/test/performance_trans100_fullspeed_test.cpp b/test/performance_trans100_fullspeed_test.cpp index cdb93dd..39060f7 100644 --- a/test/performance_trans100_fullspeed_test.cpp +++ b/test/performance_trans100_fullspeed_test.cpp @@ -87,9 +87,9 @@ std::atomic_bool FullSpeed100ActorTransTest::is_send_{false}; int main() { auto lib_dir = - myframe::Common::GetAbsolutePath(MYFRAME_LIB_DIR); + myframe::Common::GetAbsolutePath(MYFRAME_LIB_DIR).string(); auto log_dir = - myframe::Common::GetAbsolutePath(MYFRAME_LOG_DIR); + myframe::Common::GetAbsolutePath(MYFRAME_LOG_DIR).string(); myframe::InitLog(log_dir, "performance_trans100_fullspeed_test"); diff --git a/test/performance_trans10_cost_test.cpp b/test/performance_trans10_cost_test.cpp index f3bc74e..b100689 100644 --- a/test/performance_trans10_cost_test.cpp +++ b/test/performance_trans10_cost_test.cpp @@ -97,9 +97,9 @@ std::vector Trans10ActorCostTest::cost_us_list_; int main() { auto lib_dir = - myframe::Common::GetAbsolutePath(MYFRAME_LIB_DIR); + myframe::Common::GetAbsolutePath(MYFRAME_LIB_DIR).string(); auto log_dir = - myframe::Common::GetAbsolutePath(MYFRAME_LOG_DIR); + myframe::Common::GetAbsolutePath(MYFRAME_LOG_DIR).string(); myframe::InitLog(log_dir, "performance_trans10_cost_test"); diff --git a/test/performance_trans1_cost_test.cpp b/test/performance_trans1_cost_test.cpp index 8ad9624..8f0734a 100644 --- a/test/performance_trans1_cost_test.cpp +++ b/test/performance_trans1_cost_test.cpp @@ -75,9 +75,9 @@ class TransMsgCostTest : public myframe::Actor { int main() { auto lib_dir = - myframe::Common::GetAbsolutePath(MYFRAME_LIB_DIR); + myframe::Common::GetAbsolutePath(MYFRAME_LIB_DIR).string(); auto log_dir = - myframe::Common::GetAbsolutePath(MYFRAME_LOG_DIR); + myframe::Common::GetAbsolutePath(MYFRAME_LOG_DIR).string(); myframe::InitLog(log_dir, "performance_trans1_cost_test"); diff --git a/test/performance_trans1_fullspeed_test.cpp b/test/performance_trans1_fullspeed_test.cpp index 5671556..401b542 100644 --- a/test/performance_trans1_fullspeed_test.cpp +++ b/test/performance_trans1_fullspeed_test.cpp @@ -81,9 +81,9 @@ class FullSpeedTransTest : public myframe::Actor { int main() { auto lib_dir = - myframe::Common::GetAbsolutePath(MYFRAME_LIB_DIR); + myframe::Common::GetAbsolutePath(MYFRAME_LIB_DIR).string(); auto log_dir = - myframe::Common::GetAbsolutePath(MYFRAME_LOG_DIR); + myframe::Common::GetAbsolutePath(MYFRAME_LOG_DIR).string(); myframe::InitLog(log_dir, "performance_trans1_fullspeed_test"); diff --git a/test/performance_trans20_fullspeed_test.cpp b/test/performance_trans20_fullspeed_test.cpp index 60126da..ceaa8b2 100644 --- a/test/performance_trans20_fullspeed_test.cpp +++ b/test/performance_trans20_fullspeed_test.cpp @@ -86,9 +86,9 @@ std::atomic_bool FullSpeed20ActorTransTest::is_send_{false}; int main() { auto lib_dir = - myframe::Common::GetAbsolutePath(MYFRAME_LIB_DIR); + myframe::Common::GetAbsolutePath(MYFRAME_LIB_DIR).string(); auto log_dir = - myframe::Common::GetAbsolutePath(MYFRAME_LOG_DIR); + myframe::Common::GetAbsolutePath(MYFRAME_LOG_DIR).string(); myframe::InitLog(log_dir, "performance_trans20_fullspeed_test");