Commit 4315fb5bfbad751700708ebd6d15e1bb833e16a5

Authored by Wiebe Cazemier
1 parent 4ee42838

Start of simple logger

CMakeLists.txt
@@ -20,6 +20,7 @@ add_executable(FlashMQ @@ -20,6 +20,7 @@ add_executable(FlashMQ
20 rwlockguard.cpp 20 rwlockguard.cpp
21 retainedmessage.cpp 21 retainedmessage.cpp
22 cirbuf.cpp 22 cirbuf.cpp
  23 + logger.cpp
23 ) 24 )
24 25
25 target_link_libraries(FlashMQ pthread) 26 target_link_libraries(FlashMQ pthread)
FlashMQTests/FlashMQTests.pro
@@ -25,6 +25,7 @@ SOURCES += tst_maintests.cpp \ @@ -25,6 +25,7 @@ SOURCES += tst_maintests.cpp \
25 ../threaddata.cpp \ 25 ../threaddata.cpp \
26 ../types.cpp \ 26 ../types.cpp \
27 ../utils.cpp \ 27 ../utils.cpp \
  28 + ../logger.cpp \
28 mainappthread.cpp \ 29 mainappthread.cpp \
29 twoclienttestcontext.cpp 30 twoclienttestcontext.cpp
30 31
@@ -42,5 +43,6 @@ HEADERS += \ @@ -42,5 +43,6 @@ HEADERS += \
42 ../threaddata.h \ 43 ../threaddata.h \
43 ../types.h \ 44 ../types.h \
44 ../utils.h \ 45 ../utils.h \
  46 + ../logger.h \
45 mainappthread.h \ 47 mainappthread.h \
46 twoclienttestcontext.h 48 twoclienttestcontext.h
logger.cpp 0 → 100644
  1 +#include "logger.h"
  2 +#include <ctime>
  3 +#include <sstream>
  4 +#include <iomanip>
  5 +
  6 +Logger *Logger::instance = nullptr;
  7 +
  8 +Logger::Logger()
  9 +{
  10 +
  11 +}
  12 +
  13 +std::string Logger::getLogLevelString(int level) const
  14 +{
  15 + switch (level)
  16 + {
  17 + case LOG_NONE:
  18 + return "NONE";
  19 + case LOG_INFO:
  20 + return "INFO";
  21 + case LOG_NOTICE:
  22 + return "NOTICE";
  23 + case LOG_WARNING:
  24 + return "WARNING";
  25 + case LOG_ERR:
  26 + return "ERROR";
  27 + case LOG_DEBUG:
  28 + return "DEBUG";
  29 + default:
  30 + return "UNKNOWN LOG LEVEL";
  31 + }
  32 +}
  33 +
  34 +Logger *Logger::getInstance()
  35 +{
  36 + if (instance == nullptr)
  37 + instance = new Logger();
  38 + return instance;
  39 +}
  40 +
  41 +void Logger::logf(int level, const char *str, ...)
  42 +{
  43 + if (level > curLogLevel)
  44 + return;
  45 +
  46 + std::lock_guard<std::mutex> locker(logMutex);
  47 +
  48 + time_t time = std::time(nullptr);
  49 + struct tm tm = *std::localtime(&time);
  50 + std::ostringstream oss;
  51 +
  52 + std::string str_(str);
  53 + oss << "[" << std::put_time(&tm, "%Y-%m-%d %H:%M:%S") << "] [" << getLogLevelString(level) << "] " << str_;
  54 + oss.flush();
  55 + const std::string s = oss.str();
  56 + const char *logfmtstring = s.c_str();
  57 +
  58 + va_list valist;
  59 + va_start(valist, str);
  60 + if (this->file)
  61 + {
  62 + vfprintf(this->file, logfmtstring, valist);
  63 + fprintf(this->file, "\n");
  64 + fflush(this->file);
  65 + }
  66 + else
  67 + {
  68 +#ifdef TESTING
  69 + vfprintf(stderr, logfmtstring, valist); // the stdout interfers with Qt test XML output, so using stderr.
  70 + fprintf(stderr, "\n");
  71 + fflush(stderr);
  72 +#else
  73 + vfprintf(stdout, logfmtstring, valist);
  74 + fprintf(stdout, "\n");
  75 + fflush(stdout);
  76 +#endif
  77 + }
  78 + va_end(valist);
  79 +}
logger.h 0 → 100644
  1 +#ifndef LOGGER_H
  2 +#define LOGGER_H
  3 +
  4 +#include <stdio.h>
  5 +#include <stdarg.h>
  6 +#include <mutex>
  7 +
  8 +// Compatible with Mosquitto, for auth plugin compatability.
  9 +#define LOG_NONE 0x00
  10 +#define LOG_INFO 0x01
  11 +#define LOG_NOTICE 0x02
  12 +#define LOG_WARNING 0x04
  13 +#define LOG_ERR 0x08
  14 +#define LOG_DEBUG 0x10
  15 +
  16 +class Logger
  17 +{
  18 + static Logger *instance;
  19 + int curLogLevel = LOG_DEBUG;
  20 + std::mutex logMutex;
  21 + FILE *file = nullptr;
  22 +
  23 + Logger();
  24 + std::string getLogLevelString(int level) const;
  25 +
  26 +public:
  27 + static Logger *getInstance();
  28 + void logf(int level, const char *str, ...);
  29 +
  30 +};
  31 +
  32 +#endif // LOGGER_H