Commit 84738f568d9723e7ee720b775e7568cf7d4d01b7
1 parent
25b0d2f8
Setting up the logger
Showing
4 changed files
with
85 additions
and
23 deletions
include/log.h
| @@ -195,6 +195,16 @@ private: | @@ -195,6 +195,16 @@ private: | ||
| 195 | static std::string fileinfoMessage(const char* file, int line, | 195 | static std::string fileinfoMessage(const char* file, int line, |
| 196 | const std::string &message); | 196 | const std::string &message); |
| 197 | 197 | ||
| 198 | + /** | ||
| 199 | + * @brief Replace the characters in a std::string with other characters. | ||
| 200 | + * @param strToReplace The string we want to replace characters in | ||
| 201 | + * @param from_chars The characters we want to replace | ||
| 202 | + * @param to_chars The characters we want to replace with. | ||
| 203 | + * @return strToReplace This is the original string with the replaced characters. | ||
| 204 | + */ | ||
| 205 | + static void ReplaceAll( std::string &strToReplace, const std::string& from_chars, const std::string& to_chars ); | ||
| 206 | + | ||
| 207 | + | ||
| 198 | //! The main context. | 208 | //! The main context. |
| 199 | static std::string s_context; | 209 | static std::string s_context; |
| 200 | 210 |
include/threadcontext.h
| @@ -45,10 +45,16 @@ class ThreadContext | @@ -45,10 +45,16 @@ class ThreadContext | ||
| 45 | public: | 45 | public: |
| 46 | static ThreadContext& instance(); | 46 | static ThreadContext& instance(); |
| 47 | 47 | ||
| 48 | + const std::string& context() const | ||
| 49 | + { | ||
| 50 | + return m_context; | ||
| 51 | + } | ||
| 52 | + | ||
| 53 | +private: | ||
| 48 | /** | 54 | /** |
| 49 | - * @briefReturn the thread context. | 55 | + * @brief Set the thread context. |
| 50 | */ | 56 | */ |
| 51 | - void setContext( const std::string &contextString) | 57 | + void setContext( const std::string &contextString ) |
| 52 | { | 58 | { |
| 53 | m_context = contextString; | 59 | m_context = contextString; |
| 54 | } | 60 | } |
src/log.cpp
| 1 | #include "log.h" | 1 | #include "log.h" |
| 2 | 2 | ||
| 3 | -// std | ||
| 4 | - | 3 | +#include "threadcontext.h" |
| 5 | 4 | ||
| 5 | +// std | ||
| 6 | +#include <ios> | ||
| 7 | +#include <iomanip> | ||
| 8 | +#include <sstream> | ||
| 9 | +#include <syslog.h> | ||
| 10 | +#include <sys/types.h> | ||
| 11 | +#include <thread> | ||
| 12 | +#include <unistd.h> | ||
| 6 | 13 | ||
| 7 | using namespace osdev::components::mqtt; | 14 | using namespace osdev::components::mqtt; |
| 8 | 15 | ||
| @@ -63,8 +70,39 @@ void Log::log( const std::string &category, const std::string &message, LogLevel | @@ -63,8 +70,39 @@ void Log::log( const std::string &category, const std::string &message, LogLevel | ||
| 63 | else | 70 | else |
| 64 | { | 71 | { |
| 65 | // Replace % signs | 72 | // Replace % signs |
| 66 | - logMessage.replace( '%', "%%"); | 73 | + ReplaceAll( logMessage, "%", "%%" ); |
| 67 | } | 74 | } |
| 68 | 75 | ||
| 69 | writeLog( logCategory, logMessage, level ); | 76 | writeLog( logCategory, logMessage, level ); |
| 70 | } | 77 | } |
| 78 | + | ||
| 79 | +void Log::writeLog(const std::string &category, const std::string &message, LogLevel level) | ||
| 80 | +{ | ||
| 81 | + if( s_fileName.empty() ) | ||
| 82 | + { | ||
| 83 | + (void)level; | ||
| 84 | + // To be implemented. | ||
| 85 | + return; | ||
| 86 | + } | ||
| 87 | + else | ||
| 88 | + { | ||
| 89 | + setlogmask( LOG_UPTO( LOG_NOTICE ) ); | ||
| 90 | + openlog( category.c_str(), LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1 ); | ||
| 91 | + | ||
| 92 | + syslog( LOG_NOTICE, "%s", message.c_str() ); | ||
| 93 | + | ||
| 94 | + closelog(); | ||
| 95 | + } | ||
| 96 | +} | ||
| 97 | + | ||
| 98 | +void Log::ReplaceAll( std::string &strToReplace, | ||
| 99 | + const std::string& from_chars, | ||
| 100 | + const std::string& to_chars ) | ||
| 101 | +{ | ||
| 102 | + size_t start_pos = 0; | ||
| 103 | + while( ( start_pos = strToReplace.find( from_chars, start_pos ) ) != std::string::npos ) | ||
| 104 | + { | ||
| 105 | + strToReplace.replace( start_pos, from_chars.length(), to_chars ); | ||
| 106 | + start_pos += to_chars.length(); // Handles case where 'to' is a substring of 'from' | ||
| 107 | + } | ||
| 108 | +} |
src/mqttclient.cpp
| @@ -26,8 +26,7 @@ | @@ -26,8 +26,7 @@ | ||
| 26 | #include "mqttutil.h" | 26 | #include "mqttutil.h" |
| 27 | #include "mqttidgenerator.h" | 27 | #include "mqttidgenerator.h" |
| 28 | #include "mqtttypeconverter.h" | 28 | #include "mqtttypeconverter.h" |
| 29 | - | ||
| 30 | -// mlogic::common | 29 | +#include "log.h" |
| 31 | #include "lockguard.h" | 30 | #include "lockguard.h" |
| 32 | #include "uriparser.h" | 31 | #include "uriparser.h" |
| 33 | 32 | ||
| @@ -63,28 +62,31 @@ MqttClient::MqttClient(const std::string& _clientId, const std::function<void(co | @@ -63,28 +62,31 @@ MqttClient::MqttClient(const std::string& _clientId, const std::function<void(co | ||
| 63 | , m_principalClient() | 62 | , m_principalClient() |
| 64 | , m_additionalClients() | 63 | , m_additionalClients() |
| 65 | , m_eventQueue(_clientId) | 64 | , m_eventQueue(_clientId) |
| 66 | - , m_workerThread(std::thread(&MqttClient::eventHandler, this)) | 65 | + , m_workerThread( std::thread( &MqttClient::eventHandler, this ) ) |
| 67 | { | 66 | { |
| 67 | + // Initialize the logger. | ||
| 68 | + Log::init( "mqtt-cpp", "", LogLevel::Info ); | ||
| 68 | } | 69 | } |
| 69 | 70 | ||
| 70 | MqttClient::~MqttClient() | 71 | MqttClient::~MqttClient() |
| 71 | { | 72 | { |
| 72 | - // DebugLogToFIle ("MqttClient", "%1 - dtor", m_clientId); | 73 | + LogDebug( "MqttClient", std::string( m_clientId + " - disconnect" ) ); |
| 73 | { | 74 | { |
| 74 | - // DebugLogToFIle ("MqttClient", "%1 - disconnect", m_clientId); | 75 | + LogDebug( "MqttClient", std::string( m_clientId + " - disconnect" ) ); |
| 75 | this->disconnect(); | 76 | this->disconnect(); |
| 76 | decltype(m_principalClient) principalClient{}; | 77 | decltype(m_principalClient) principalClient{}; |
| 77 | 78 | ||
| 78 | OSDEV_COMPONENTS_LOCKGUARD(m_internalMutex); | 79 | OSDEV_COMPONENTS_LOCKGUARD(m_internalMutex); |
| 79 | - // DebugLogToFIle ("MqttClient", "%1 - cleanup principal client", m_clientId); | 80 | + LogDebug( "MqttClient", std::string( m_clientId + " - cleanup principal client" ) ); |
| 80 | m_principalClient.swap(principalClient); | 81 | m_principalClient.swap(principalClient); |
| 81 | } | 82 | } |
| 82 | - // DebugLogToFIle ("MqttClient", "%1 - dtor stop queue", m_clientId); | 83 | + |
| 84 | + LogDebug( "MqttClient", std::string( m_clientId + " - dtor stop queue" ) ); | ||
| 83 | m_eventQueue.stop(); | 85 | m_eventQueue.stop(); |
| 84 | if (m_workerThread.joinable()) { | 86 | if (m_workerThread.joinable()) { |
| 85 | m_workerThread.join(); | 87 | m_workerThread.join(); |
| 86 | } | 88 | } |
| 87 | - // DebugLogToFIle ("MqttClient", "%1 - dtor ready", m_clientId); | 89 | + LogDebug( "MqttClient", std::string( m_clientId + " - dtor ready" ) ); |
| 88 | } | 90 | } |
| 89 | 91 | ||
| 90 | std::string MqttClient::clientId() const | 92 | std::string MqttClient::clientId() const |
| @@ -135,7 +137,8 @@ void MqttClient::connect(const std::string& host, int port, const Credentials& c | @@ -135,7 +137,8 @@ void MqttClient::connect(const std::string& host, int port, const Credentials& c | ||
| 135 | 137 | ||
| 136 | void MqttClient::connect(const std::string& _endpoint) | 138 | void MqttClient::connect(const std::string& _endpoint) |
| 137 | { | 139 | { |
| 138 | - // InfoLogToFIle ("MqttClient", "%1 - Request connect", m_clientId); | 140 | + LogInfo( "MqttClient", std::string( m_clientId + " - Request connect" ) ); |
| 141 | + | ||
| 139 | OSDEV_COMPONENTS_LOCKGUARD(m_interfaceMutex); | 142 | OSDEV_COMPONENTS_LOCKGUARD(m_interfaceMutex); |
| 140 | IMqttClientImpl* client(nullptr); | 143 | IMqttClientImpl* client(nullptr); |
| 141 | { | 144 | { |
| @@ -148,8 +151,8 @@ void MqttClient::connect(const std::string& _endpoint) | @@ -148,8 +151,8 @@ void MqttClient::connect(const std::string& _endpoint) | ||
| 148 | } | 151 | } |
| 149 | else | 152 | else |
| 150 | { | 153 | { |
| 151 | - // ErrorLogToFIle ("MqttClient", "%1 - Cannot connect to different endpoint. Disconnect first.", m_clientId); | ||
| 152 | - // Normally a throw (Yuck!) (MqttException, "Cannot connect while already connected"); | 154 | + LogError( "MqttClient", std::string( m_clientId + " - Cannot connect to different endpoint. Disconnect first." ) ); |
| 155 | + return; | ||
| 153 | } | 156 | } |
| 154 | } | 157 | } |
| 155 | m_endpoint = _endpoint; | 158 | m_endpoint = _endpoint; |
| @@ -168,26 +171,31 @@ void MqttClient::connect(const std::string& _endpoint) | @@ -168,26 +171,31 @@ void MqttClient::connect(const std::string& _endpoint) | ||
| 168 | 171 | ||
| 169 | void MqttClient::disconnect() | 172 | void MqttClient::disconnect() |
| 170 | { | 173 | { |
| 171 | - // InfoLogToFIle ("MqttClient", "%1 - Request disconnect", m_clientId); | 174 | + LogInfo( "MqttClient", std::string( m_clientId + " - Request disconnect" ) ); |
| 172 | OSDEV_COMPONENTS_LOCKGUARD(m_interfaceMutex); | 175 | OSDEV_COMPONENTS_LOCKGUARD(m_interfaceMutex); |
| 173 | 176 | ||
| 174 | decltype(m_additionalClients) additionalClients{}; | 177 | decltype(m_additionalClients) additionalClients{}; |
| 175 | std::vector<IMqttClientImpl*> clients{}; | 178 | std::vector<IMqttClientImpl*> clients{}; |
| 176 | { | 179 | { |
| 177 | OSDEV_COMPONENTS_LOCKGUARD(m_internalMutex); | 180 | OSDEV_COMPONENTS_LOCKGUARD(m_internalMutex); |
| 178 | - if (!m_principalClient || m_principalClient->connectionStatus() == ConnectionStatus::Disconnected || m_principalClient->connectionStatus() == ConnectionStatus::DisconnectInProgress) { | 181 | + if (!m_principalClient || m_principalClient->connectionStatus() == ConnectionStatus::Disconnected || m_principalClient->connectionStatus() == ConnectionStatus::DisconnectInProgress) |
| 182 | + { | ||
| 179 | return; | 183 | return; |
| 180 | } | 184 | } |
| 181 | - m_additionalClients.swap(additionalClients); | 185 | + m_additionalClients.swap( additionalClients ); |
| 182 | 186 | ||
| 183 | - for (const auto& c : additionalClients) { | ||
| 184 | - clients.push_back(c.get()); | 187 | + for (const auto& c : additionalClients) |
| 188 | + { | ||
| 189 | + clients.push_back( c.get() ); | ||
| 185 | } | 190 | } |
| 186 | - clients.push_back(m_principalClient.get()); | 191 | + clients.push_back( m_principalClient.get() ); |
| 187 | } | 192 | } |
| 188 | 193 | ||
| 194 | + | ||
| 195 | + LogDebug( "MqttClient", std::string( m_clientId + " - Unsubscribe and disconnect clients" ) ); | ||
| 189 | // DebugLogToFIle ("MqttClient", "%1 - Unsubscribe and disconnect clients", m_clientId); | 196 | // DebugLogToFIle ("MqttClient", "%1 - Unsubscribe and disconnect clients", m_clientId); |
| 190 | - for (auto& cl : clients) { | 197 | + for ( auto& cl : clients ) |
| 198 | + { | ||
| 191 | cl->unsubscribeAll(); | 199 | cl->unsubscribeAll(); |
| 192 | } | 200 | } |
| 193 | this->waitForCompletionInternal(clients, std::chrono::milliseconds(2000), std::set<Token>{}); | 201 | this->waitForCompletionInternal(clients, std::chrono::milliseconds(2000), std::set<Token>{}); |