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);