Commit 6990c6895e4898f8a8655309eef5ffc5d778ec63

Authored by Wiebe Cazemier
1 parent 38563f95

Implement the log levels

authplugin.cpp
@@ -50,7 +50,7 @@ void AuthPlugin::loadPlugin(const std::string &pathToSoFile) @@ -50,7 +50,7 @@ void AuthPlugin::loadPlugin(const std::string &pathToSoFile)
50 if (pathToSoFile.empty()) 50 if (pathToSoFile.empty())
51 return; 51 return;
52 52
53 - logger->logf(LOG_INFO, "Loading auth plugin %s", pathToSoFile.c_str()); 53 + logger->logf(LOG_NOTICE, "Loading auth plugin %s", pathToSoFile.c_str());
54 54
55 initialized = false; 55 initialized = false;
56 wanted = true; 56 wanted = true;
configfileparser.cpp
@@ -66,6 +66,8 @@ ConfigFileParser::ConfigFileParser(const std::string &path) : @@ -66,6 +66,8 @@ ConfigFileParser::ConfigFileParser(const std::string &path) :
66 validKeys.insert("auth_plugin_serialize_auth_checks"); 66 validKeys.insert("auth_plugin_serialize_auth_checks");
67 validKeys.insert("client_initial_buffer_size"); 67 validKeys.insert("client_initial_buffer_size");
68 validKeys.insert("max_packet_size"); 68 validKeys.insert("max_packet_size");
  69 + validKeys.insert("log_debug");
  70 + validKeys.insert("log_subscriptions");
69 71
70 validListenKeys.insert("port"); 72 validListenKeys.insert("port");
71 validListenKeys.insert("protocol"); 73 validListenKeys.insert("protocol");
@@ -303,6 +305,18 @@ void ConfigFileParser::loadFile(bool test) @@ -303,6 +305,18 @@ void ConfigFileParser::loadFile(bool test)
303 } 305 }
304 tmpSettings->maxPacketSize = newVal; 306 tmpSettings->maxPacketSize = newVal;
305 } 307 }
  308 +
  309 + if (key == "log_debug")
  310 + {
  311 + bool tmp = stringTruthiness(value);
  312 + tmpSettings->logDebug = tmp;
  313 + }
  314 +
  315 + if (key == "log_subscriptions")
  316 + {
  317 + bool tmp = stringTruthiness(value);
  318 + tmpSettings->logSubscriptions = tmp;
  319 + }
306 } 320 }
307 } 321 }
308 catch (std::invalid_argument &ex) // catch for the stoi() 322 catch (std::invalid_argument &ex) // catch for the stoi()
iowrapper.cpp
@@ -265,7 +265,7 @@ ssize_t IoWrapper::writeOrSslWrite(int fd, const void *buf, size_t nbytes, IoWra @@ -265,7 +265,7 @@ ssize_t IoWrapper::writeOrSslWrite(int fd, const void *buf, size_t nbytes, IoWra
265 unsigned long error_code = ERR_get_error(); 265 unsigned long error_code = ERR_get_error();
266 if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) 266 if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE)
267 { 267 {
268 - logger->logf(LOG_DEBUG, "Write is incomplete: %d", err); 268 + logger->logf(LOG_DEBUG, "SSL Write is incomplete: %d. Will be retried later.", err);
269 *error = IoWrapResult::Wouldblock; 269 *error = IoWrapResult::Wouldblock;
270 IncompleteSslWrite sslAction(buf_, nbytes_); 270 IncompleteSslWrite sslAction(buf_, nbytes_);
271 this->incompleteSslWrite = sslAction; 271 this->incompleteSslWrite = sslAction;
logger.cpp
@@ -81,9 +81,24 @@ void Logger::setLogPath(const std::string &path) @@ -81,9 +81,24 @@ void Logger::setLogPath(const std::string &path)
81 Logger::logPath = path; 81 Logger::logPath = path;
82 } 82 }
83 83
  84 +void Logger::setFlags(bool logDebug, bool logSubscriptions)
  85 +{
  86 + std::lock_guard<std::mutex> locker(logMutex);
  87 +
  88 + if (logDebug)
  89 + curLogLevel |= LOG_DEBUG;
  90 + else
  91 + curLogLevel &= ~LOG_DEBUG;
  92 +
  93 + if (logSubscriptions)
  94 + curLogLevel |= (LOG_UNSUBSCRIBE & LOG_SUBSCRIBE);
  95 + else
  96 + curLogLevel &= ~(LOG_UNSUBSCRIBE & LOG_SUBSCRIBE);
  97 +}
  98 +
