Merged
Merge Request #19 · created by Steven de Ridder


added logging struct to mqtt connect function.

added a LogSettings struct which allows the user to set the log level and mask in the mqtt connect.


From default_logging_level into development

Merged by Peter M. Groen

Source branch has been removed
2 participants

examples/pub/publisher.cpp
... ... @@ -31,10 +31,11 @@ Publisher::Publisher()
31 31  
32 32 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)
33 33 {
34   - m_mqtt_client.setLogLevel( osdev::components::log::LogLevel::Info );
35   - m_mqtt_client.setMask( osdev::components::log::LogMask::Upto );
  34 + m_mqtt_client.connect( hostname, portnumber,
  35 + osdev::components::mqtt::Credentials( username, password ),
  36 + osdev::components::mqtt::mqtt_LWT( lwt_topic, lwt_message ), true,
  37 + osdev::components::log::LogSettings{ osdev::components::log::LogLevel::Debug, osdev::components::log::LogMask::None } );
36 38  
37   - m_mqtt_client.connect( hostname, portnumber, osdev::components::mqtt::Credentials( username, password ), osdev::components::mqtt::mqtt_LWT( lwt_topic, lwt_message ) );
38 39 std::cout << "Client state : " << m_mqtt_client.state() << std::endl;
39 40 }
40 41  
... ...
include/imqttclient.h
... ... @@ -37,9 +37,10 @@
37 37 // mlogic::mqtt
38 38 #include "connectionstatus.h"
39 39 #include "credentials.h"
  40 +#include "log.h"
40 41 #include "mqttmessage.h"
41   -#include "token.h"
42 42 #include "mqtt_lwt.h"
  43 +#include "token.h"
43 44  
44 45 namespace osdev {
45 46 namespace components {
... ... @@ -60,13 +61,16 @@ public:
60 61 * @param port The port to use.
61 62 * @param credentials The credentials to use.
62 63 */
63   - virtual void connect(const std::string& host, int port, const Credentials& credentials, const mqtt_LWT &lwt = mqtt_LWT(), bool blocking = false ) = 0;
  64 + virtual void connect( const std::string& host, int port, const Credentials& credentials,
  65 + const mqtt_LWT &lwt = mqtt_LWT(), bool blocking = false,
  66 + const log::LogSettings &log_settings = log::LogSettings() ) = 0;
64 67  
65 68 /**
66 69 * @brief Connect to the endpoint
67 70 * @param endpoint an uri endpoint description.
68 71 */
69   - virtual void connect(const std::string& endpoint, const mqtt_LWT &lwt = mqtt_LWT(), bool blocking = false ) = 0;
  72 + virtual void connect( const std::string& endpoint, const mqtt_LWT &lwt = mqtt_LWT(),
  73 + bool blocking = false, const log::LogSettings &log_settings = log::LogSettings() ) = 0;
70 74  
71 75 /**
72 76 * @brief Disconnect the client from the broker
... ...
include/log.h
... ... @@ -105,6 +105,12 @@ enum class LogMask
105 105 None
106 106 };
107 107  
  108 +struct LogSettings
  109 +{
  110 + LogLevel level = LogLevel::Info;
  111 + LogMask mask = LogMask::Upto;
  112 +};
  113 +
108 114 /*! \class Log
109 115 \brief Basic logging mechanism.
110 116 */
... ...
include/mqttclient.h
... ... @@ -115,12 +115,15 @@ public:
115 115 /**
116 116 * @see IMqttClient
117 117 */
118   - virtual void connect( const std::string& host, int port, const Credentials &credentials, const mqtt_LWT &lwt = mqtt_LWT(), bool blocking = false ) override;
  118 + virtual void connect( const std::string& host, int port, const Credentials &credentials,
  119 + const mqtt_LWT &lwt = mqtt_LWT(), bool blocking = false,
  120 + const log::LogSettings &log_settings = log::LogSettings() ) override;
119 121  
120 122 /**
121 123 * @see IMqttClient
122 124 */
123   - virtual void connect( const std::string &endpoint, const mqtt_LWT &lwt = mqtt_LWT(), bool blocking = false ) override;
  125 + virtual void connect( const std::string &endpoint, const mqtt_LWT &lwt = mqtt_LWT(),
  126 + bool blocking = false, const log::LogSettings &log_settings = log::LogSettings() ) override;
124 127  
125 128 /**
126 129 * @see IMqttClient
... ...
src/mqttclient.cpp
... ... @@ -124,7 +124,7 @@ StateEnum MqttClient::state() const
124 124 return m_serverState.state();
125 125 }
126 126  
127   -void MqttClient::connect(const std::string& host, int port, const Credentials &credentials, const mqtt_LWT &lwt, bool blocking )
  127 +void MqttClient::connect(const std::string& host, int port, const Credentials &credentials, const mqtt_LWT &lwt, bool blocking, const LogSettings &log_settings )
128 128 {
129 129 osdev::components::mqtt::ParsedUri _endpoint = {
130 130 { "scheme", "tcp" },
... ... @@ -134,11 +134,14 @@ void MqttClient::connect(const std::string&amp; host, int port, const Credentials &amp;c
134 134 { "port", std::to_string(port) }
135 135 };
136 136  
137   - this->connect( UriParser::toString( _endpoint ), lwt, blocking );
  137 + this->connect( UriParser::toString( _endpoint ), lwt, blocking, log_settings );
138 138 }
139 139  
140   -void MqttClient::connect( const std::string &_endpoint, const mqtt_LWT &lwt, bool blocking )
  140 +void MqttClient::connect( const std::string &_endpoint, const mqtt_LWT &lwt, bool blocking, const LogSettings &log_settings )
141 141 {
  142 + Log::setLogLevel( log_settings.level );
  143 + Log::setMask( log_settings.mask );
  144 +
142 145 LogInfo( "MqttClient", std::string( m_clientId + " - Request connect" ) );
143 146  
144 147 OSDEV_COMPONENTS_LOCKGUARD(m_interfaceMutex);
... ...