Skip to content

Commit

Permalink
Allow configuration of broker log level through command line
Browse files Browse the repository at this point in the history
  • Loading branch information
kleunen committed Dec 21, 2020
1 parent 9b14967 commit 7aadd03
Show file tree
Hide file tree
Showing 11 changed files with 253 additions and 54 deletions.
45 changes: 38 additions & 7 deletions .github/workflows/gha.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ on:
types: [opened, synchronize]
push:
branches:
- '*'
- master
tags:
- '*'

Expand Down Expand Up @@ -101,21 +101,52 @@ jobs:
uses: actions/checkout@v2
- name: Configure
env:
S_CFLAGS:
S_CXXFLAGS: -DBOOST_ASIO_NO_DEPRECATED -DBOOST_MULTI_INDEX_DISABLE_SERIALIZATION -DBOOST_MULTI_INDEX_ENABLE_INVARIANT_CHECKING
S_LDFLAGS:
S_CFLAGS:
S_CXXFLAGS: -DBOOST_ASIO_NO_DEPRECATED -DBOOST_MULTI_INDEX_DISABLE_SERIALIZATION
S_LDFLAGS: -static -static-libgcc -static-libstdc++
run: |
export CFLAGS=${S_CFLAGS} && export CXXFLAGS=${S_CXXFLAGS} && export LDFLAGS=${S_LDFLAGS}
FLAGS="-DCMAKE_CXX_COMPILER=clang++ -DMQTT_USE_STATIC_BOOST=ON -DMQTT_TEST_1=OFF -DMQTT_TEST_2=ONFF -DMQTT_TEST_3=OFF -DMQTT_TEST_4=OFF -DMQTT_TEST_5=OFF -DMQTT_TEST_6=OFF -DMQTT_TEST_7=OFF -DMQTT_BUILD_EXAMPLES=ON -DCMAKE_BUILD_TYPE=Release -DMQTT_USE_WS=ON -DMQTT_USE_STR_CHECK=OFF -DMQTT_USE_LOG=ON -DMQTT_STD_ANY=OFF -DMQTT_STD_OPTIONAL=OFF -DMQTT_STD_VARIANT=OFF -DMQTT_STD_STRING_VIEW=OFF -DMQTT_STD_SHARED_PTR_ARRAY=OFF"
FLAGS="-DCMAKE_CXX_COMPILER=clang++ -DMQTT_USE_STATIC_BOOST=ON -DMQTT_USE_STATIC_OPENSSL=ON -DMQTT_TEST_1=OFF -DMQTT_TEST_2=OFF -DMQTT_TEST_3=OFF -DMQTT_TEST_4=OFF -DMQTT_TEST_5=OFF -DMQTT_TEST_6=OFF -DMQTT_TEST_7=OFF -DMQTT_BUILD_EXAMPLES=ON -DCMAKE_BUILD_TYPE=Release -DMQTT_USE_WS=ON -DMQTT_USE_TLS=ON -DMQTT_USE_STR_CHECK=OFF -DMQTT_USE_LOG=ON -DMQTT_STD_ANY=OFF -DMQTT_STD_OPTIONAL=OFF -DMQTT_STD_VARIANT=OFF -DMQTT_STD_STRING_VIEW=OFF -DMQTT_STD_SHARED_PTR_ARRAY=OFF"
BOOST_ROOT=$BOOST_ROOT_1_72_0 cmake -S ${{ github.workspace }} -B ${{ runner.temp }} ${FLAGS} -DCMAKE_C_FLAGS="${CFLAGS}" -DCMAKE_CXX_FLAGS="${CXXFLAGS}" -DCMAKE_EXE_LINKER_FLAGS="${LDFLAGS}"
- name: Check Header Dependencies
run: |
cmake --build ${{ runner.temp }} --parallel $(nproc) --clean-first --target check_deps
- name: Compile
run: |
VERBOSE=1 cmake --build ${{ runner.temp }} --clean-first
VERBOSE=1 cmake --build ${{ runner.temp }} --clean-first --target broker
sudo apt-get install upx-ucl
upx-ucl --ultra-brute ${{ runner.temp }}/example/broker
- name: 'Upload Broker'
uses: actions/upload-artifact@v2
with:
name: broker
name: linux-static-broker
path: ${{ runner.temp }}/example/broker

