Merged
Merge Request #19
·
created by
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
-
mentioned in commit b4a71d8cd80931c1d62b3a59b8f2d069b89e4a48
-
Status changed to merged
Showing
5 changed files
examples/pub/publisher.cpp
@@ -31,10 +31,11 @@ Publisher::Publisher() | @@ -31,10 +31,11 @@ Publisher::Publisher() | ||
31 | 31 | ||
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) | 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 | std::cout << "Client state : " << m_mqtt_client.state() << std::endl; | 39 | std::cout << "Client state : " << m_mqtt_client.state() << std::endl; |
39 | } | 40 | } |
40 | 41 |
include/imqttclient.h
@@ -37,9 +37,10 @@ | @@ -37,9 +37,10 @@ | ||
37 | // mlogic::mqtt | 37 | // mlogic::mqtt |
38 | #include "connectionstatus.h" | 38 | #include "connectionstatus.h" |
39 | #include "credentials.h" | 39 | #include "credentials.h" |
40 | +#include "log.h" | ||
40 | #include "mqttmessage.h" | 41 | #include "mqttmessage.h" |
41 | -#include "token.h" | ||
42 | #include "mqtt_lwt.h" | 42 | #include "mqtt_lwt.h" |
43 | +#include "token.h" | ||
43 | 44 | ||
44 | namespace osdev { | 45 | namespace osdev { |
45 | namespace components { | 46 | namespace components { |
@@ -60,13 +61,16 @@ public: | @@ -60,13 +61,16 @@ public: | ||
60 | * @param port The port to use. | 61 | * @param port The port to use. |
61 | * @param credentials The credentials to use. | 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 | * @brief Connect to the endpoint | 69 | * @brief Connect to the endpoint |
67 | * @param endpoint an uri endpoint description. | 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 | * @brief Disconnect the client from the broker | 76 | * @brief Disconnect the client from the broker |
include/log.h
@@ -105,6 +105,12 @@ enum class LogMask | @@ -105,6 +105,12 @@ enum class LogMask | ||
105 | None | 105 | None |
106 | }; | 106 | }; |
107 | 107 | ||
108 | +struct LogSettings | ||
109 | +{ | ||
110 | + LogLevel level = LogLevel::Info; | ||
111 | + LogMask mask = LogMask::Upto; | ||
112 | +}; | ||
113 | + | ||
108 | /*! \class Log | 114 | /*! \class Log |
109 | \brief Basic logging mechanism. | 115 | \brief Basic logging mechanism. |
110 | */ | 116 | */ |
include/mqttclient.h
@@ -115,12 +115,15 @@ public: | @@ -115,12 +115,15 @@ public: | ||
115 | /** | 115 | /** |
116 | * @see IMqttClient | 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 | * @see IMqttClient | 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 | * @see IMqttClient | 129 | * @see IMqttClient |
src/mqttclient.cpp
@@ -124,7 +124,7 @@ StateEnum MqttClient::state() const | @@ -124,7 +124,7 @@ StateEnum MqttClient::state() const | ||
124 | return m_serverState.state(); | 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 | osdev::components::mqtt::ParsedUri _endpoint = { | 129 | osdev::components::mqtt::ParsedUri _endpoint = { |
130 | { "scheme", "tcp" }, | 130 | { "scheme", "tcp" }, |
@@ -134,11 +134,14 @@ void MqttClient::connect(const std::string& host, int port, const Credentials &c | @@ -134,11 +134,14 @@ void MqttClient::connect(const std::string& host, int port, const Credentials &c | ||
134 | { "port", std::to_string(port) } | 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 | LogInfo( "MqttClient", std::string( m_clientId + " - Request connect" ) ); | 145 | LogInfo( "MqttClient", std::string( m_clientId + " - Request connect" ) ); |
143 | 146 | ||
144 | OSDEV_COMPONENTS_LOCKGUARD(m_interfaceMutex); | 147 | OSDEV_COMPONENTS_LOCKGUARD(m_interfaceMutex); |