Commit 8bf44a9d1d697eed97678fb9023d462c41acea81

Authored by Peter M. Groen
1 parent 106664ec

Sensible return values on methods in mqttclient

include/log.h deleted
1   -#pragma once
2   -
3   -// std
4   -#include <mutex>
5   -#include <syslog.h>
6   -#include <string>
7   -#include <unistd.h>
8   -
9   -namespace osdev {
10   -namespace components {
11   -namespace mqtt {
12   -
13   -#define LogTrace(context, text) \
14   - if ( osdev::components::mqtt::Log::level() <= osdev::components::mqtt::LogLevel::Trace ) { \
15   - osdev::components::mqtt::Log::trace(__FILE__, __LINE__, context, text); \
16   - }
17   -
18   -#define LogDebug(context, text) \
19   - if ( osdev::components::mqtt::Log::level() <= osdev::components::mqtt::LogLevel::Debug ) { \
20   - osdev::components::mqtt::Log::debug(__FILE__, __LINE__, context, text); \
21   - }
22   -
23   -#define LogInfo(context, text) \
24   - if ( osdev::components::mqtt::Log::level() <= osdev::components::mqtt::LogLevel::Info ) { \
25   - osdev::components::mqtt::Log::info(__FILE__, __LINE__, context, text); \
26   - }
27   -
28   -#define LogWarning(context, text) \
29   - if ( osdev::components::mqtt::Log::level() <= osdev::components::mqtt::LogLevel::Warning ) {\
30   - osdev::components::mqtt::Log::warning(__FILE__, __LINE__, context, text); \
31   - }
32   -
33   -#define LogError(context, text) \
34   - if ( osdev::components::mqtt::Log::level() <= osdev::components::mqtt::LogLevel::Error ) { \
35   - osdev::components::mqtt::Log::error(__FILE__, __LINE__, context, text); \
36   - }
37   -
38   -enum class LogLevel
39   -{
40   - Trace,
41   - Debug,
42   - Info,
43   - Warning,
44   - Error
45   -};
46   -
47   -/*! \class Log
48   - * \brief Basic logging mechanism.
49   - */
50   -class Log
51   -{
52   -public:
53   - /**
54   - * @brief String that identifies the start of the program in the logging.
55   - */
56   - static const std::string s_startMarker;
57   -
58   - /**
59   - * @brief Initialize the logging mechanism
60   - * @param context - The main context
61   - * @param logFile - Logfile if available
62   - * @param logDepth - Initial log-depth
63   - */
64   - static void init( const std::string &context,
65   - const std::string &logFile,
66   - LogLevel logDepth = LogLevel::Info);
67   -
68   - //! Shutdown the logging mechanism
69   - static void terminate();
70   -
71   - /**
72   - * @brief Log a trace message in a category.
73   - * @param file - Name of the source-file
74   - * @param line - The line number in the source-file
75   - * @param category - The category of the message.
76   - * @param message - The string to print
77   - */
78   - static void trace( const char *file, int line,
79   - const std::string &category, const std::string &message );
80   -
81   - /**
82   - * @brief Log a debug message in a category.
83   - * @param file - Name of the source-file
84   - * @param line - The line number in the source-file
85   - * @param category - The category of the message.
86   - * @param message - The string to print
87   - */
88   - static void debug( const char *file, int line,
89   - const std::string &category, const std::string &message );
90   -
91   - /**
92   - * @brief Log an info message in a category.
93   - * @param file - Name of the source-file
94   - * @param line - The line number in the source-file
95   - * @param category - The category of the message.
96   - * @param message - The string to print
97   - */
98   - static void info( const char *file, int line,
99   - const std::string &category, const std::string &message );
100   -
101   - /**
102   - * @brief Log a warning message in a category.
103   - * @param file - Name of the source-file
104   - * @param line - The line number in the source-file
105   - * @param category - The category of the message.
106   - * @param message - The string to print
107   - */
108   - static void warning( const char *file, int line,
109   - const std::string &category, const std::string &message );
110   -
111   - /**
112   - * @brief Log an error message in a category.
113   - * @param file - Name of the source-file
114   - * @param line - The line number in the source-file
115   - * @param category - The category of the message.
116   - * @param message - The string to print
117   - */
118   - static void error( const char *file, int line,
119   - const std::string &category, const std::string &message );
120   -
121   - /**
122   - * @return The current log level.
123   - */
124   - static LogLevel level()
125   - {
126   - return s_logLevel;
127   - }
128   -
129   -protected:
130   - /**
131   - * @brief Log a debug message in a category.
132   - * @param category The category of the message.
133   - * @param message The string to print.
134   - */
135   - static void trace(const std::string &category, const std::string &message);
136   -
137   - /**
138   - * @brief Log a debug message in a category.
139   - * @param category The category of the message.
140   - * @param message The string to print.
141   - */
142   - static void debug(const std::string &category, const std::string &message);
143   -
144   - /**
145   - * @brief Log an info message in a category.
146   - * @param category The category of the message.
147   - * @param message The string to print.
148   - */
149   - static void info(const std::string &category, const std::string &message);
150   -
151   - /**
152   - * @brief Log a warning message in a category.
153   - * @param category The category of the message.
154   - * @param message The string to print.
155   - */
156   - static void warning(const std::string &category, const std::string &message);
157   -
158   - /**
159   - * @brief Log an error message in a category.
160   - * @param category The category of the message.
161   - * @param message The string to print.
162   - */
163   - static void error(const std::string &category, const std::string &message);
164   -
165   - //! Create a convenient timestamp
166   - static std::string getDateTime();
167   -
168   - /**
169   - * @brief Write the message to the logfile
170   - * @param category The category of the message.
171   - * @param message The string to print.
172   - * @param level Selected log level
173   - */
174   - static void writeLog(const std::string &category,
175   - const std::string &message,
176   - LogLevel level);
177   -
178   - /**
179   - * @brief Log an error message in a category.
180   - * @param category The category of the message.
181   - * @param message The string to print.
182   - * @param level Selected log level
183   - */
184   - static void log(const std::string &category,
185   - const std::string &message,
186   - LogLevel level);
187   -
188   -private:
189   - /**
190   - * @brief Put filename, line-number and message in one string
191   - * @param file Source-file
192   - * @param line Line in source-file
193   - * @param message The string to print
194   - * @return Formatted string with file, line and message
195   - */
196   - static std::string fileinfoMessage(const char* file, int line,
197   - const std::string &message);
198   -
199   - /**
200   - * @brief Replace the characters in a std::string with other characters.
201   - * @param strToReplace The string we want to replace characters in
202   - * @param from_chars The characters we want to replace
203   - * @param to_chars The characters we want to replace with.
204   - * @return strToReplace This is the original string with the replaced characters.
205   - */
206   - static void ReplaceAll( std::string &strToReplace, const std::string& from_chars, const std::string& to_chars );
207   -
208   -
209   - //! The main context.
210   - static std::string s_context;
211   -
212   - //! The name of the LogFile.
213   - static std::string s_fileName;
214   -
215   - //! The amount of logging
216   - static LogLevel s_logLevel;
217   -
218   - //! Mutex to keep the logger thread-safe
219   - static std::mutex m_mutex;
220   -};
221   -
222   -} /* End namespace mqtt */
223   -} /* End namespace components */
224   -} /* End namespace osdev */
include/threadcontext.h deleted
1   -#pragma once
2   -
3   -// std
4   -#include <string>
5   -
6   -namespace osdev {
7   -namespace components {
8   -namespace mqtt {
9   -
10   -/**
11   - * @brief Set the current thread context
12   - * The context is restored to the previous context when this object goes out of scope.
13   - * @note This object is meat to be used on the stack.
14   - */
15   -class ThreadContextScope
16   -{
17   -public:
18   - /**
19   - * @brief Construct a scoped object that sets the current thread context.
20   - * @param context The context that will be used by the loggin framework.
21   - */
22   - explicit ThreadContextScope( const std::string &context );
23   - ~ThreadContextScope();
24   -
25   - // Non copyable and non movable
26   - ThreadContextScope( const ThreadContextScope& ) = delete;
27   - ThreadContextScope& operator=( const ThreadContextScope& ) = delete;
28   - ThreadContextScope( ThreadContextScope&& ) = delete;
29   - ThreadContextScope& operator=( ThreadContextScope&& ) = delete;
30   -
31   -private:
32   - std::string m_previousContext; ///< Copy of the previous context.
33   -};
34   -
35   -/**
36   - * @brief Add context to a thread.
37   - * For every thread only one specific instance of this object will exist.
38   - * @note Contexts can only be set by using a ThreadContextScope object.
39   - */
40   -class ThreadContext
41   -{
42   - // Contexts can only be set by using the ThreadContextScope object.
43   - friend class ThreadContextScope;
44   -
45   -public:
46   - static ThreadContext& instance();
47   -
48   - const std::string& context() const
49   - {
50   - return m_context;
51   - }
52   -
53   -private:
54   - /**
55   - * @brief Set the thread context.
56   - */
57   - void setContext( const std::string &contextString )
58   - {
59   - m_context = contextString;
60   - }
61   -
62   - /**
63   - * Construct a ThreadContext object.
64   - * The context is set to "default"
65   - */
66   - ThreadContext();
67   -
68   - // Non copyable and non moveable
69   - ThreadContext(const ThreadContext&) = delete;
70   - ThreadContext& operator=(const ThreadContext&) = delete;
71   - ThreadContext(ThreadContext&&) = delete;
72   - ThreadContext& operator=(ThreadContext&&) = delete;
73   -
74   - std::string m_context; ///< The context string
75   -};
76   -
77   -} // End namespace mqtt
78   -} // End namespace components
79   -} // End namespace osdev
src/CMakeLists.txt
... ... @@ -59,8 +59,6 @@ set(SRC_LIST
59 59 ${CMAKE_CURRENT_SOURCE_DIR}/sharedreaderlock.cpp
60 60 ${CMAKE_CURRENT_SOURCE_DIR}/stringutils.cpp
61 61 ${CMAKE_CURRENT_SOURCE_DIR}/uriparser.cpp
62   - ${CMAKE_CURRENT_SOURCE_DIR}/log.cpp
63   - ${CMAKE_CURRENT_SOURCE_DIR}/threadcontext.cpp
64 62 )
65 63  
66 64 include(library)
... ...
src/log.cpp deleted
1   -#include "log.h"
2   -#include "threadcontext.h"
3   -
4   -// std
5   -#include <ios>
6   -#include <iomanip>
7   -#include <lockguard.h>
8   -#include <sstream>
9   -#include <syslog.h>
10   -#include <sys/types.h>
11   -#include <thread>
12   -#include <unistd.h>
13   -
14   -using namespace osdev::components::mqtt;
15   -
16   -namespace {
17   -
18   -std::string toString( LogLevel level )
19   -{
20   - switch (level)
21   - {
22   - case LogLevel::Trace:
23   - return "T";
24   - case LogLevel::Debug:
25   - return "D";
26   - case LogLevel::Info:
27   - return "I";
28   - case LogLevel::Warning:
29   - return "W";
30   - case LogLevel::Error:
31   - return "E";
32   - }
33   - return "U";
34   -}
35   -
36   -} // namespace
37   -
38   -const std::string Log::s_startMarker = std::string("|________________________________________________________________________________________________________________________________|");
39   -std::string Log::s_context = std::string();
40   -std::string Log::s_fileName = std::string();
41   -LogLevel Log::s_logLevel = LogLevel::Info;
42   -
43   -void Log::init( const std::string &context, const std::string &logFile, LogLevel logDepth )
44   -{
45   - s_logLevel = logDepth;
46   - s_context = context;
47   -
48   - if( !logFile.empty())
49   - {
50   - s_fileName = logFile;
51   - }
52   -}
53   -
54   -void Log::terminate()
55   -{
56   - s_context.clear();
57   - s_fileName.clear();
58   - s_logLevel = LogLevel::Info;
59   -}
60   -
61   -void Log::log( const std::string &category, const std::string &message, LogLevel level )
62   -{
63   - std::lock_guard<std::mutex> lock(m_mutex);
64   -
65   - std::string logCategory = s_context + '|' + toString( level ) + '|' + ThreadContext::instance().context() + '|' + category;
66   - std::string logMessage = message;
67   - if( logMessage.empty() )
68   - {
69   - static const std::string emptyMessage( "--" );
70   - logMessage = emptyMessage;
71   - }
72   - else
73   - {
74   - // Replace % signs
75   - ReplaceAll( logMessage, "%", "%%" );
76   - }
77   -
78   - writeLog( logCategory, logMessage, level );
79   -}
80   -
81   -std::string Log::fileinfoMessage( const char *file, int line, const std::string &message )
82   -{
83   - static const std::string templ("%1:%2| %3");
84   - QFileInfo fi(file);
85   - return templ.arg( fi.fileName() ).arg(line).arg(message);
86   -}
87   -
88   -void Log::writeLog(const std::string &category, const std::string &message, LogLevel level)
89   -{
90   - if( s_fileName.empty() )
91   - {
92   - (void)level;
93   - // To be implemented.
94   - return;
95   - }
96   - else
97   - {
98   - setlogmask( LOG_UPTO( LOG_NOTICE ) );
99   - openlog( category.c_str(), LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1 );
100   -
101   - syslog( LOG_NOTICE, "%s", message.c_str() );
102   -
103   - closelog();
104   - }
105   -}
106   -
107   -void Log::trace(const std::string &category, const std::string &message)
108   -{
109   - log(category, message, LogLevel::Trace);
110   -}
111   -
112   -void Log::debug(const std::string& category, const std::string& message)
113   -{
114   - log( category, message, LogLevel::Debug );
115   -}
116   -
117   -void Log::info(const std::string& category, const std::string& message)
118   -{
119   - log( category, message, LogLevel::Info );
120   -}
121   -
122   -void Log::warning(const std::string& category, const std::string& message)
123   -{
124   - log(category, message, LogLevel::Warning );
125   -}
126   -
127   -void Log::error(const std::string& category, const std::string& message)
128   -{
129   - log(category, message, LogLevel::Error );
130   -}
131   -
132   -void Log::trace(const char *file, int line, const std::string &category, const std::string &message)
133   -{
134   - log(category, fileinfoMessage(file, line, message), LogLevel::Trace );
135   -}
136   -
137   -void Log::debug(const char* file, int line, const std::string& category, const std::string& message)
138   -{
139   - log( category, fileinfoMessage(file, line, message), LogLevel::Debug );
140   -}
141   -
142   -void Log::info(const char* file, int line, const std::string& category, const std::string& message)
143   -{
144   - log( category, fileinfoMessage(file, line, message), LogLevel::Info );
145   -}
146   -
147   -void Log::warning(const char* file, int line, const std::string& category, const std::string& message)
148   -{
149   - log( category, fileinfoMessage(file, line, message), LogLevel::Warning);
150   -}
151   -
152   -void Log::error(const char* file, int line, const std::string& category, const std::string& message)
153   -{
154   - log( category, fileinfoMessage(file, line, message), LogLevel::Error );
155   -}
156   -
157   -void Log::ReplaceAll( std::string &strToReplace,
158   - const std::string& from_chars,
159   - const std::string& to_chars )
160   -{
161   - size_t start_pos = 0;
162   - while( ( start_pos = strToReplace.find( from_chars, start_pos ) ) != std::string::npos )
163   - {
164   - strToReplace.replace( start_pos, from_chars.length(), to_chars );
165   - start_pos += to_chars.length(); // Handles case where 'to' is a substring of 'from'
166   - }
167   -}
src/mqttclient.cpp
... ... @@ -26,7 +26,6 @@
26 26 #include "mqttutil.h"
27 27 #include "mqttidgenerator.h"
28 28 #include "mqtttypeconverter.h"
29   -#include "log.h"
30 29 #include "lockguard.h"
31 30 #include "uriparser.h"
32 31  
... ... @@ -64,29 +63,26 @@ MqttClient::MqttClient(const std::string&amp; _clientId, const std::function&lt;void(co
64 63 , m_eventQueue(_clientId)
65 64 , m_workerThread( std::thread( &MqttClient::eventHandler, this ) )
66 65 {
67   - // Initialize the logger.
68   - Log::init( "mqtt-cpp", "", LogLevel::Info );
69 66 }
70 67  
71 68 MqttClient::~MqttClient()
72 69 {
73   - LogDebug( "MqttClient", std::string( m_clientId + " - disconnect" ) );
74 70 {
75   - LogDebug( "MqttClient", std::string( m_clientId + " - disconnect" ) );
  71 + // LogDebug( "MqttClient", std::string( m_clientId + " - disconnect" ) );
76 72 this->disconnect();
77 73 decltype(m_principalClient) principalClient{};
78 74  
79 75 OSDEV_COMPONENTS_LOCKGUARD(m_internalMutex);
80   - LogDebug( "MqttClient", std::string( m_clientId + " - cleanup principal client" ) );
  76 + // LogDebug( "MqttClient", std::string( m_clientId + " - cleanup principal client" ) );
81 77 m_principalClient.swap(principalClient);
82 78 }
83 79  
84   - LogDebug( "MqttClient", std::string( m_clientId + " - dtor stop queue" ) );
  80 + // LogDebug( "MqttClient", std::string( m_clientId + " - dtor stop queue" ) );
85 81 m_eventQueue.stop();
86 82 if (m_workerThread.joinable()) {
87 83 m_workerThread.join();
88 84 }
89   - LogDebug( "MqttClient", std::string( m_clientId + " - dtor ready" ) );
  85 + // LogDebug( "MqttClient", std::string( m_clientId + " - dtor ready" ) );
90 86 }
91 87  
92 88 std::string MqttClient::clientId() const
... ... @@ -137,7 +133,7 @@ void MqttClient::connect(const std::string&amp; host, int port, const Credentials&amp; c
137 133  
138 134 void MqttClient::connect(const std::string& _endpoint)
139 135 {
140   - LogInfo( "MqttClient", std::string( m_clientId + " - Request connect" ) );
  136 + // LogInfo( "MqttClient", std::string( m_clientId + " - Request connect" ) );
141 137  
142 138 OSDEV_COMPONENTS_LOCKGUARD(m_interfaceMutex);
143 139 IMqttClientImpl* client(nullptr);
... ... @@ -151,7 +147,7 @@ void MqttClient::connect(const std::string&amp; _endpoint)
151 147 }
152 148 else
153 149 {
154   - LogError( "MqttClient", std::string( m_clientId + " - Cannot connect to different endpoint. Disconnect first." ) );
  150 + // LogError( "MqttClient", std::string( m_clientId + " - Cannot connect to different endpoint. Disconnect first." ) );
155 151 return;
156 152 }
157 153 }
... ... @@ -171,7 +167,7 @@ void MqttClient::connect(const std::string&amp; _endpoint)
171 167  
172 168 void MqttClient::disconnect()
173 169 {
174   - LogInfo( "MqttClient", std::string( m_clientId + " - Request disconnect" ) );
  170 + // LogInfo( "MqttClient", std::string( m_clientId + " - Request disconnect" ) );
175 171 OSDEV_COMPONENTS_LOCKGUARD(m_interfaceMutex);
176 172  
177 173 decltype(m_additionalClients) additionalClients{};
... ... @@ -192,7 +188,7 @@ void MqttClient::disconnect()
192 188 }
193 189  
194 190  
195   - LogDebug( "MqttClient", std::string( m_clientId + " - Unsubscribe and disconnect clients" ) );
  191 + // LogDebug( "MqttClient", std::string( m_clientId + " - Unsubscribe and disconnect clients" ) );
196 192 // DebugLogToFIle ("MqttClient", "%1 - Unsubscribe and disconnect clients", m_clientId);
197 193 for ( auto& cl : clients )
198 194 {
... ... @@ -228,6 +224,7 @@ Token MqttClient::publish(const MqttMessage&amp; message, int qos)
228 224 }
229 225 // ErrorLogToFIle ("MqttClient", "%1 - Unable to publish, not connected", m_clientId);
230 226 // Throw (MqttException, "Not connected");
  227 + return Token(m_clientId, -1);
231 228 }
232 229 client = m_principalClient.get();
233 230 }
... ... @@ -246,6 +243,7 @@ Token MqttClient::subscribe(const std::string&amp; topic, int qos, const std::functi
246 243 {
247 244 // ErrorLogToFIle ("MqttClient", "%1 - Unable to subscribe, not connected", m_clientId);
248 245 // throw (?)(MqttException, "Not connected");
  246 + return Token(m_clientId, -1);
249 247 }
250 248 if (!m_principalClient->isOverlapping(topic)) {
251 249 client = m_principalClient.get();
... ... @@ -287,6 +285,7 @@ std::set&lt;Token&gt; MqttClient::unsubscribe(const std::string&amp; topic, int qos)
287 285 if (!m_principalClient || m_principalClient->connectionStatus() == ConnectionStatus::Disconnected) {
288 286 // ErrorLogToFIle ("MqttClient", "%1 - Unable to unsubscribe, not connected", m_clientId);
289 287 // Throw (MqttException, "Not connected");
  288 + return std::set<Token>();
290 289 }
291 290 clients.push_back(m_principalClient.get());
292 291 for (const auto& c : m_additionalClients) {
... ...
src/threadcontext.cpp deleted
1   -#include "threadcontext.h"
2   -
3   -// std
4   -#include <thread>
5   -
6   -using namespace osdev::components::mqtt;
7   -
8   -ThreadContextScope::ThreadContextScope( const std::string &_context )
9   - : m_previousContext( ThreadContext::instance().context() )
10   -{
11   - ThreadContext::instance().setContext( _context );
12   -}
13   -
14   -ThreadContextScope::~ThreadContextScope()
15   -{
16   - ThreadContext::instance().setContext( m_previousContext );
17   -}
18   -
19   -// static
20   -ThreadContext& ThreadContext::instance()
21   -{
22   - static thread_local ThreadContext tc;
23   - return tc;
24   -}
25   -
26   -ThreadContext::ThreadContext()
27   - : m_context( "default" )
28   -{
29   -
30   -}