From ed61b702f65e1ed2a027bba9b7cc9ec9e3c26fbb Mon Sep 17 00:00:00 2001 From: jie Date: Fri, 22 Oct 2021 12:14:12 +0800 Subject: [PATCH] change default config value --- WebApp/WebApp/WebApp.cpp | 2 ++ WebApp/bserv/bserv.cpp | 18 ++++++++++-------- WebApp/bserv/include/bserv/config.hpp | 6 ++++-- WebApp/bserv/include/bserv/logging.hpp | 21 ++++++++++++++------- config-Windows.json | 3 ++- config-ubuntu.json | 3 ++- 6 files changed, 34 insertions(+), 19 deletions(-) diff --git a/WebApp/WebApp/WebApp.cpp b/WebApp/WebApp/WebApp.cpp index 1f0dcae..f83b946 100644 --- a/WebApp/WebApp/WebApp.cpp +++ b/WebApp/WebApp/WebApp.cpp @@ -46,6 +46,8 @@ int main(int argc, char* argv[]) { config.set_num_db_conn((int)config_obj["conn-num"].as_int64()); if (config_obj.contains("conn-str")) config.set_db_conn_str(config_obj["conn-str"].as_string().c_str()); + if (config_obj.contains("log-dir")) + config.set_log_path(std::string{ config_obj["log-dir"].as_string() }); if (!config_obj.contains("template_root")) { std::cerr << "`template_root` must be specified" << std::endl; return EXIT_FAILURE; diff --git a/WebApp/bserv/bserv.cpp b/WebApp/bserv/bserv.cpp index 732c1bd..48c5c9b 100644 --- a/WebApp/bserv/bserv.cpp +++ b/WebApp/bserv/bserv.cpp @@ -460,14 +460,16 @@ namespace bserv { ws_routes_{ std::move(ws_routes) } { init_logging(config); - // database connection - try { - db_conn_mgr_ = std::make_shared< - db_connection_manager>(config.get_db_conn_str(), config.get_num_db_conn()); - } - catch (const std::exception& e) { - lgfatal << "db connection initialization failed: " << e.what() << std::endl; - exit(EXIT_FAILURE); + if (config.get_db_conn_str() != "") { + // database connection + try { + db_conn_mgr_ = std::make_shared< + db_connection_manager>(config.get_db_conn_str(), config.get_num_db_conn()); + } + catch (const std::exception& e) { + lgfatal << "db connection initialization failed: " << e.what() << std::endl; + exit(EXIT_FAILURE); + } } session_mgr_ = std::make_shared(); diff --git a/WebApp/bserv/include/bserv/config.hpp b/WebApp/bserv/include/bserv/config.hpp index a0bf656..35fcb4e 100644 --- a/WebApp/bserv/include/bserv/config.hpp +++ b/WebApp/bserv/include/bserv/config.hpp @@ -19,10 +19,12 @@ namespace bserv { const int EXPIRY_TIME = 30; // seconds const std::size_t LOG_ROTATION_SIZE = 8 * 1024 * 1024; - const std::string LOG_PATH = "./log/" + NAME; + //const std::string LOG_PATH = "./log/" + NAME; + const std::string LOG_PATH = ""; const int NUM_DB_CONN = 10; - const std::string DB_CONN_STR = "dbname=bserv"; + //const std::string DB_CONN_STR = "dbname=bserv"; + const std::string DB_CONN_STR = ""; #define decl_field(type, name, default_value) \ private: \ diff --git a/WebApp/bserv/include/bserv/logging.hpp b/WebApp/bserv/include/bserv/logging.hpp index babac4a..1c10c79 100644 --- a/WebApp/bserv/include/bserv/logging.hpp +++ b/WebApp/bserv/include/bserv/logging.hpp @@ -31,15 +31,22 @@ namespace bserv { // this function should be called before logging is used inline void init_logging(const server_config& config) { - logging::add_file_log( - keywords::file_name = config.get_log_path() + "_%Y%m%d_%H-%M-%S.%N.log", - keywords::rotation_size = config.get_log_rotation_size(), - keywords::format = "[%Severity%][%TimeStamp%][%ThreadID%]: %Message%" - ); + if (config.get_log_path() != "") { + std::string filename = config.get_log_path(); + if (!filename.ends_with('/')) { + filename += '/'; + } + filename += config.get_name(); + logging::add_file_log( + keywords::file_name = filename + "_%Y%m%d_%H-%M-%S.%N.log", + keywords::rotation_size = config.get_log_rotation_size(), + keywords::format = "[%Severity%][%TimeStamp%][%ThreadID%]: %Message%" + ); #if defined(_MSC_VER) && defined(_DEBUG) - // write to console as well - logging::add_console_log(std::cout); + // write to console as well + logging::add_console_log(std::cout); #endif + } logging::core::get()->set_filter( #if defined(_MSC_VER) && defined(_DEBUG) logging::trivial::severity >= logging::trivial::trace diff --git a/config-Windows.json b/config-Windows.json index 8ecc295..fa4b409 100644 --- a/config-Windows.json +++ b/config-Windows.json @@ -4,5 +4,6 @@ "conn-num": 4, "conn-str": "postgresql://[username]:[password]@[url]:[port]/[db]", "static_root": "../../templates/statics", - "template_root": "../../templates" + "template_root": "../../templates", + "log-dir": "./log" } \ No newline at end of file diff --git a/config-ubuntu.json b/config-ubuntu.json index 51651ea..3111bf7 100644 --- a/config-ubuntu.json +++ b/config-ubuntu.json @@ -4,5 +4,6 @@ "conn-num": 4, "conn-str": "postgresql://[username]:[password]@[url]:[port]/[db]", "static_root": "../../../templates/statics", - "template_root": "../../../templates" + "template_root": "../../../templates", + "log-dir": "./log" }