84 void Logger::logf(int level, const char *str, va_list valist) 99 void Logger::logf(int level, const char *str, va_list valist)
85 { 100 {
86 - if (level > curLogLevel) // TODO: wrong: bitmap based 101 + if ((level & curLogLevel) == 0)
87 return; 102 return;
88 103
89 std::lock_guard<std::mutex> locker(logMutex); 104 std::lock_guard<std::mutex> locker(logMutex);
logger.h
@@ -6,12 +6,15 @@ @@ -6,12 +6,15 @@
6 #include <mutex> 6 #include <mutex>
7 7
8 // Compatible with Mosquitto, for auth plugin compatability. 8 // Compatible with Mosquitto, for auth plugin compatability.
  9 +// Can be OR'ed together.
9 #define LOG_NONE 0x00 10 #define LOG_NONE 0x00
10 #define LOG_INFO 0x01 11 #define LOG_INFO 0x01
11 #define LOG_NOTICE 0x02 12 #define LOG_NOTICE 0x02
12 #define LOG_WARNING 0x04 13 #define LOG_WARNING 0x04
13 #define LOG_ERR 0x08 14 #define LOG_ERR 0x08
14 #define LOG_DEBUG 0x10 15 #define LOG_DEBUG 0x10
  16 +#define LOG_SUBSCRIBE 0x20
  17 +#define LOG_UNSUBSCRIBE 0x40
15 18
16 int logSslError(const char *str, size_t len, void *u); 19 int logSslError(const char *str, size_t len, void *u);
17 20
@@ -19,7 +22,7 @@ class Logger @@ -19,7 +22,7 @@ class Logger
19 { 22 {
20 static Logger *instance; 23 static Logger *instance;
21 static std::string logPath; 24 static std::string logPath;
22 - int curLogLevel = LOG_DEBUG; 25 + int curLogLevel = LOG_ERR | LOG_WARNING | LOG_NOTICE | LOG_INFO | LOG_SUBSCRIBE | LOG_UNSUBSCRIBE ;
23 std::mutex logMutex; 26 std::mutex logMutex;
24 FILE *file = nullptr; 27 FILE *file = nullptr;
25 bool alsoLogToStd = true; 28 bool alsoLogToStd = true;
@@ -35,6 +38,7 @@ public: @@ -35,6 +38,7 @@ public:
35 void noLongerLogToStd(); 38 void noLongerLogToStd();
36 39
37 void setLogPath(const std::string &path); 40 void setLogPath(const std::string &path);
  41 + void setFlags(bool logDebug, bool logSubscriptions);
38 42
39 }; 43 };
40 44
main.cpp
@@ -40,7 +40,7 @@ int register_signal_handers() @@ -40,7 +40,7 @@ int register_signal_handers()
40 if (sigaction(SIGHUP, &sa, nullptr) != 0 || sigaction(SIGTERM, &sa, nullptr) != 0 || sigaction(SIGINT, &sa, nullptr) != 0) 40 if (sigaction(SIGHUP, &sa, nullptr) != 0 || sigaction(SIGTERM, &sa, nullptr) != 0 || sigaction(SIGINT, &sa, nullptr) != 0)
41 { 41 {
42 Logger *logger = Logger::getInstance(); 42 Logger *logger = Logger::getInstance();
43 - logger->logf(LOG_INFO, "Error registering signal handlers"); 43 + logger->logf(LOG_ERR, "Error registering signal handlers");
44 return -1; 44 return -1;
45 } 45 }
46 46
mainapp.cpp
@@ -585,6 +585,7 @@ void MainApp::loadConfig() @@ -585,6 +585,7 @@ void MainApp::loadConfig()
585 585
586 logger->setLogPath(settings->logPath); 586 logger->setLogPath(settings->logPath);
587 logger->reOpen(); 587 logger->reOpen();
  588 + logger->setFlags(settings->logDebug, settings->logSubscriptions);
588 589
589 setlimits(1000000); 590 setlimits(1000000);
590 591
mqttpacket.cpp
@@ -360,7 +360,7 @@ void MqttPacket::handleSubscribe() @@ -360,7 +360,7 @@ void MqttPacket::handleSubscribe()
360 if (qos > 2) 360 if (qos > 2)
361 throw ProtocolError("QoS is greater than 2, and/or reserved bytes in QoS field are not 0."); 361 throw ProtocolError("QoS is greater than 2, and/or reserved bytes in QoS field are not 0.");
362 362
363 - logger->logf(LOG_INFO, "Client '%s' subscribed to '%s'", sender->repr().c_str(), topic.c_str()); 363 + logger->logf(LOG_SUBSCRIBE, "Client '%s' subscribed to '%s'", sender->repr().c_str(), topic.c_str());
364 sender->getThreadData()->getSubscriptionStore()->addSubscription(sender, topic, qos); 364 sender->getThreadData()->getSubscriptionStore()->addSubscription(sender, topic, qos);
365 subs_reponse_codes.push_back(qos); 365 subs_reponse_codes.push_back(qos);
366 } 366 }
@@ -388,7 +388,7 @@ void MqttPacket::handleUnsubscribe() @@ -388,7 +388,7 @@ void MqttPacket::handleUnsubscribe()
388 throw ProtocolError("Subscribe topic not valid UTF-8."); 388 throw ProtocolError("Subscribe topic not valid UTF-8.");
389 389
390 sender->getThreadData()->getSubscriptionStore()->removeSubscription(sender, topic); 390 sender->getThreadData()->getSubscriptionStore()->removeSubscription(sender, topic);
391 - logger->logf(LOG_INFO, "Client '%s' unsubscribed to '%s'", sender->repr().c_str(), topic.c_str()); 391 + logger->logf(LOG_UNSUBSCRIBE, "Client '%s' unsubscribed from '%s'", sender->repr().c_str(), topic.c_str());
392 } 392 }
393 393
394 UnsubAck unsubAck(packet_id); 394 UnsubAck unsubAck(packet_id);
settings.h
@@ -23,6 +23,8 @@ public: @@ -23,6 +23,8 @@ public:
23 bool authPluginSerializeAuthChecks = false; 23 bool authPluginSerializeAuthChecks = false;
24 int clientInitialBufferSize = 1024; // Must be power of 2 24 int clientInitialBufferSize = 1024; // Must be power of 2
25 int maxPacketSize = 268435461; // 256 MB + 5 25 int maxPacketSize = 268435461; // 256 MB + 5
  26 + bool logDebug = false;
  27 + bool logSubscriptions = false;
26 std::list<std::shared_ptr<Listener>> listeners; // Default one is created later, when none are defined. 28 std::list<std::shared_ptr<Listener>> listeners; // Default one is created later, when none are defined.
27 29
28 AuthOptCompatWrap &getAuthOptsCompat(); 30 AuthOptCompatWrap &getAuthOptsCompat();