Skip to content

Commit

Permalink
use std::filesystem::path
Browse files Browse the repository at this point in the history
  • Loading branch information
lkpworkspace committed Oct 23, 2023
1 parent 299991c commit 2dcb95b
Show file tree
Hide file tree
Showing 15 changed files with 73 additions and 54 deletions.
18 changes: 9 additions & 9 deletions launcher/launcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<myframe::App>();
if (false == g_app->Init(
lib_dir,
lib_dir.string(),
module_args.GetThreadPoolSize(),
module_args.GetConnEventSize(),
module_args.GetWarningMsgSize())) {
Expand All @@ -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";
Expand All @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions launcher/module_argument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ Author: 李柯鹏 <likepeng0418@163.com>
#include "module_argument.h"
#include <unistd.h>
#include <iostream>
#include "myframe/common.h"

namespace myframe {

Expand Down Expand Up @@ -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()
Expand Down
11 changes: 6 additions & 5 deletions launcher/module_argument.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Author: 李柯鹏 <likepeng0418@163.com>
#include <string>
#include <list>
#include "cmdline.h"
#include "myframe/common.h"

namespace myframe {

Expand All @@ -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<std::string> conf_list_;
cmdline::parser parser_;
};
Expand Down
5 changes: 3 additions & 2 deletions myframe/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
29 changes: 8 additions & 21 deletions myframe/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ Author: 李柯鹏 <likepeng0418@163.com>

#include "myframe/platform.h"
#if defined(MYFRAME_OS_LINUX) || defined(MYFRAME_OS_ANDROID)
#include <dirent.h>
#include <unistd.h>
#else
#error "Platform not supported"
Expand All @@ -19,26 +18,16 @@ Author: 李柯鹏 <likepeng0418@163.com>
#include <fstream>
#include <sstream>


namespace myframe {

std::vector<std::string> Common::GetDirFiles(const std::string& conf_path) {
std::vector<std::string> 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<stdfs::path> Common::GetDirFiles(const std::string& conf_path) {
std::vector<stdfs::path> 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;
}

Expand Down Expand Up @@ -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;
}

Expand Down
4 changes: 2 additions & 2 deletions myframe/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ namespace myframe {

class MYFRAME_EXPORT Common final {
public:
static std::vector<std::string> GetDirFiles(const std::string& conf_path);
static std::vector<stdfs::path> 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 <typename T>
Expand Down
2 changes: 1 addition & 1 deletion myframe/log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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, "");
Expand Down
6 changes: 6 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions test/app_send_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down
25 changes: 25 additions & 0 deletions test/common_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/****************************************************************************
Copyright (c) 2019, 李柯鹏
All rights reserved.
Author: 李柯鹏 <likepeng0418@163.com>
****************************************************************************/
#include <glog/logging.h>

#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;
}
4 changes: 2 additions & 2 deletions test/performance_trans100_fullspeed_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down
4 changes: 2 additions & 2 deletions test/performance_trans10_cost_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ std::vector<int> 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");

Expand Down
4 changes: 2 additions & 2 deletions test/performance_trans1_cost_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down
4 changes: 2 additions & 2 deletions test/performance_trans1_fullspeed_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down
4 changes: 2 additions & 2 deletions test/performance_trans20_fullspeed_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down

0 comments on commit 2dcb95b

Please sign in to comment.