Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions candy-service/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
33 changes: 33 additions & 0 deletions candy-service/src/main.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "candy/client.h"
#include <Poco/Exception.h>
#include <Poco/File.h>
#include <Poco/JSON/Object.h>
#include <Poco/JSON/Parser.h>
#include <Poco/Net/HTTPRequestHandler.h>
Expand All @@ -19,6 +20,8 @@
#include <iterator>
#include <map>
#include <mutex>
#include <spdlog/sinks/rotating_file_sink.h>
#include <spdlog/spdlog.h>
#include <sstream>
#include <thread>

Expand Down Expand Up @@ -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();
Expand All @@ -162,6 +167,16 @@ class CandyServiceApp : public Poco::Util::ServerApplication {
.repeatable(false)
.argument("address:port")
.callback(Poco::Util::OptionCallback<CandyServiceApp>(this, &CandyServiceApp::handleBind)));
options.addOption(Poco::Util::Option("logdir", "", "Specify log directory")
.required(false)
.repeatable(false)
.argument("path")
.callback(Poco::Util::OptionCallback<CandyServiceApp>(this, &CandyServiceApp::handleLogDir)));
options.addOption(Poco::Util::Option("loglevel", "", "Specify log level")
.required(false)
.repeatable(false)
.argument("level")
.callback(Poco::Util::OptionCallback<CandyServiceApp>(this, &CandyServiceApp::handleLogLevel)));
}

void handleHelp(const std::string &name, const std::string &value) {
Expand All @@ -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());
Expand All @@ -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;
Expand Down