From 1a77e1b5e910767ff573fa9dcf45caf349aa32e5 Mon Sep 17 00:00:00 2001 From: Steven Date: Fri, 2 Sep 2022 12:15:13 +0200 Subject: [PATCH] added logging struct to mqtt connect function. --- examples/pub/publisher.cpp | 7 ++++--- include/imqttclient.h | 10 +++++++--- include/log.h | 6 ++++++ include/mqttclient.h | 7 +++++-- src/mqttclient.cpp | 9 ++++++--- 5 files changed, 28 insertions(+), 11 deletions(-) diff --git a/examples/pub/publisher.cpp b/examples/pub/publisher.cpp index f968a79..fa3e2af 100644 --- a/examples/pub/publisher.cpp +++ b/examples/pub/publisher.cpp @@ -31,10 +31,11 @@ Publisher::Publisher() void Publisher::connect(const std::string &hostname, int portnumber, const std::string &username, const std::string &password, const std::string &lwt_topic, const std::string &lwt_message) { - m_mqtt_client.setLogLevel( osdev::components::log::LogLevel::Info ); - m_mqtt_client.setMask( osdev::components::log::LogMask::Upto ); + m_mqtt_client.connect( hostname, portnumber, + osdev::components::mqtt::Credentials( username, password ), + osdev::components::mqtt::mqtt_LWT( lwt_topic, lwt_message ), true, + osdev::components::log::LogSettings{ osdev::components::log::LogLevel::Debug, osdev::components::log::LogMask::None } ); - m_mqtt_client.connect( hostname, portnumber, osdev::components::mqtt::Credentials( username, password ), osdev::components::mqtt::mqtt_LWT( lwt_topic, lwt_message ) ); std::cout << "Client state : " << m_mqtt_client.state() << std::endl; } diff --git a/include/imqttclient.h b/include/imqttclient.h index 4540909..b9cf350 100644 --- a/include/imqttclient.h +++ b/include/imqttclient.h @@ -37,9 +37,10 @@ // mlogic::mqtt #include "connectionstatus.h" #include "credentials.h" +#include "log.h" #include "mqttmessage.h" -#include "token.h" #include "mqtt_lwt.h" +#include "token.h" namespace osdev { namespace components { @@ -60,13 +61,16 @@ public: * @param port The port to use. * @param credentials The credentials to use. */ - virtual void connect(const std::string& host, int port, const Credentials& credentials, const mqtt_LWT &lwt = mqtt_LWT(), bool blocking = false ) = 0; + virtual void connect( const std::string& host, int port, const Credentials& credentials, + const mqtt_LWT &lwt = mqtt_LWT(), bool blocking = false, + const log::LogSettings &log_settings = log::LogSettings() ) = 0; /** * @brief Connect to the endpoint * @param endpoint an uri endpoint description. */ - virtual void connect(const std::string& endpoint, const mqtt_LWT &lwt = mqtt_LWT(), bool blocking = false ) = 0; + virtual void connect( const std::string& endpoint, const mqtt_LWT &lwt = mqtt_LWT(), + bool blocking = false, const log::LogSettings &log_settings = log::LogSettings() ) = 0; /** * @brief Disconnect the client from the broker diff --git a/include/log.h b/include/log.h index 31e84a5..e22a77c 100644 --- a/include/log.h +++ b/include/log.h @@ -105,6 +105,12 @@ enum class LogMask None }; +struct LogSettings +{ + LogLevel level = LogLevel::Info; + LogMask mask = LogMask::Upto; +}; + /*! \class Log \brief Basic logging mechanism. */ diff --git a/include/mqttclient.h b/include/mqttclient.h index 7e10d8d..85d620e 100644 --- a/include/mqttclient.h +++ b/include/mqttclient.h @@ -115,12 +115,15 @@ public: /** * @see IMqttClient */ - virtual void connect( const std::string& host, int port, const Credentials &credentials, const mqtt_LWT &lwt = mqtt_LWT(), bool blocking = false ) override; + virtual void connect( const std::string& host, int port, const Credentials &credentials, + const mqtt_LWT &lwt = mqtt_LWT(), bool blocking = false, + const log::LogSettings &log_settings = log::LogSettings() ) override; /** * @see IMqttClient */ - virtual void connect( const std::string &endpoint, const mqtt_LWT &lwt = mqtt_LWT(), bool blocking = false ) override; + virtual void connect( const std::string &endpoint, const mqtt_LWT &lwt = mqtt_LWT(), + bool blocking = false, const log::LogSettings &log_settings = log::LogSettings() ) override; /** * @see IMqttClient diff --git a/src/mqttclient.cpp b/src/mqttclient.cpp index 48ea2a4..5618b4b 100644 --- a/src/mqttclient.cpp +++ b/src/mqttclient.cpp @@ -124,7 +124,7 @@ StateEnum MqttClient::state() const return m_serverState.state(); } -void MqttClient::connect(const std::string& host, int port, const Credentials &credentials, const mqtt_LWT &lwt, bool blocking ) +void MqttClient::connect(const std::string& host, int port, const Credentials &credentials, const mqtt_LWT &lwt, bool blocking, const LogSettings &log_settings ) { osdev::components::mqtt::ParsedUri _endpoint = { { "scheme", "tcp" }, @@ -134,11 +134,14 @@ void MqttClient::connect(const std::string& host, int port, const Credentials &c { "port", std::to_string(port) } }; - this->connect( UriParser::toString( _endpoint ), lwt, blocking ); + this->connect( UriParser::toString( _endpoint ), lwt, blocking, log_settings ); } -void MqttClient::connect( const std::string &_endpoint, const mqtt_LWT &lwt, bool blocking ) +void MqttClient::connect( const std::string &_endpoint, const mqtt_LWT &lwt, bool blocking, const LogSettings &log_settings ) { + Log::setLogLevel( log_settings.level ); + Log::setMask( log_settings.mask ); + LogInfo( "MqttClient", std::string( m_clientId + " - Request connect" ) ); OSDEV_COMPONENTS_LOCKGUARD(m_interfaceMutex); -- libgit2 0.21.4