macos-release:
runs-on: macos-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Install Dependencies
run: |
brew install boost openssl
- name: Configure
env:
OPENSSL_ROOT_DIR: /usr/local/opt/openssl
run: |
cmake -S ${{ github.workspace }} -B ${{ runner.temp }} ${{ matrix.pattern }} -DCMAKE_CXX_COMPILER=clang++ -DMQTT_USE_STATIC_BOOST=ON -DMQTT_USE_STATIC_OPENSSL=ON -DMQTT_TEST_1=OFF -DMQTT_TEST_2=OFF -DMQTT_TEST_3=OFF -DMQTT_TEST_4=OFF -DMQTT_TEST_5=OFF -DMQTT_TEST_6=OFF -DMQTT_TEST_7=OFF -DMQTT_BUILD_EXAMPLES=ON -DCMAKE_BUILD_TYPE=Release -DMQTT_USE_WS=ON -DMQTT_USE_TLS=ON -DMQTT_USE_STR_CHECK=OFF -DMQTT_USE_LOG=ON -DMQTT_STD_ANY=OFF -DMQTT_STD_OPTIONAL=OFF -DMQTT_STD_VARIANT=OFF -DMQTT_STD_STRING_VIEW=OFF -DMQTT_STD_SHARED_PTR_ARRAY=OFF
- name: Check Header Dependencies
run: |
cmake --build ${{ runner.temp }} --parallel $(sysctl -n hw.ncpu) --clean-first --target check_deps
- name: Compile
env:
CXXFLAGS: -Werror -pedantic -Wall -Wextra -Wno-ignored-qualifiers
LDFLAGS: -Wconversion
run: |
cmake --build ${{ runner.temp }} --parallel $(sysctl -n hw.ncpu) --clean-first --target broker
- name: 'Upload Broker'
uses: actions/upload-artifact@v2
with:
name: macos-broker
path: ${{ runner.temp }}/example/broker

15 changes: 12 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -123,16 +123,25 @@ FIND_PACKAGE (Threads REQUIRED)

IF (MQTT_USE_LOG)
MESSAGE (STATUS "Logging enabled")
FIND_PACKAGE (Boost 1.67.0 REQUIRED COMPONENTS system date_time log filesystem thread)
FIND_PACKAGE (Boost 1.67.0 REQUIRED COMPONENTS system date_time log filesystem thread program_options)
ELSE ()
MESSAGE (STATUS "Logging disabled")
FIND_PACKAGE (Boost 1.67.0 REQUIRED COMPONENTS system date_time)
FIND_PACKAGE (Boost 1.67.0 REQUIRED COMPONENTS system date_time program_options)
ENDIF ()

IF (MQTT_USE_TLS)
FIND_PACKAGE (OpenSSL REQUIRED)
IF (MQTT_USE_STATIC_OPENSSL)
FIND_PACKAGE (ZLIB REQUIRED)
IF (UNIX AND NOT APPLE)
# Try to find static zlib library also
set(_CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES})
SET(CMAKE_FIND_LIBRARY_SUFFIXES ".a")
FIND_PACKAGE (ZLIB REQUIRED)
set(CMAKE_FIND_LIBRARY_SUFFIXES ${_CMAKE_FIND_LIBRARY_SUFFIXES})
unset(_CMAKE_FIND_LIBRARY_SUFFIXES)
ELSE()
FIND_PACKAGE (ZLIB REQUIRED)
ENDIF ()
ENDIF ()
ENDIF ()

