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 50 if (pathToSoFile.empty())
51 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 55 initialized = false;
56 56 wanted = true;
... ...
configfileparser.cpp
... ... @@ -66,6 +66,8 @@ ConfigFileParser::ConfigFileParser(const std::string &path) :
66 66 validKeys.insert("auth_plugin_serialize_auth_checks");
67 67 validKeys.insert("client_initial_buffer_size");
68 68 validKeys.insert("max_packet_size");
  69 + validKeys.insert("log_debug");
  70 + validKeys.insert("log_subscriptions");
69 71  
70 72 validListenKeys.insert("port");
71 73 validListenKeys.insert("protocol");
... ... @@ -303,6 +305,18 @@ void ConfigFileParser::loadFile(bool test)
303 305 }
304 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 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 265 unsigned long error_code = ERR_get_error();
266 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 269 *error = IoWrapResult::Wouldblock;
270 270 IncompleteSslWrite sslAction(buf_, nbytes_);
271 271 this->incompleteSslWrite = sslAction;
... ...
logger.cpp
... ... @@ -81,9 +81,24 @@ void Logger::setLogPath(const std::string &path)
81 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 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 102 return;
88 103  
89 104 std::lock_guard<std::mutex> locker(logMutex);
... ...
logger.h
... ... @@ -6,12 +6,15 @@
6 6 #include <mutex>
7 7  
8 8 // Compatible with Mosquitto, for auth plugin compatability.
  9 +// Can be OR'ed together.
9 10 #define LOG_NONE 0x00
10 11 #define LOG_INFO 0x01
11 12 #define LOG_NOTICE 0x02
12 13 #define LOG_WARNING 0x04
13 14 #define LOG_ERR 0x08
14 15 #define LOG_DEBUG 0x10
  16 +#define LOG_SUBSCRIBE 0x20
  17 +#define LOG_UNSUBSCRIBE 0x40
15 18  
16 19 int logSslError(const char *str, size_t len, void *u);
17 20  
... ... @@ -19,7 +22,7 @@ class Logger
19 22 {
20 23 static Logger *instance;
21 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 26 std::mutex logMutex;
24 27 FILE *file = nullptr;
25 28 bool alsoLogToStd = true;
... ... @@ -35,6 +38,7 @@ public:
35 38 void noLongerLogToStd();
36 39  
37 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 40 if (sigaction(SIGHUP, &sa, nullptr) != 0 || sigaction(SIGTERM, &sa, nullptr) != 0 || sigaction(SIGINT, &sa, nullptr) != 0)
41 41 {
42 42 Logger *logger = Logger::getInstance();
43   - logger->logf(LOG_INFO, "Error registering signal handlers");
  43 + logger->logf(LOG_ERR, "Error registering signal handlers");
44 44 return -1;
45 45 }
46 46  
... ...
mainapp.cpp
... ... @@ -585,6 +585,7 @@ void MainApp::loadConfig()
585 585  
586 586 logger->setLogPath(settings->logPath);
587 587 logger->reOpen();
  588 + logger->setFlags(settings->logDebug, settings->logSubscriptions);
588 589  
589 590 setlimits(1000000);
590 591  
... ...
mqttpacket.cpp
... ... @@ -360,7 +360,7 @@ void MqttPacket::handleSubscribe()
360 360 if (qos > 2)
361 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 364 sender->getThreadData()->getSubscriptionStore()->addSubscription(sender, topic, qos);
365 365 subs_reponse_codes.push_back(qos);
366 366 }
... ... @@ -388,7 +388,7 @@ void MqttPacket::handleUnsubscribe()
388 388 throw ProtocolError("Subscribe topic not valid UTF-8.");
389 389  
390 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 394 UnsubAck unsubAck(packet_id);
... ...
settings.h
... ... @@ -23,6 +23,8 @@ public:
23 23 bool authPluginSerializeAuthChecks = false;
24 24 int clientInitialBufferSize = 1024; // Must be power of 2
25 25 int maxPacketSize = 268435461; // 256 MB + 5
  26 + bool logDebug = false;
  27 + bool logSubscriptions = false;
26 28 std::list<std::shared_ptr<Listener>> listeners; // Default one is created later, when none are defined.
27 29  
28 30 AuthOptCompatWrap &getAuthOptsCompat();
... ...