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
8 changes: 0 additions & 8 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ set(CMAKE_SKIP_BUILD_RPATH TRUE)

if (${CANDY_STATIC})
set(CANDY_STATIC_OPENSSL 1)
set(CANDY_STATIC_SPDLOG 1)
set(CANDY_STATIC_NLOHMANN_JSON 1)
set(CANDY_STATIC_POCO 1)
endif()

Expand Down Expand Up @@ -64,12 +62,6 @@ else()
find_package(OpenSSL REQUIRED)
endif()

if (${CANDY_STATIC_SPDLOG})
Fetch(spdlog "https://github.com/gabime/spdlog.git" "v1.15.3")
else()
find_package(spdlog REQUIRED)
endif()

if (${CANDY_STATIC_POCO})
set(ENABLE_DATA OFF CACHE BOOL "" FORCE)
set(ENABLE_DATA_MYSQL OFF CACHE BOOL "" FORCE)
Expand Down
1 change: 0 additions & 1 deletion candy-cli/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ target_include_directories(candy-cli PUBLIC

set_target_properties(candy-cli PROPERTIES OUTPUT_NAME "candy")

target_link_libraries(candy-cli PRIVATE spdlog::spdlog)
target_link_libraries(candy-cli PRIVATE Poco::Foundation Poco::JSON)
target_link_libraries(candy-cli PRIVATE Candy::Library)

Expand Down
35 changes: 26 additions & 9 deletions candy-cli/src/config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,21 @@
#include "config.h"
#include "argparse.h"
#include "candy/candy.h"
#include "utils/log.h"
#include <Poco/ConsoleChannel.h>
#include <Poco/Format.h>
#include <Poco/FormattingChannel.h>
#include <Poco/JSON/Object.h>
#include <Poco/Logger.h>
#include <Poco/Message.h>
#include <Poco/PatternFormatter.h>
#include <Poco/Platform.h>
#include <Poco/String.h>
#include <filesystem>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <spdlog/spdlog.h>
#include <sstream>
#include <string>

Expand Down Expand Up @@ -53,8 +59,7 @@ int arguments::parse(int argc, char *argv[]) {
program.add_argument("-w", "--websocket")
.help("WebSocket signaling address (e.g. \"ws://host:port/ws\").\n"
"Client supports ws:// and wss://; server supports ws:// only.")
.metavar("<url>")
.required();
.metavar("<url>");

program.add_argument("-p", "--password")
.help("pre-shared key for authentication and P2P encryption.\n"
Expand Down Expand Up @@ -192,11 +197,23 @@ int arguments::parse(int argc, char *argv[]) {
exit(1);
}

// Set up default console channel with pattern
if (this->noTimestamp) {
spdlog::set_pattern("[%^%l%$] %v");
Poco::AutoPtr<Poco::PatternFormatter> pFormatter = new Poco::PatternFormatter("[%q] %t");
Poco::AutoPtr<Poco::FormattingChannel> pFormattingChannel = new Poco::FormattingChannel(pFormatter);
Poco::AutoPtr<Poco::ConsoleChannel> pConsoleChannel = new Poco::ConsoleChannel;
pFormattingChannel->setChannel(pConsoleChannel);
Poco::Logger::root().setChannel(pFormattingChannel);
} else {
Poco::AutoPtr<Poco::PatternFormatter> pFormatter = new Poco::PatternFormatter("%Y-%m-%d %H:%M:%S [%q] %t");
Poco::AutoPtr<Poco::FormattingChannel> pFormattingChannel = new Poco::FormattingChannel(pFormatter);
Poco::AutoPtr<Poco::ConsoleChannel> pConsoleChannel = new Poco::ConsoleChannel;
pFormattingChannel->setChannel(pConsoleChannel);
Poco::Logger::root().setChannel(pFormattingChannel);
}

if (this->debug) {
spdlog::set_level(spdlog::level::debug);
Poco::Logger::root().setLevel(Poco::Message::PRIO_DEBUG);
}
return 0;
} catch (const std::exception &e) {
Expand Down Expand Up @@ -235,11 +252,11 @@ void arguments::parseFile(std::string cfgFile) {
if (handler != cfgHandlers.end()) {
handler->second(trim(cfg.second));
} else {
spdlog::warn("unknown config: {}={}", cfg.first, cfg.second);
candy::logger().warning(Poco::format("unknown config: %s=%s", cfg.first, cfg.second));
}
}
} catch (std::exception &e) {
spdlog::error("parse config file failed: {}", e.what());
candy::logger().error(Poco::format("parse config file failed: %s", std::string(e.what())));
exit(1);
}
}
Expand Down Expand Up @@ -276,7 +293,7 @@ int saveTunAddress(const std::string &name, const std::string &cidr) {
}
return 0;
} catch (std::exception &e) {
spdlog::critical("save latest address failed: {}", e.what());
candy::logger().fatal(Poco::format("save latest address failed: %s", std::string(e.what())));
return -1;
}
}
Expand Down Expand Up @@ -329,7 +346,7 @@ std::string initVirtualMac() {
}
return vmac;
} catch (std::exception &e) {
spdlog::critical("init vmac failed: {}", e.what());
candy::logger().fatal(Poco::format("init vmac failed: %s", std::string(e.what())));
return "";
}
}
Expand Down
2 changes: 1 addition & 1 deletion candy-cli/src/main.cc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "candy/candy.h"
#include "config.h"
#include <signal.h>
#include <spdlog/spdlog.h>
#include <thread>