Expand Down
2 changes: 2 additions & 0 deletions example/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ FOREACH (source_file ${exec_PROGRAMS})
TARGET_COMPILE_DEFINITIONS (${source_file_we} PUBLIC $<IF:$<BOOL:${MQTT_USE_STATIC_BOOST}>,,BOOST_LOG_DYN_LINK>)
TARGET_LINK_LIBRARIES (${source_file_we} Boost::log)
ENDIF ()
TARGET_COMPILE_DEFINITIONS (${source_file_we} PUBLIC $<IF:$<BOOL:${MQTT_USE_STATIC_BOOST}>,,BOOST_PROGRAM_OPTIONS_DYN_LINK>)
TARGET_LINK_LIBRARIES (${source_file_we} Boost::program_options)
ENDFOREACH ()

IF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC")
Expand Down
165 changes: 157 additions & 8 deletions example/broker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,165 @@
// http://www.boost.org/LICENSE_1_0.txt)

#include <mqtt/config.hpp>

#include "../test/system/test_server_no_tls.hpp"
#include "../test/system/test_server_no_tls_ws.hpp"
#include "../test/system/test_server_tls.hpp"
#include "../test/system/test_server_tls_ws.hpp"
#include <mqtt/setup_log.hpp>

#include <mqtt/broker/broker.hpp>
#include <boost/program_options.hpp>

#include <fstream>

#if defined(MQTT_USE_TLS)
boost::asio::ssl::context init_ctx(boost::program_options::variables_map const& vm)
{
boost::asio::ssl::context ctx(boost::asio::ssl::context::tlsv12);
ctx.set_options(
boost::asio::ssl::context::default_workarounds |
boost::asio::ssl::context::single_dh_use);
ctx.use_certificate_file(vm["tls.certificate"].as<std::string>(), boost::asio::ssl::context::pem);
ctx.use_private_key_file(vm["tls.private_key"].as<std::string>(), boost::asio::ssl::context::pem);
return ctx;
}
#endif // defined(MQTT_USE_TLS)

void run_broker(boost::program_options::variables_map const& vm)
{
try {
boost::asio::io_context ioc;
MQTT_NS::broker::broker_t b(ioc);

std::unique_ptr<test_server_no_tls> s;
if(vm["tcp.enable"].as<unsigned int>()) {
s = std::make_unique<test_server_no_tls>(ioc, b, vm["tcp.port"].as<uint16_t>());
}

#if defined(MQTT_USE_WS)
std::unique_ptr<test_server_no_tls_ws> s_ws;
if(vm["ws.enable"].as<unsigned int>()) {
s_ws = std::make_unique<test_server_no_tls_ws>(ioc, b, vm["ws.port"].as<uint16_t>());
}
#endif // defined(MQTT_USE_WS)

#if defined(MQTT_USE_TLS)
std::unique_ptr<test_server_tls> s_tls;
if (vm["tls.enable"].as<unsigned int>()) {
s_tls = std::make_unique<test_server_tls>(ioc, init_ctx(vm), b, vm["tls.port"].as<uint16_t>());
}
#endif // defined(MQTT_USE_TLS)

#if defined(MQTT_USE_TLS) && defined(MQTT_USE_WS)
std::unique_ptr<test_server_tls_ws> s_tls_ws;
if (vm["tlsws.enable"].as<unsigned int>()) {
s_tls_ws = std::make_unique<test_server_tls_ws>(ioc, init_ctx(vm), b, vm["tlsws.port"].as<uint16_t>());
}
#endif // defined(MQTT_USE_TLS) && defined(MQTT_USE_WS)


ioc.run();
} catch(std::exception &e) {
MQTT_LOG("mqtt_broker", error) << e.what();
}
}

