From 7771e50f430be9fcaa5bf334c7bb6dabc348502b Mon Sep 17 00:00:00 2001 From: Steven Date: Mon, 18 Jul 2022 11:36:34 +0200 Subject: [PATCH] moved log.h to include and added log setters to mqttclient (default up to info ) --- examples/pub/publisher.cpp | 3 +++ include/log.h | 324 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ include/mqttclient.h | 25 ++++++++++++++++++++++--- src/CMakeLists.txt | 1 - src/log.cpp | 4 ++-- src/log.h | 324 ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ src/mqttclient.cpp | 18 +++++++++++++++--- 7 files changed, 366 insertions(+), 333 deletions(-) create mode 100644 include/log.h delete mode 100644 src/log.h diff --git a/examples/pub/publisher.cpp b/examples/pub/publisher.cpp index 90a6068..f968a79 100644 --- a/examples/pub/publisher.cpp +++ b/examples/pub/publisher.cpp @@ -31,6 +31,9 @@ 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.setLogLevel( osdev::components::log::LogLevel::Info ); + m_mqtt_client.setMask( osdev::components::log::LogMask::Upto ); + m_mqtt_client.connect( hostname, portnumber, osdev::components::mqtt::Credentials( username, password ), osdev::components::mqtt::mqtt_LWT( lwt_topic, lwt_message ) ); std::cout << "Client state : " << m_mqtt_client.state() << std::endl; } diff --git a/include/log.h b/include/log.h new file mode 100644 index 0000000..31e84a5 --- /dev/null +++ b/include/log.h @@ -0,0 +1,324 @@ +/* **************************************************************************** + * Copyright 2019 Open Systems Development BV * + * * + * Permission is hereby granted, free of charge, to any person obtaining a * + * copy of this software and associated documentation files (the "Software"), * + * to deal in the Software without restriction, including without limitation * + * the rights to use, copy, modify, merge, publish, distribute, sublicense, * + * and/or sell copies of the Software, and to permit persons to whom the * + * Software is furnished to do so, subject to the following conditions: * + * * + * The above copyright notice and this permission notice shall be included in * + * all copies or substantial portions of the Software. * + * * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * + * DEALINGS IN THE SOFTWARE. * + * ***************************************************************************/ +#pragma once + +#include +#include + +namespace osdev { +namespace components { +namespace log { + +#define LogEmergency(context, text) \ + if (osdev::components::log::Log::level() >= osdev::components::log::LogLevel::Emergency)\ + { \ + osdev::components::log::Log::emergency(__FILE__, __LINE__, context, text); \ + } + +#define LogAlert(context, text) \ + if (osdev::components::log::Log::level() >= osdev::components::log::LogLevel::Alert) \ + { \ + osdev::components::log::Log::alert(__FILE__, __LINE__, context, text); \ + } + +#define LogCritical(context, text) \ + if (osdev::components::log::Log::level() >= osdev::components::log::LogLevel::Critical) \ + { \ + osdev::components::log::Log::critical(__FILE__, __LINE__, context, text); \ + } + +#define LogError(context, text) \ + if (osdev::components::log::Log::level() >= osdev::components::log::LogLevel::Error) \ + { \ + osdev::components::log::Log::error(__FILE__, __LINE__, context, text); \ + } + +#define LogWarning(context, text) \ + if (osdev::components::log::Log::level() >= osdev::components::log::LogLevel::Warning) \ + { \ + osdev::components::log::Log::warning(__FILE__, __LINE__, context, text); \ + } + +#define LogNotice(context, text) \ + if (osdev::components::log::Log::level() >= osdev::components::log::LogLevel::Notice) \ + { \ + osdev::components::log::Log::notice(__FILE__, __LINE__, context, text); \ + } + +#define LogInfo(context, text) \ + if (osdev::components::log::Log::level() >= osdev::components::log::LogLevel::Info) \ + { \ + osdev::components::log::Log::info(__FILE__, __LINE__, context, text); \ + } + +#define LogDebug(context, text) \ + if (osdev::components::log::Log::level() >= osdev::components::log::LogLevel::Debug) \ + { \ + osdev::components::log::Log::debug(__FILE__, __LINE__, context, text); \ + } + +/*! + * \brief The LogLevel enum + * Enumeration class dealing with LogLevels. + * Used to convert to syslog macro's. + */ +enum class LogLevel +{ + Emergency = 0, + Alert, + Critical, + Error, + Warning, + Notice, + Info, + Debug +}; + +/*! + * \brief The LogMask enum + * Enumeration class dealing with LogMask. + * Used to convert to syslog macro's. + */ +enum class LogMask +{ + Mask = 0, + Upto, + None +}; + +/*! \class Log + \brief Basic logging mechanism. +*/ +class Log +{ +public: + /** + * @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 = std::string(), + LogLevel logDepth = LogLevel::Info ); + + //! Shutdown the logging mechanism + static void terminate(); + + /*! + * \brief Write to syslog + * \param priority - priority level [from debug up to emergency] + * \param message - The string to print + * \param context - The context name. default name is the name of the executable. + */ + static void write( const int &priority, const std::string &message, const std::string &context = std::string() ); + + /** + * @brief Log an emergency 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 emergency( const char* file, int line, const std::string& category, const std::string& message ); + /** + * @brief Log an alert 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 alert ( const char* file, int line, const std::string& category, const std::string& message ); + /** + * @brief Log a critical 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 critical ( 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 ); + /** + * @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 a notice 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 notice ( 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 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 ); + + /** + * @return The current log level. + */ + static LogLevel level() + { + if( s_logMask == LogMask::None ) + { + return LogLevel::Debug; + } + return s_logLevel; + } + + /*! + * \brief setMask update the current logMask + * \param logMask - Enum defining the logmask used. + */ + static void setMask ( LogMask logMask ) { s_logMask = logMask; } + /*! + * \brief setLogLevel update the current logLevel + * \param logLevel - Enum defining the logLevel used, in combination with Mask. + */ + static void setLogLevel( LogLevel logLevel ) { s_logLevel = logLevel; } + /*! + * \brief setContext update the current context + * \param context - String containing the new context name. + */ + static void setContext ( std::string context ) { s_context = context; } + +protected: + /** + * @brief Log an emergency message in a category. + * @param category The category of the message. + * @param message The string to print. + */ + static void emergency( const std::string& category, const std::string& message ); + /** + * @brief Log an alert message in a category. + * @param category The category of the message. + * @param message The string to print. + */ + static void alert ( const std::string& category, const std::string& message ); + /** + * @brief Log a critical message in a category. + * @param category The category of the message. + * @param message The string to print. + */ + static void critical ( 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 ); + /** + * @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 a notice message in a category. + * @param category The category of the message. + * @param message The string to print. + */ + static void notice ( 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 an 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 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 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 ); + + /** + * @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); + + //! 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; + + //! The mask + static LogMask s_logMask; +}; + +} // End namespace log +} // End namespace components +} // End namespace osdev + diff --git a/include/mqttclient.h b/include/mqttclient.h index 7eaa795..7e10d8d 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 { @@ -166,6 +169,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/log.h b/src/log.h deleted file mode 100644 index 31e84a5..0000000 --- a/src/log.h +++ /dev/null @@ -1,324 +0,0 @@ -/* **************************************************************************** - * Copyright 2019 Open Systems Development BV * - * * - * Permission is hereby granted, free of charge, to any person obtaining a * - * copy of this software and associated documentation files (the "Software"), * - * to deal in the Software without restriction, including without limitation * - * the rights to use, copy, modify, merge, publish, distribute, sublicense, * - * and/or sell copies of the Software, and to permit persons to whom the * - * Software is furnished to do so, subject to the following conditions: * - * * - * The above copyright notice and this permission notice shall be included in * - * all copies or substantial portions of the Software. * - * * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * - * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING * - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * - * DEALINGS IN THE SOFTWARE. * - * ***************************************************************************/ -#pragma once - -#include -#include - -namespace osdev { -namespace components { -namespace log { - -#define LogEmergency(context, text) \ - if (osdev::components::log::Log::level() >= osdev::components::log::LogLevel::Emergency)\ - { \ - osdev::components::log::Log::emergency(__FILE__, __LINE__, context, text); \ - } - -#define LogAlert(context, text) \ - if (osdev::components::log::Log::level() >= osdev::components::log::LogLevel::Alert) \ - { \ - osdev::components::log::Log::alert(__FILE__, __LINE__, context, text); \ - } - -#define LogCritical(context, text) \ - if (osdev::components::log::Log::level() >= osdev::components::log::LogLevel::Critical) \ - { \ - osdev::components::log::Log::critical(__FILE__, __LINE__, context, text); \ - } - -#define LogError(context, text) \ - if (osdev::components::log::Log::level() >= osdev::components::log::LogLevel::Error) \ - { \ - osdev::components::log::Log::error(__FILE__, __LINE__, context, text); \ - } - -#define LogWarning(context, text) \ - if (osdev::components::log::Log::level() >= osdev::components::log::LogLevel::Warning) \ - { \ - osdev::components::log::Log::warning(__FILE__, __LINE__, context, text); \ - } - -#define LogNotice(context, text) \ - if (osdev::components::log::Log::level() >= osdev::components::log::LogLevel::Notice) \ - { \ - osdev::components::log::Log::notice(__FILE__, __LINE__, context, text); \ - } - -#define LogInfo(context, text) \ - if (osdev::components::log::Log::level() >= osdev::components::log::LogLevel::Info) \ - { \ - osdev::components::log::Log::info(__FILE__, __LINE__, context, text); \ - } - -#define LogDebug(context, text) \ - if (osdev::components::log::Log::level() >= osdev::components::log::LogLevel::Debug) \ - { \ - osdev::components::log::Log::debug(__FILE__, __LINE__, context, text); \ - } - -/*! - * \brief The LogLevel enum - * Enumeration class dealing with LogLevels. - * Used to convert to syslog macro's. - */ -enum class LogLevel -{ - Emergency = 0, - Alert, - Critical, - Error, - Warning, - Notice, - Info, - Debug -}; - -/*! - * \brief The LogMask enum - * Enumeration class dealing with LogMask. - * Used to convert to syslog macro's. - */ -enum class LogMask -{ - Mask = 0, - Upto, - None -}; - -/*! \class Log - \brief Basic logging mechanism. -*/ -class Log -{ -public: - /** - * @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 = std::string(), - LogLevel logDepth = LogLevel::Info ); - - //! Shutdown the logging mechanism - static void terminate(); - - /*! - * \brief Write to syslog - * \param priority - priority level [from debug up to emergency] - * \param message - The string to print - * \param context - The context name. default name is the name of the executable. - */ - static void write( const int &priority, const std::string &message, const std::string &context = std::string() ); - - /** - * @brief Log an emergency 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 emergency( const char* file, int line, const std::string& category, const std::string& message ); - /** - * @brief Log an alert 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 alert ( const char* file, int line, const std::string& category, const std::string& message ); - /** - * @brief Log a critical 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 critical ( 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 ); - /** - * @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 a notice 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 notice ( 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 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 ); - - /** - * @return The current log level. - */ - static LogLevel level() - { - if( s_logMask == LogMask::None ) - { - return LogLevel::Debug; - } - return s_logLevel; - } - - /*! - * \brief setMask update the current logMask - * \param logMask - Enum defining the logmask used. - */ - static void setMask ( LogMask logMask ) { s_logMask = logMask; } - /*! - * \brief setLogLevel update the current logLevel - * \param logLevel - Enum defining the logLevel used, in combination with Mask. - */ - static void setLogLevel( LogLevel logLevel ) { s_logLevel = logLevel; } - /*! - * \brief setContext update the current context - * \param context - String containing the new context name. - */ - static void setContext ( std::string context ) { s_context = context; } - -protected: - /** - * @brief Log an emergency message in a category. - * @param category The category of the message. - * @param message The string to print. - */ - static void emergency( const std::string& category, const std::string& message ); - /** - * @brief Log an alert message in a category. - * @param category The category of the message. - * @param message The string to print. - */ - static void alert ( const std::string& category, const std::string& message ); - /** - * @brief Log a critical message in a category. - * @param category The category of the message. - * @param message The string to print. - */ - static void critical ( 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 ); - /** - * @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 a notice message in a category. - * @param category The category of the message. - * @param message The string to print. - */ - static void notice ( 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 an 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 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 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 ); - - /** - * @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); - - //! 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; - - //! The mask - static LogMask s_logMask; -}; - -} // End namespace log -} // End namespace components -} // End namespace osdev - diff --git a/src/mqttclient.cpp b/src/mqttclient.cpp index e1454f4..48ea2a4 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" @@ -613,3 +610,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 ); +} -- libgit2 0.21.4