diff --git a/include/log.h b/include/log.h index d0fb9ce..c3d18a2 100644 --- a/include/log.h +++ b/include/log.h @@ -195,6 +195,16 @@ private: static std::string fileinfoMessage(const char* file, int line, const std::string &message); + /** + * @brief Replace the characters in a std::string with other characters. + * @param strToReplace The string we want to replace characters in + * @param from_chars The characters we want to replace + * @param to_chars The characters we want to replace with. + * @return strToReplace This is the original string with the replaced characters. + */ + static void ReplaceAll( std::string &strToReplace, const std::string& from_chars, const std::string& to_chars ); + + //! The main context. static std::string s_context; diff --git a/include/threadcontext.h b/include/threadcontext.h index a12a664..eea96a4 100644 --- a/include/threadcontext.h +++ b/include/threadcontext.h @@ -45,10 +45,16 @@ class ThreadContext public: static ThreadContext& instance(); + const std::string& context() const + { + return m_context; + } + +private: /** - * @briefReturn the thread context. + * @brief Set the thread context. */ - void setContext( const std::string &contextString) + void setContext( const std::string &contextString ) { m_context = contextString; } diff --git a/src/log.cpp b/src/log.cpp index b2ff503..0bb4d0a 100644 --- a/src/log.cpp +++ b/src/log.cpp @@ -1,8 +1,15 @@ #include "log.h" -// std - +#include "threadcontext.h" +// std +#include +#include +#include +#include +#include +#include +#include using namespace osdev::components::mqtt; @@ -63,8 +70,39 @@ void Log::log( const std::string &category, const std::string &message, LogLevel else { // Replace % signs - logMessage.replace( '%', "%%"); + ReplaceAll( logMessage, "%", "%%" ); } writeLog( logCategory, logMessage, level ); } + +void Log::writeLog(const std::string &category, const std::string &message, LogLevel level) +{ + if( s_fileName.empty() ) + { + (void)level; + // To be implemented. + return; + } + else + { + setlogmask( LOG_UPTO( LOG_NOTICE ) ); + openlog( category.c_str(), LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1 ); + + syslog( LOG_NOTICE, "%s", message.c_str() ); + + closelog(); + } +} + +void Log::ReplaceAll( std::string &strToReplace, + const std::string& from_chars, + const std::string& to_chars ) +{ + size_t start_pos = 0; + while( ( start_pos = strToReplace.find( from_chars, start_pos ) ) != std::string::npos ) + { + strToReplace.replace( start_pos, from_chars.length(), to_chars ); + start_pos += to_chars.length(); // Handles case where 'to' is a substring of 'from' + } +} diff --git a/src/mqttclient.cpp b/src/mqttclient.cpp index 6a128f3..4b4a26d 100644 --- a/src/mqttclient.cpp +++ b/src/mqttclient.cpp @@ -26,8 +26,7 @@ #include "mqttutil.h" #include "mqttidgenerator.h" #include "mqtttypeconverter.h" - -// mlogic::common +#include "log.h" #include "lockguard.h" #include "uriparser.h" @@ -63,28 +62,31 @@ MqttClient::MqttClient(const std::string& _clientId, const std::functiondisconnect(); decltype(m_principalClient) principalClient{}; OSDEV_COMPONENTS_LOCKGUARD(m_internalMutex); - // DebugLogToFIle ("MqttClient", "%1 - cleanup principal client", m_clientId); + LogDebug( "MqttClient", std::string( m_clientId + " - cleanup principal client" ) ); m_principalClient.swap(principalClient); } - // DebugLogToFIle ("MqttClient", "%1 - dtor stop queue", m_clientId); + + LogDebug( "MqttClient", std::string( m_clientId + " - dtor stop queue" ) ); m_eventQueue.stop(); if (m_workerThread.joinable()) { m_workerThread.join(); } - // DebugLogToFIle ("MqttClient", "%1 - dtor ready", m_clientId); + LogDebug( "MqttClient", std::string( m_clientId + " - dtor ready" ) ); } std::string MqttClient::clientId() const @@ -135,7 +137,8 @@ void MqttClient::connect(const std::string& host, int port, const Credentials& c void MqttClient::connect(const std::string& _endpoint) { - // InfoLogToFIle ("MqttClient", "%1 - Request connect", m_clientId); + LogInfo( "MqttClient", std::string( m_clientId + " - Request connect" ) ); + OSDEV_COMPONENTS_LOCKGUARD(m_interfaceMutex); IMqttClientImpl* client(nullptr); { @@ -148,8 +151,8 @@ void MqttClient::connect(const std::string& _endpoint) } else { - // ErrorLogToFIle ("MqttClient", "%1 - Cannot connect to different endpoint. Disconnect first.", m_clientId); - // Normally a throw (Yuck!) (MqttException, "Cannot connect while already connected"); + LogError( "MqttClient", std::string( m_clientId + " - Cannot connect to different endpoint. Disconnect first." ) ); + return; } } m_endpoint = _endpoint; @@ -168,26 +171,31 @@ void MqttClient::connect(const std::string& _endpoint) void MqttClient::disconnect() { - // InfoLogToFIle ("MqttClient", "%1 - Request disconnect", m_clientId); + LogInfo( "MqttClient", std::string( m_clientId + " - Request disconnect" ) ); OSDEV_COMPONENTS_LOCKGUARD(m_interfaceMutex); decltype(m_additionalClients) additionalClients{}; std::vector clients{}; { OSDEV_COMPONENTS_LOCKGUARD(m_internalMutex); - if (!m_principalClient || m_principalClient->connectionStatus() == ConnectionStatus::Disconnected || m_principalClient->connectionStatus() == ConnectionStatus::DisconnectInProgress) { + if (!m_principalClient || m_principalClient->connectionStatus() == ConnectionStatus::Disconnected || m_principalClient->connectionStatus() == ConnectionStatus::DisconnectInProgress) + { return; } - m_additionalClients.swap(additionalClients); + m_additionalClients.swap( additionalClients ); - for (const auto& c : additionalClients) { - clients.push_back(c.get()); + for (const auto& c : additionalClients) + { + clients.push_back( c.get() ); } - clients.push_back(m_principalClient.get()); + clients.push_back( m_principalClient.get() ); } + + LogDebug( "MqttClient", std::string( m_clientId + " - Unsubscribe and disconnect clients" ) ); // DebugLogToFIle ("MqttClient", "%1 - Unsubscribe and disconnect clients", m_clientId); - for (auto& cl : clients) { + for ( auto& cl : clients ) + { cl->unsubscribeAll(); } this->waitForCompletionInternal(clients, std::chrono::milliseconds(2000), std::set{});