int main(int argc, char *argv[]) {
arguments args;
Expand Down
1 change: 0 additions & 1 deletion candy-service/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ 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
47 changes: 40 additions & 7 deletions candy-service/src/main.cc
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
#include "candy/client.h"
#include "utils/log.h"
#include <Poco/AutoPtr.h>
#include <Poco/ConsoleChannel.h>
#include <Poco/Exception.h>
#include <Poco/File.h>
#include <Poco/FileChannel.h>
#include <Poco/Format.h>
#include <Poco/FormattingChannel.h>
#include <Poco/JSON/Object.h>
#include <Poco/JSON/Parser.h>
#include <Poco/Logger.h>
#include <Poco/Message.h>
#include <Poco/Net/HTTPRequestHandler.h>
#include <Poco/Net/HTTPRequestHandlerFactory.h>
#include <Poco/Net/HTTPServer.h>
#include <Poco/Net/HTTPServerRequest.h>
#include <Poco/Net/HTTPServerResponse.h>
#include <Poco/Net/ServerSocket.h>
#include <Poco/Net/SocketAddress.h>
#include <Poco/PatternFormatter.h>
#include <Poco/StreamCopier.h>
#include <Poco/Timestamp.h>
#include <Poco/Util/HelpFormatter.h>
Expand All @@ -20,8 +29,6 @@
#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 @@ -222,12 +229,38 @@ class CandyServiceApp : public Poco::Util::ServerApplication {

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);
Poco::AutoPtr<Poco::FileChannel> pFileChannel = new Poco::FileChannel(logdir + "/app.log");
pFileChannel->setProperty("rotation", "10 M");
pFileChannel->setProperty("archive", "number");
pFileChannel->setProperty("purgeCount", "5");
Poco::AutoPtr<Poco::PatternFormatter> pFormatter = new Poco::PatternFormatter("%Y-%m-%d %H:%M:%S [%q] %t");
Poco::AutoPtr<Poco::FormattingChannel> pFormattingChannel = new Poco::FormattingChannel(pFormatter);
pFormattingChannel->setChannel(pFileChannel);
Poco::Logger::root().setChannel(pFormattingChannel);
} else {
Poco::AutoPtr<Poco::PatternFormatter> pFormatter = new Poco::PatternFormatter("%Y-%m-%d %H:%M:%S [%q] %t");
Poco::AutoPtr<Poco::FormattingChannel> pFormattingChannel = new Poco::FormattingChannel(pFormatter);
Poco::AutoPtr<Poco::ConsoleChannel> pConsoleChannel = new Poco::ConsoleChannel;
pFormattingChannel->setChannel(pConsoleChannel);
Poco::Logger::root().setChannel(pFormattingChannel);
}

if (!loglevel.empty()) {
spdlog::set_level(spdlog::level::from_str(loglevel));
if (loglevel == "debug") {
Poco::Logger::root().setLevel(Poco::Message::PRIO_DEBUG);
} else if (loglevel == "information" || loglevel == "info") {
Poco::Logger::root().setLevel(Poco::Message::PRIO_INFORMATION);
} else if (loglevel == "warning" || loglevel == "warn") {
Poco::Logger::root().setLevel(Poco::Message::PRIO_WARNING);
} else if (loglevel == "error") {
Poco::Logger::root().setLevel(Poco::Message::PRIO_ERROR);
} else if (loglevel == "fatal" || loglevel == "critical") {
Poco::Logger::root().setLevel(Poco::Message::PRIO_FATAL);
} else if (loglevel == "trace") {
Poco::Logger::root().setLevel(Poco::Message::PRIO_TRACE);
} else {
candy::logger().warning(Poco::format("unknown log level: %s", loglevel));
}
}

