-
mentioned in commit 8f48be6bfe32ba45a1c1ef04ed8e2dbbce307b29
-
Status changed to merged
-
moved log.h to include and added log setters to mqttclient (default up to info ) See merge request !16
-
Master See merge request !14
Showing
7 changed files
examples/pub/publisher.cpp
| ... | ... | @@ -31,7 +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.connect( hostname, portnumber, osdev::components::mqtt::Credentials( username, password ), osdev::components::mqtt::mqtt_LWT( lwt_topic, lwt_message ) ); | |
| 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 } ); | |
| 38 | + | |
| 35 | 39 | std::cout << "Client state : " << m_mqtt_client.state() << std::endl; |
| 36 | 40 | } |
| 37 | 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 | ... | ... |
src/log.h renamed to include/log.h
include/mqttclient.h
| ... | ... | @@ -31,12 +31,15 @@ |
| 31 | 31 | #include <vector> |
| 32 | 32 | |
| 33 | 33 | // osdev::components::mqtt |
| 34 | -#include "synchronizedqueue.h" | |
| 35 | -#include "istatecallback.h" | |
| 36 | -#include "serverstate.h" | |
| 37 | 34 | |
| 35 | +#include "istatecallback.h" | |
| 38 | 36 | #include "imqttclient.h" |
| 39 | 37 | #include "mqtt_lwt.h" |
| 38 | +#include "serverstate.h" | |
| 39 | +#include "synchronizedqueue.h" | |
| 40 | + | |
| 41 | +// osdev::components::logger | |
| 42 | +#include "log.h" | |
| 40 | 43 | |
| 41 | 44 | namespace osdev { |
| 42 | 45 | namespace components { |
| ... | ... | @@ -112,12 +115,15 @@ public: |
| 112 | 115 | /** |
| 113 | 116 | * @see IMqttClient |
| 114 | 117 | */ |
| 115 | - 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; | |
| 116 | 121 | |
| 117 | 122 | /** |
| 118 | 123 | * @see IMqttClient |
| 119 | 124 | */ |
| 120 | - 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; | |
| 121 | 127 | |
| 122 | 128 | /** |
| 123 | 129 | * @see IMqttClient |
| ... | ... | @@ -166,6 +172,22 @@ public: |
| 166 | 172 | */ |
| 167 | 173 | virtual std::string endpoint() const override; |
| 168 | 174 | |
| 175 | + /*! | |
| 176 | + * \brief setMask update the current logMask | |
| 177 | + * \param logMask - Enum defining the logmask used. | |
| 178 | + */ | |
| 179 | + void setMask ( osdev::components::log::LogMask logMask ); | |
| 180 | + /*! | |
| 181 | + * \brief setLogLevel update the current logLevel | |
| 182 | + * \param logLevel - Enum defining the logLevel used, in combination with Mask. | |
| 183 | + */ | |
| 184 | + void setLogLevel( osdev::components::log::LogLevel logLevel ); | |
| 185 | + /*! | |
| 186 | + * \brief setContext update the current context | |
| 187 | + * \param context - String containing the new context name. | |
| 188 | + */ | |
| 189 | + void setContext ( std::string context ); | |
| 190 | + | |
| 169 | 191 | private: |
| 170 | 192 | /*! |
| 171 | 193 | * \brief Callback used to pick up the connection status of the wrappers. | ... | ... |
src/CMakeLists.txt
src/log.cpp
| ... | ... | @@ -53,8 +53,8 @@ int toInt( LogLevel level ) |
| 53 | 53 | |
| 54 | 54 | std::string Log::s_context = std::string(); |
| 55 | 55 | std::string Log::s_fileName = std::string(); |
| 56 | -LogLevel Log::s_logLevel = LogLevel::Debug; | |
| 57 | -LogMask Log::s_logMask = LogMask::None; | |
| 56 | +LogLevel Log::s_logLevel = LogLevel::Info; | |
| 57 | +LogMask Log::s_logMask = LogMask::Upto; | |
| 58 | 58 | |
| 59 | 59 | void Log::init( const std::string& context, const std::string& logFile, LogLevel logDepth ) |
| 60 | 60 | { | ... | ... |
src/mqttclient.cpp
| ... | ... | @@ -21,9 +21,6 @@ |
| 21 | 21 | * ***************************************************************************/ |
| 22 | 22 | #include "mqttclient.h" |
| 23 | 23 | |
| 24 | -// osdev::components::logger | |
| 25 | -#include "log.h" | |
| 26 | - | |
| 27 | 24 | // osdev::components::mqtt |
| 28 | 25 | #include "clientpaho.h" |
| 29 | 26 | #include "mqttutil.h" |
| ... | ... | @@ -127,7 +124,7 @@ StateEnum MqttClient::state() const |
| 127 | 124 | return m_serverState.state(); |
| 128 | 125 | } |
| 129 | 126 | |
| 130 | -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 ) | |
| 131 | 128 | { |
| 132 | 129 | osdev::components::mqtt::ParsedUri _endpoint = { |
| 133 | 130 | { "scheme", "tcp" }, |
| ... | ... | @@ -137,11 +134,14 @@ void MqttClient::connect(const std::string& host, int port, const Credentials &c |
| 137 | 134 | { "port", std::to_string(port) } |
| 138 | 135 | }; |
| 139 | 136 | |
| 140 | - this->connect( UriParser::toString( _endpoint ), lwt, blocking ); | |
| 137 | + this->connect( UriParser::toString( _endpoint ), lwt, blocking, log_settings ); | |
| 141 | 138 | } |
| 142 | 139 | |
| 143 | -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 ) | |
| 144 | 141 | { |
| 142 | + Log::setLogLevel( log_settings.level ); | |
| 143 | + Log::setMask( log_settings.mask ); | |
| 144 | + | |
| 145 | 145 | LogInfo( "MqttClient", std::string( m_clientId + " - Request connect" ) ); |
| 146 | 146 | |
| 147 | 147 | OSDEV_COMPONENTS_LOCKGUARD(m_interfaceMutex); |
| ... | ... | @@ -613,3 +613,18 @@ void MqttClient::eventHandler() |
| 613 | 613 | } |
| 614 | 614 | LogInfo("[MqttClient::eventHandler]", std::string( m_clientId + " - leaving event handler." ) ); |
| 615 | 615 | } |
| 616 | + | |
| 617 | +void MqttClient::setMask(log::LogMask logMask ) | |
| 618 | +{ | |
| 619 | + Log::setMask( logMask ); | |
| 620 | +} | |
| 621 | + | |
| 622 | +void MqttClient::setLogLevel(log::LogLevel logLevel) | |
| 623 | +{ | |
| 624 | + Log::setLogLevel( logLevel ); | |
| 625 | +} | |
| 626 | + | |
| 627 | +void MqttClient::setContext(std::string context) | |
| 628 | +{ | |
| 629 | + Log::setContext( context ); | |
| 630 | +} | ... | ... |