From 8bf44a9d1d697eed97678fb9023d462c41acea81 Mon Sep 17 00:00:00 2001 From: Peter M. Groen Date: Thu, 7 Apr 2022 19:19:06 +0200 Subject: [PATCH] Sensible return values on methods in mqttclient --- include/log.h | 224 -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- include/threadcontext.h | 79 ------------------------------------------------------------------------------- src/CMakeLists.txt | 2 -- src/log.cpp | 167 ----------------------------------------------------------------------------------------------------------------------------------------------------------------------- src/mqttclient.cpp | 23 +++++++++++------------ src/threadcontext.cpp | 30 ------------------------------ 6 files changed, 11 insertions(+), 514 deletions(-) delete mode 100644 include/log.h delete mode 100644 include/threadcontext.h delete mode 100644 src/log.cpp delete mode 100644 src/threadcontext.cpp diff --git a/include/log.h b/include/log.h deleted file mode 100644 index 6c95166..0000000 --- a/include/log.h +++ /dev/null @@ -1,224 +0,0 @@ -#pragma once - -// std -#include -#include -#include -#include - -namespace osdev { -namespace components { -namespace mqtt { - -#define LogTrace(context, text) \ - if ( osdev::components::mqtt::Log::level() <= osdev::components::mqtt::LogLevel::Trace ) { \ - osdev::components::mqtt::Log::trace(__FILE__, __LINE__, context, text); \ - } - -#define LogDebug(context, text) \ - if ( osdev::components::mqtt::Log::level() <= osdev::components::mqtt::LogLevel::Debug ) { \ - osdev::components::mqtt::Log::debug(__FILE__, __LINE__, context, text); \ - } - -#define LogInfo(context, text) \ - if ( osdev::components::mqtt::Log::level() <= osdev::components::mqtt::LogLevel::Info ) { \ - osdev::components::mqtt::Log::info(__FILE__, __LINE__, context, text); \ - } - -#define LogWarning(context, text) \ - if ( osdev::components::mqtt::Log::level() <= osdev::components::mqtt::LogLevel::Warning ) {\ - osdev::components::mqtt::Log::warning(__FILE__, __LINE__, context, text); \ - } - -#define LogError(context, text) \ - if ( osdev::components::mqtt::Log::level() <= osdev::components::mqtt::LogLevel::Error ) { \ - osdev::components::mqtt::Log::error(__FILE__, __LINE__, context, text); \ - } - -enum class LogLevel -{ - Trace, - Debug, - Info, - Warning, - Error -}; - -/*! \class Log - * \brief Basic logging mechanism. - */ -class Log -{ -public: - /** - * @brief String that identifies the start of the program in the logging. - */ - static const std::string s_startMarker; - - /** - * @brief Initialize the logging mechanism - * @param context - The main context - * @param logFile - Logfile if available - * @param logDepth - Initial log-depth - */ - static void init( const std::string &context, - const std::string &logFile, - LogLevel logDepth = LogLevel::Info); - - //! Shutdown the logging mechanism - static void terminate(); - - /** - * @brief Log a trace message in a category. - * @param file - Name of the source-file - * @param line - The line number in the source-file - * @param category - The category of the message. - * @param message - The string to print - */ - static void trace( const char *file, int line, - const std::string &category, const std::string &message ); - - /** - * @brief Log a debug message in a category. - * @param file - Name of the source-file - * @param line - The line number in the source-file - * @param category - The category of the message. - * @param message - The string to print - */ - static void debug( const char *file, int line, - const std::string &category, const std::string &message ); - - /** - * @brief Log an info message in a category. - * @param file - Name of the source-file - * @param line - The line number in the source-file - * @param category - The category of the message. - * @param message - The string to print - */ - static void info( const char *file, int line, - const std::string &category, const std::string &message ); - - /** - * @brief Log a warning message in a category. - * @param file - Name of the source-file - * @param line - The line number in the source-file - * @param category - The category of the message. - * @param message - The string to print - */ - static void warning( const char *file, int line, - const std::string &category, const std::string &message ); - - /** - * @brief Log an error message in a category. - * @param file - Name of the source-file - * @param line - The line number in the source-file - * @param category - The category of the message. - * @param message - The string to print - */ - static void error( const char *file, int line, - const std::string &category, const std::string &message ); - - /** - * @return The current log level. - */ - static LogLevel level() - { - return s_logLevel; - } - -protected: - /** - * @brief Log a debug message in a category. - * @param category The category of the message. - * @param message The string to print. - */ - static void trace(const std::string &category, const std::string &message); - - /** - * @brief Log a debug message in a category. - * @param category The category of the message. - * @param message The string to print. - */ - static void debug(const std::string &category, const std::string &message); - - /** - * @brief Log an info message in a category. - * @param category The category of the message. - * @param message The string to print. - */ - static void info(const std::string &category, const std::string &message); - - /** - * @brief Log a warning message in a category. - * @param category The category of the message. - * @param message The string to print. - */ - static void warning(const std::string &category, const std::string &message); - - /** - * @brief Log an error message in a category. - * @param category The category of the message. - * @param message The string to print. - */ - static void error(const std::string &category, const std::string &message); - - //! Create a convenient timestamp - static std::string getDateTime(); - - /** - * @brief Write the message to the logfile - * @param category The category of the message. - * @param message The string to print. - * @param level Selected log level - */ - static void writeLog(const std::string &category, - const std::string &message, - LogLevel level); - - /** - * @brief Log an error message in a category. - * @param category The category of the message. - * @param message The string to print. - * @param level Selected log level - */ - static void log(const std::string &category, - const std::string &message, - LogLevel level); - -private: - /** - * @brief Put filename, line-number and message in one string - * @param file Source-file - * @param line Line in source-file - * @param message The string to print - * @return Formatted string with file, line and message - */ - 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; - - //! The name of the LogFile. - static std::string s_fileName; - - //! The amount of logging - static LogLevel s_logLevel; - - //! Mutex to keep the logger thread-safe - static std::mutex m_mutex; -}; - -} /* End namespace mqtt */ -} /* End namespace components */ -} /* End namespace osdev */ diff --git a/include/threadcontext.h b/include/threadcontext.h deleted file mode 100644 index eea96a4..0000000 --- a/include/threadcontext.h +++ /dev/null @@ -1,79 +0,0 @@ -#pragma once - -// std -#include - -namespace osdev { -namespace components { -namespace mqtt { - -/** - * @brief Set the current thread context - * The context is restored to the previous context when this object goes out of scope. - * @note This object is meat to be used on the stack. - */ -class ThreadContextScope -{ -public: - /** - * @brief Construct a scoped object that sets the current thread context. - * @param context The context that will be used by the loggin framework. - */ - explicit ThreadContextScope( const std::string &context ); - ~ThreadContextScope(); - - // Non copyable and non movable - ThreadContextScope( const ThreadContextScope& ) = delete; - ThreadContextScope& operator=( const ThreadContextScope& ) = delete; - ThreadContextScope( ThreadContextScope&& ) = delete; - ThreadContextScope& operator=( ThreadContextScope&& ) = delete; - -private: - std::string m_previousContext; ///< Copy of the previous context. -}; - -/** - * @brief Add context to a thread. - * For every thread only one specific instance of this object will exist. - * @note Contexts can only be set by using a ThreadContextScope object. - */ -class ThreadContext -{ - // Contexts can only be set by using the ThreadContextScope object. - friend class ThreadContextScope; - -public: - static ThreadContext& instance(); - - const std::string& context() const - { - return m_context; - } - -private: - /** - * @brief Set the thread context. - */ - void setContext( const std::string &contextString ) - { - m_context = contextString; - } - - /** - * Construct a ThreadContext object. - * The context is set to "default" - */ - ThreadContext(); - - // Non copyable and non moveable - ThreadContext(const ThreadContext&) = delete; - ThreadContext& operator=(const ThreadContext&) = delete; - ThreadContext(ThreadContext&&) = delete; - ThreadContext& operator=(ThreadContext&&) = delete; - - std::string m_context; ///< The context string -}; - -} // End namespace mqtt -} // End namespace components -} // End namespace osdev diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 2d8873e..6820971 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -59,8 +59,6 @@ set(SRC_LIST ${CMAKE_CURRENT_SOURCE_DIR}/sharedreaderlock.cpp ${CMAKE_CURRENT_SOURCE_DIR}/stringutils.cpp ${CMAKE_CURRENT_SOURCE_DIR}/uriparser.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/log.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/threadcontext.cpp ) include(library) diff --git a/src/log.cpp b/src/log.cpp deleted file mode 100644 index ca78b3f..0000000 --- a/src/log.cpp +++ /dev/null @@ -1,167 +0,0 @@ -#include "log.h" -#include "threadcontext.h" - -// std -#include -#include -#include -#include -#include -#include -#include -#include - -using namespace osdev::components::mqtt; - -namespace { - -std::string toString( LogLevel level ) -{ - switch (level) - { - case LogLevel::Trace: - return "T"; - case LogLevel::Debug: - return "D"; - case LogLevel::Info: - return "I"; - case LogLevel::Warning: - return "W"; - case LogLevel::Error: - return "E"; - } - return "U"; -} - -} // namespace - -const std::string Log::s_startMarker = std::string("|________________________________________________________________________________________________________________________________|"); -std::string Log::s_context = std::string(); -std::string Log::s_fileName = std::string(); -LogLevel Log::s_logLevel = LogLevel::Info; - -void Log::init( const std::string &context, const std::string &logFile, LogLevel logDepth ) -{ - s_logLevel = logDepth; - s_context = context; - - if( !logFile.empty()) - { - s_fileName = logFile; - } -} - -void Log::terminate() -{ - s_context.clear(); - s_fileName.clear(); - s_logLevel = LogLevel::Info; -} - -void Log::log( const std::string &category, const std::string &message, LogLevel level ) -{ - std::lock_guard lock(m_mutex); - - std::string logCategory = s_context + '|' + toString( level ) + '|' + ThreadContext::instance().context() + '|' + category; - std::string logMessage = message; - if( logMessage.empty() ) - { - static const std::string emptyMessage( "--" ); - logMessage = emptyMessage; - } - else - { - // Replace % signs - ReplaceAll( logMessage, "%", "%%" ); - } - - writeLog( logCategory, logMessage, level ); -} - -std::string Log::fileinfoMessage( const char *file, int line, const std::string &message ) -{ - static const std::string templ("%1:%2| %3"); - QFileInfo fi(file); - return templ.arg( fi.fileName() ).arg(line).arg(message); -} - -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::trace(const std::string &category, const std::string &message) -{ - log(category, message, LogLevel::Trace); -} - -void Log::debug(const std::string& category, const std::string& message) -{ - log( category, message, LogLevel::Debug ); -} - -void Log::info(const std::string& category, const std::string& message) -{ - log( category, message, LogLevel::Info ); -} - -void Log::warning(const std::string& category, const std::string& message) -{ - log(category, message, LogLevel::Warning ); -} - -void Log::error(const std::string& category, const std::string& message) -{ - log(category, message, LogLevel::Error ); -} - -void Log::trace(const char *file, int line, const std::string &category, const std::string &message) -{ - log(category, fileinfoMessage(file, line, message), LogLevel::Trace ); -} - -void Log::debug(const char* file, int line, const std::string& category, const std::string& message) -{ - log( category, fileinfoMessage(file, line, message), LogLevel::Debug ); -} - -void Log::info(const char* file, int line, const std::string& category, const std::string& message) -{ - log( category, fileinfoMessage(file, line, message), LogLevel::Info ); -} - -void Log::warning(const char* file, int line, const std::string& category, const std::string& message) -{ - log( category, fileinfoMessage(file, line, message), LogLevel::Warning); -} - -void Log::error(const char* file, int line, const std::string& category, const std::string& message) -{ - log( category, fileinfoMessage(file, line, message), LogLevel::Error ); -} - -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 4b4a26d..ac7ea4f 100644 --- a/src/mqttclient.cpp +++ b/src/mqttclient.cpp @@ -26,7 +26,6 @@ #include "mqttutil.h" #include "mqttidgenerator.h" #include "mqtttypeconverter.h" -#include "log.h" #include "lockguard.h" #include "uriparser.h" @@ -64,29 +63,26 @@ MqttClient::MqttClient(const std::string& _clientId, const std::functiondisconnect(); decltype(m_principalClient) principalClient{}; OSDEV_COMPONENTS_LOCKGUARD(m_internalMutex); - LogDebug( "MqttClient", std::string( m_clientId + " - cleanup principal client" ) ); + // LogDebug( "MqttClient", std::string( m_clientId + " - cleanup principal client" ) ); m_principalClient.swap(principalClient); } - LogDebug( "MqttClient", std::string( m_clientId + " - dtor stop queue" ) ); + // LogDebug( "MqttClient", std::string( m_clientId + " - dtor stop queue" ) ); m_eventQueue.stop(); if (m_workerThread.joinable()) { m_workerThread.join(); } - LogDebug( "MqttClient", std::string( m_clientId + " - dtor ready" ) ); + // LogDebug( "MqttClient", std::string( m_clientId + " - dtor ready" ) ); } std::string MqttClient::clientId() const @@ -137,7 +133,7 @@ void MqttClient::connect(const std::string& host, int port, const Credentials& c void MqttClient::connect(const std::string& _endpoint) { - LogInfo( "MqttClient", std::string( m_clientId + " - Request connect" ) ); + // LogInfo( "MqttClient", std::string( m_clientId + " - Request connect" ) ); OSDEV_COMPONENTS_LOCKGUARD(m_interfaceMutex); IMqttClientImpl* client(nullptr); @@ -151,7 +147,7 @@ void MqttClient::connect(const std::string& _endpoint) } else { - LogError( "MqttClient", std::string( m_clientId + " - Cannot connect to different endpoint. Disconnect first." ) ); + // LogError( "MqttClient", std::string( m_clientId + " - Cannot connect to different endpoint. Disconnect first." ) ); return; } } @@ -171,7 +167,7 @@ void MqttClient::connect(const std::string& _endpoint) void MqttClient::disconnect() { - LogInfo( "MqttClient", std::string( m_clientId + " - Request disconnect" ) ); + // LogInfo( "MqttClient", std::string( m_clientId + " - Request disconnect" ) ); OSDEV_COMPONENTS_LOCKGUARD(m_interfaceMutex); decltype(m_additionalClients) additionalClients{}; @@ -192,7 +188,7 @@ void MqttClient::disconnect() } - LogDebug( "MqttClient", std::string( m_clientId + " - Unsubscribe and disconnect clients" ) ); + // LogDebug( "MqttClient", std::string( m_clientId + " - Unsubscribe and disconnect clients" ) ); // DebugLogToFIle ("MqttClient", "%1 - Unsubscribe and disconnect clients", m_clientId); for ( auto& cl : clients ) { @@ -228,6 +224,7 @@ Token MqttClient::publish(const MqttMessage& message, int qos) } // ErrorLogToFIle ("MqttClient", "%1 - Unable to publish, not connected", m_clientId); // Throw (MqttException, "Not connected"); + return Token(m_clientId, -1); } client = m_principalClient.get(); } @@ -246,6 +243,7 @@ Token MqttClient::subscribe(const std::string& topic, int qos, const std::functi { // ErrorLogToFIle ("MqttClient", "%1 - Unable to subscribe, not connected", m_clientId); // throw (?)(MqttException, "Not connected"); + return Token(m_clientId, -1); } if (!m_principalClient->isOverlapping(topic)) { client = m_principalClient.get(); @@ -287,6 +285,7 @@ std::set MqttClient::unsubscribe(const std::string& topic, int qos) if (!m_principalClient || m_principalClient->connectionStatus() == ConnectionStatus::Disconnected) { // ErrorLogToFIle ("MqttClient", "%1 - Unable to unsubscribe, not connected", m_clientId); // Throw (MqttException, "Not connected"); + return std::set(); } clients.push_back(m_principalClient.get()); for (const auto& c : m_additionalClients) { diff --git a/src/threadcontext.cpp b/src/threadcontext.cpp deleted file mode 100644 index 998bd67..0000000 --- a/src/threadcontext.cpp +++ /dev/null @@ -1,30 +0,0 @@ -#include "threadcontext.h" - -// std -#include - -using namespace osdev::components::mqtt; - -ThreadContextScope::ThreadContextScope( const std::string &_context ) - : m_previousContext( ThreadContext::instance().context() ) -{ - ThreadContext::instance().setContext( _context ); -} - -ThreadContextScope::~ThreadContextScope() -{ - ThreadContext::instance().setContext( m_previousContext ); -} - -// static -ThreadContext& ThreadContext::instance() -{ - static thread_local ThreadContext tc; - return tc; -} - -ThreadContext::ThreadContext() - : m_context( "default" ) -{ - -} -- libgit2 0.21.4