if (bindAddress.empty()) {
Expand All @@ -247,10 +280,10 @@ class CandyServiceApp : public Poco::Util::ServerApplication {
Poco::Net::HTTPServer server(new JSONRequestHandlerFactory, socket, params);

server.start();
spdlog::info("bind: {}:{}", bindAddress, port);
candy::logger().information(Poco::format("bind: %s:%d", bindAddress, port));

waitForTerminationRequest();
spdlog::info("exit signal detected");
candy::logger().information("exit signal detected");

server.stop();

Expand Down
1 change: 0 additions & 1 deletion candy/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ else()
target_link_libraries(candy-library PRIVATE OpenSSL::SSL OpenSSL::Crypto)
endif()

target_link_libraries(candy-library PRIVATE spdlog::spdlog)
target_link_libraries(candy-library PRIVATE Poco::Foundation Poco::JSON Poco::Net Poco::NetSSL)
target_link_libraries(candy-library PRIVATE Threads::Threads)

Expand Down
11 changes: 6 additions & 5 deletions candy/src/candy/client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
#include "candy/client.h"
#include "core/client.h"
#include "utils/atomic.h"
#include "utils/log.h"
#include <Poco/Format.h>
#include <Poco/JSON/Object.h>
#include <Poco/JSON/Stringifier.h>
#include <map>
#include <memory>
#include <mutex>
#include <optional>
#include <shared_mutex>
#include <spdlog/spdlog.h>

namespace candy {
namespace client {
Expand Down Expand Up @@ -56,7 +57,7 @@ std::optional<std::shared_ptr<Instance>> try_create_instance(const std::string &
std::unique_lock lock(instance_mutex);
auto it = instance_map.find(id);
if (it != instance_map.end()) {
spdlog::warn("instance already exists: id={}", id);
candy::logger().warning(Poco::format("instance already exists: id=%s", id));
return std::nullopt;
}
auto manager = std::make_shared<Instance>();
Expand All @@ -83,7 +84,7 @@ bool run(const std::string &id, const Poco::JSON::Object &config) {
return oss.str();
};

spdlog::info("run enter: id={} config={}", id, toString(config));
candy::logger().information(Poco::format("run enter: id=%s config=%s", id, toString(config)));
while ((*instance)->is_running()) {
std::this_thread::sleep_for(std::chrono::seconds(1));
auto client = (*instance)->create_client();
Expand All @@ -100,7 +101,7 @@ bool run(const std::string &id, const Poco::JSON::Object &config) {
client->setLocalhost(config.getValue<std::string>("localhost"));
client->run();
}
spdlog::info("run exit: id={} ", id);
candy::logger().information(Poco::format("run exit: id=%s ", id));

return try_erase_instance(id);
}
Expand All @@ -109,7 +110,7 @@ bool shutdown(const std::string &id) {
std::shared_lock lock(instance_mutex);
auto it = instance_map.find(id);
if (it == instance_map.end()) {
spdlog::warn("instance not found: id={}", id);
candy::logger().warning(Poco::format("instance not found: id=%s", id));
return false;
}
if (auto instance = it->second) {
Expand Down
4 changes: 3 additions & 1 deletion candy/src/core/net.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// SPDX-License-Identifier: MIT
#include "core/net.h"
#include "utils/log.h"
#include <Poco/Format.h>
#include <Poco/Net/IPAddress.h>
#include <cstring>
#include <exception>
Expand Down Expand Up @@ -151,7 +153,7 @@ int Address::fromCidr(const std::string &cidr) {
host.fromString(cidr.substr(0UL, pos));
mask.fromPrefix(std::stoi(cidr.substr(pos + 1)));
} catch (std::exception &e) {
spdlog::warn("address parse cidr failed: {}: {}", e.what(), cidr);
candy::logger().warning(Poco::format("address parse cidr failed: %s: %s", std::string(e.what()), cidr));
return -1;
}
return 0;
Expand Down
4 changes: 0 additions & 4 deletions candy/src/core/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

#include <array>
#include <cstdint>
#include <spdlog/spdlog.h>
#include <string>
#include <type_traits>

Expand Down Expand Up @@ -90,7 +89,6 @@ struct __attribute__((packed)) SysRouteEntry {
IP4 nexthop;
};

/* 用于表示地址和掩码的组合,用于判断主机是否属于某个网络 */
class Address {
public:
Address();
Expand All @@ -100,10 +98,8 @@ class Address {
IP4 &Mask();
IP4 Net();

// 当前网络内的下一个地址
Address Next();

// 判断是否是有效的主机地址
bool isValid();

int fromCidr(const std::string &cidr);
Expand Down
Loading
Loading