int main(int argc, char **argv) {
try {
boost::program_options::options_description desc;

boost::program_options::options_description general_desc("General options");
general_desc.add_options()
("help", "produce help message")
("cfg", boost::program_options::value<std::string>()->default_value(""), "Load configuration file")
#if defined(MQTT_USE_LOG)
("verbose", boost::program_options::value<unsigned int>()->default_value(1), "set verbose level, possible values:\n 0 - Fatal\n 1 - Error\n 2 - Warning\n 3 - Info\n 4 - Debug\n 5 - Trace")
#endif // defined(MQTT_USE_LOG)
;

boost::program_options::options_description notls_desc("TCP Server options");
notls_desc.add_options()
("tcp.port", boost::program_options::value<uint16_t>()->default_value(broker_notls_port), "default port (TCP)")
("tcp.enable", boost::program_options::value<unsigned int>()->default_value(1), "0/1")
;

desc.add(general_desc).add(notls_desc);

#if defined(MQTT_USE_WS)
boost::program_options::options_description ws_desc("TCP Websocket Server options");
ws_desc.add_options()
("ws.port", boost::program_options::value<uint16_t>()->default_value(broker_notls_ws_port), "default port (TCP)")
("ws.enable", boost::program_options::value<unsigned int>()->default_value(0), "0/1")
;

desc.add(ws_desc);
#endif // defined(MQTT_USE_WS)

#if defined(MQTT_USE_TLS)
boost::program_options::options_description tls_desc("TLS Server options");
tls_desc.add_options()
("tls.port", boost::program_options::value<uint16_t>()->default_value(broker_tls_port), "default port (TLS)")
("tls.enable", boost::program_options::value<unsigned int>()->default_value(0), "0/1")
("tls.certificate", boost::program_options::value<std::string>()->default_value("server.crt.pem"), "Certificate file")
("tls.private_key", boost::program_options::value<std::string>()->default_value("server.key.pem"), "Private key file")
;
desc.add(tls_desc);
#endif // defined(MQTT_USE_TLS)

#if defined(MQTT_USE_WS) && defined(MQTT_USE_TLS)
boost::program_options::options_description tlsws_desc("TLS Websocket Server options");
tlsws_desc.add_options()
("tlsws.port", boost::program_options::value<uint16_t>()->default_value(broker_tls_ws_port), "default port (TLS)")
("tlsws.enable", boost::program_options::value<unsigned int>()->default_value(0), "0/1")
;
desc.add(tlsws_desc);
#endif // defined(MQTT_USE_TLS) && defined(MQTT_USE_WS)

boost::program_options::variables_map vm;
boost::program_options::store(boost::program_options::parse_command_line(argc, argv, desc), vm);
if (!vm["cfg"].as<std::string>().empty()) {
std::ifstream input(vm["cfg"].as<std::string>());
if (input.good()) {
boost::program_options::store(boost::program_options::parse_config_file(input, desc), vm);
} else
{
throw std::runtime_error("Failed to open specified configuration file");
}
}

boost::program_options::notify(vm);

if (vm.count("help")) {
std::cout << desc << std::endl;
return 1;
}

#if defined(MQTT_USE_LOG)
switch (vm["verbose"].as<unsigned int>()) {
case 5:
MQTT_NS::setup_log(mqtt::severity_level::trace);
break;
case 4:
MQTT_NS::setup_log(mqtt::severity_level::debug);
break;
case 3:
MQTT_NS::setup_log(mqtt::severity_level::info);
break;
case 2:
MQTT_NS::setup_log(mqtt::severity_level::warning);
break;
default:
MQTT_NS::setup_log(mqtt::severity_level::error);
break;
case 0:
MQTT_NS::setup_log(mqtt::severity_level::fatal);
break;
}
#else
MQTT_NS::setup_log();
#endif

int main() {
MQTT_NS::setup_log();
boost::asio::io_context ioc;
MQTT_NS::broker::broker_t b(ioc);
test_server_no_tls s(ioc, b);
ioc.run();
run_broker(vm);
} catch(std::exception &e) {
std::cerr << e.what() << std::endl;
}
}
5 changes: 3 additions & 2 deletions test/system/combi_test.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "test_server_no_tls.hpp"
#if defined(MQTT_USE_TLS)
#include "test_server_tls.hpp"
#include "test_ctx_init.hpp"
#endif // defined(MQTT_USE_TLS)

#if defined(MQTT_USE_WS)
Expand Down Expand Up @@ -91,7 +92,7 @@ inline void do_tls_test(
auto f = p.get_future();
std::thread th(
[&] {
s.emplace(iocb, b);
s.emplace(iocb, test_ctx_init(), b);
p.set_value();
iocb.run();
}
Expand Down Expand Up @@ -193,7 +194,7 @@ inline void do_tls_ws_test(
auto f = p.get_future();
std::thread th(
[&] {
s.emplace(iocb, b);
s.emplace(iocb, test_ctx_init(), b);
p.set_value();
iocb.run();
}
Expand Down
9 changes: 3 additions & 6 deletions test/system/st_underlying_timeout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,11 @@ BOOST_AUTO_TEST_CASE( connect_tls_ws_ashs ) {
as::io_context ioc;

// server
ctx_init ci;
MQTT_NS::server_tls_ws<> server(
as::ip::tcp::endpoint(
as::ip::tcp::v4(),
broker_tls_ws_port),
std::move(ci.ctx),
test_ctx_init(),
ioc);

server.set_accept_handler(
Expand Down Expand Up @@ -153,12 +152,11 @@ BOOST_AUTO_TEST_CASE( connect_tls_ws_upg ) {
as::io_context ioc;

// server
ctx_init ci;
MQTT_NS::server_tls_ws<> server(
as::ip::tcp::endpoint(
as::ip::tcp::v4(),
broker_tls_ws_port),
std::move(ci.ctx),
test_ctx_init(),
ioc);

server.set_accept_handler(
Expand Down Expand Up @@ -236,12 +234,11 @@ BOOST_AUTO_TEST_CASE( connect_tls_ashs ) {
as::io_context ioc;

// server
ctx_init ci;
MQTT_NS::server_tls<> server(
as::ip::tcp::endpoint(
as::ip::tcp::v4(),
broker_tls_port),
std::move(ci.ctx),
test_ctx_init(),
ioc);

server.set_accept_handler(
Expand Down
26 changes: 13 additions & 13 deletions test/system/test_ctx_init.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@

#include "test_settings.hpp"

struct ctx_init {
ctx_init() : ctx(boost::asio::ssl::context::tlsv12) {
ctx.set_options(
boost::asio::ssl::context::default_workarounds |
boost::asio::ssl::context::single_dh_use);
std::string path = boost::unit_test::framework::master_test_suite().argv[0];
std::size_t pos = path.find_last_of("/\\");
std::string base = (pos == std::string::npos) ? "" : path.substr(0, pos + 1);
ctx.use_certificate_file(base + "server.crt.pem", boost::asio::ssl::context::pem);
ctx.use_private_key_file(base + "server.key.pem", boost::asio::ssl::context::pem);
}
boost::asio::ssl::context ctx;
};
static inline boost::asio::ssl::context test_ctx_init() {
boost::asio::ssl::context ctx(boost::asio::ssl::context::tlsv12);
ctx.set_options(
boost::asio::ssl::context::default_workarounds |
boost::asio::ssl::context::single_dh_use);
std::string path = boost::unit_test::framework::master_test_suite().argv[0];
std::size_t pos = path.find_last_of("/\\");
std::string base = (pos == std::string::npos) ? "" : path.substr(0, pos + 1);
ctx.use_certificate_file(base + "server.crt.pem", boost::asio::ssl::context::pem);
ctx.use_private_key_file(base + "server.key.pem", boost::asio::ssl::context::pem);
return ctx;
}


#endif // defined(MQTT_USE_TLS)

Expand Down
4 changes: 2 additions & 2 deletions test/system/test_server_no_tls.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ using con_sp_t = std::shared_ptr<con_t>;

class test_server_no_tls {
public:
test_server_no_tls(as::io_context& ioc, MQTT_NS::broker::broker_t& b)
test_server_no_tls(as::io_context& ioc, MQTT_NS::broker::broker_t& b, uint16_t port = broker_notls_port)
: server_(
as::ip::tcp::endpoint(
as::ip::tcp::v4(), broker_notls_port
as::ip::tcp::v4(), port
),
ioc,
ioc,
Expand Down
Loading

0 comments on commit 7aadd03

Please sign in to comment.