diff --git a/candy-service/CMakeLists.txt b/candy-service/CMakeLists.txt index 20f5f700..33720d36 100644 --- a/candy-service/CMakeLists.txt +++ b/candy-service/CMakeLists.txt @@ -8,6 +8,7 @@ target_include_directories(candy-service PUBLIC set_target_properties(candy-service PROPERTIES OUTPUT_NAME "candy-service") +target_link_libraries(candy-service PRIVATE spdlog::spdlog) target_link_libraries(candy-service PRIVATE Poco::Foundation Poco::Net Poco::JSON Poco::Util) target_link_libraries(candy-service PRIVATE Threads::Threads) target_link_libraries(candy-service PRIVATE Candy::Library) diff --git a/candy-service/src/main.cc b/candy-service/src/main.cc index ffd090b6..fe3754a5 100644 --- a/candy-service/src/main.cc +++ b/candy-service/src/main.cc @@ -1,5 +1,6 @@ #include "candy/client.h" #include +#include #include #include #include @@ -19,6 +20,8 @@ #include #include #include +#include +#include #include #include @@ -143,6 +146,8 @@ class CandyServiceApp : public Poco::Util::ServerApplication { std::string bindAddress; int port = 0; bool helpRequested = false; + std::string logdir; + std::string loglevel; void initialize(Poco::Util::Application &self) override { loadConfiguration(); @@ -162,6 +167,16 @@ class CandyServiceApp : public Poco::Util::ServerApplication { .repeatable(false) .argument("address:port") .callback(Poco::Util::OptionCallback(this, &CandyServiceApp::handleBind))); + options.addOption(Poco::Util::Option("logdir", "", "Specify log directory") + .required(false) + .repeatable(false) + .argument("path") + .callback(Poco::Util::OptionCallback(this, &CandyServiceApp::handleLogDir))); + options.addOption(Poco::Util::Option("loglevel", "", "Specify log level") + .required(false) + .repeatable(false) + .argument("level") + .callback(Poco::Util::OptionCallback(this, &CandyServiceApp::handleLogLevel))); } void handleHelp(const std::string &name, const std::string &value) { @@ -186,6 +201,14 @@ class CandyServiceApp : public Poco::Util::ServerApplication { } } + void handleLogDir(const std::string &name, const std::string &dir) { + this->logdir = dir; + } + + void handleLogLevel(const std::string &name, const std::string &level) { + this->loglevel = level; + } + void displayHelp() { Poco::Util::HelpFormatter helpFormatter(options()); helpFormatter.setCommand(commandName()); @@ -197,6 +220,16 @@ class CandyServiceApp : public Poco::Util::ServerApplication { return Poco::Util::Application::EXIT_OK; } + if (!logdir.empty()) { + Poco::File(logdir).createDirectories(); + auto logger = spdlog::rotating_logger_mt("app", logdir + "/app.log", 10 * 1024 * 1024, 5, true); + spdlog::set_default_logger(logger); + } + + if (!loglevel.empty()) { + spdlog::set_level(spdlog::level::from_str(loglevel)); + } + if (bindAddress.empty()) { bindAddress = "localhost"; port = 26817;