diff --git a/examples/pub/publisher.cpp b/examples/pub/publisher.cpp index 90a6068..fa3e2af 100644 --- a/examples/pub/publisher.cpp +++ b/examples/pub/publisher.cpp @@ -31,7 +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.connect( hostname, portnumber, osdev::components::mqtt::Credentials( username, password ), osdev::components::mqtt::mqtt_LWT( lwt_topic, lwt_message ) ); + 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 } ); + 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/src/log.h b/include/log.h index 31e84a5..e22a77c 100644 --- a/src/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 7eaa795..85d620e 100644 --- a/include/mqttclient.h +++ b/include/mqttclient.h @@ -31,12 +31,15 @@ #include // osdev::components::mqtt -#include "synchronizedqueue.h" -#include "istatecallback.h" -#include "serverstate.h" +#include "istatecallback.h" #include "imqttclient.h" #include "mqtt_lwt.h" +#include "serverstate.h" +#include "synchronizedqueue.h" + +// osdev::components::logger +#include "log.h" namespace osdev { namespace components { @@ -112,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 @@ -166,6 +172,22 @@ public: */ virtual std::string endpoint() const override; + /*! + * \brief setMask update the current logMask + * \param logMask - Enum defining the logmask used. + */ + void setMask ( osdev::components::log::LogMask logMask ); + /*! + * \brief setLogLevel update the current logLevel + * \param logLevel - Enum defining the logLevel used, in combination with Mask. + */ + void setLogLevel( osdev::components::log::LogLevel logLevel ); + /*! + * \brief setContext update the current context + * \param context - String containing the new context name. + */ + void setContext ( std::string context ); + private: /*! * \brief Callback used to pick up the connection status of the wrappers. diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index dedee11..51a66ae 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -10,7 +10,6 @@ include(compiler) include_directories( ${CMAKE_SOURCE_DIR}/include - ${CMAKE_SOURCE_DIR}/submodules/logger/src ) set(SRC_LIST diff --git a/src/log.cpp b/src/log.cpp index 7feed80..95d8dfc 100644 --- a/src/log.cpp +++ b/src/log.cpp @@ -53,8 +53,8 @@ int toInt( LogLevel level ) std::string Log::s_context = std::string(); std::string Log::s_fileName = std::string(); -LogLevel Log::s_logLevel = LogLevel::Debug; -LogMask Log::s_logMask = LogMask::None; +LogLevel Log::s_logLevel = LogLevel::Info; +LogMask Log::s_logMask = LogMask::Upto; void Log::init( const std::string& context, const std::string& logFile, LogLevel logDepth ) { diff --git a/src/mqttclient.cpp b/src/mqttclient.cpp index e1454f4..5618b4b 100644 --- a/src/mqttclient.cpp +++ b/src/mqttclient.cpp @@ -21,9 +21,6 @@ * ***************************************************************************/ #include "mqttclient.h" -// osdev::components::logger -#include "log.h" - // osdev::components::mqtt #include "clientpaho.h" #include "mqttutil.h" @@ -127,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" }, @@ -137,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); @@ -613,3 +613,18 @@ void MqttClient::eventHandler() } LogInfo("[MqttClient::eventHandler]", std::string( m_clientId + " - leaving event handler." ) ); } + +void MqttClient::setMask(log::LogMask logMask ) +{ + Log::setMask( logMask ); +} + +void MqttClient::setLogLevel(log::LogLevel logLevel) +{ + Log::setLogLevel( logLevel ); +} + +void MqttClient::setContext(std::string context) +{ + Log::setContext( context ); +}