From 423cc62e12c6ac5c949148b3dfaeb5112b29ffff Mon Sep 17 00:00:00 2001 From: f4exb Date: Sat, 11 Nov 2017 23:44:05 +0100 Subject: [PATCH] Logging: added a console and optional dual file logger --- logging/CMakeLists.txt | 2 + logging/loggerwithfile.cpp | 39 +++++++++++++++++++ logging/loggerwithfile.h | 80 ++++++++++++++++++++++++++++++++++++++ logging/logging.pri | 4 +- 4 files changed, 123 insertions(+), 2 deletions(-) create mode 100644 logging/loggerwithfile.cpp create mode 100644 logging/loggerwithfile.h diff --git a/logging/CMakeLists.txt b/logging/CMakeLists.txt index 38e2cb3de..4c615fae8 100644 --- a/logging/CMakeLists.txt +++ b/logging/CMakeLists.txt @@ -2,6 +2,7 @@ project(logging) set(logging_SOURCES dualfilelogger.cpp + loggerwithfile.cpp filelogger.cpp logger.cpp logmessage.cpp @@ -9,6 +10,7 @@ set(logging_SOURCES set(httpserver_HEADERS dualfilelogger.h + loggerwithfile.h filelogger.h logger.h logmessage.h diff --git a/logging/loggerwithfile.cpp b/logging/loggerwithfile.cpp new file mode 100644 index 000000000..72009fe0d --- /dev/null +++ b/logging/loggerwithfile.cpp @@ -0,0 +1,39 @@ +/* + * loggerwithfile.cpp + * + * Created on: Nov 11, 2017 + * Author: f4exb + */ + +#include "loggerwithfile.h" + +using namespace qtwebapp; + +LoggerWithFile::LoggerWithFile(QObject* parent) + :Logger(parent), fileLogger(0), useFileFlogger(false) +{ + consoleLogger = new Logger(this); +} + +void LoggerWithFile::createFileLogger(const FileLoggerSettings& settings, const int refreshInterval) +{ + fileLogger = new FileLogger(settings, refreshInterval, this); +} + +void LoggerWithFile::log(const QtMsgType type, const QString& message, const QString &file, const QString &function, const int line) +{ + consoleLogger->log(type,message,file,function,line); + + if (fileLogger && useFileFlogger) { + fileLogger->log(type,message,file,function,line); + } +} + +void LoggerWithFile::clear(const bool buffer, const bool variables) +{ + consoleLogger->clear(buffer,variables); + + if (fileLogger && useFileFlogger) { + fileLogger->clear(buffer,variables); + } +} diff --git a/logging/loggerwithfile.h b/logging/loggerwithfile.h new file mode 100644 index 000000000..67bbe09d5 --- /dev/null +++ b/logging/loggerwithfile.h @@ -0,0 +1,80 @@ +/* + * loggerwithfile.h + * + * Created on: Nov 11, 2017 + * Author: f4exb + */ + +#ifndef LOGGING_LOGGERWITHFILE_H_ +#define LOGGING_LOGGERWITHFILE_H_ + +#include +#include "logger.h" +#include "filelogger.h" + +namespace qtwebapp { + +/** + Logs messages to console and optionally to a file simultaneously. + @see FileLogger for a description of the two underlying file logger. + @see Logger for a description of the console loger. +*/ + +class DECLSPEC LoggerWithFile : public Logger { + Q_OBJECT + Q_DISABLE_COPY(LoggerWithFile) + +public: + LoggerWithFile(QObject *parent = 0); + + void createFileLogger(const FileLoggerSettings& settings, const int refreshInterval=10000); + + /** + Decorate and log the message, if type>=minLevel. + This method is thread safe. + @param type Message type (level) + @param message Message text + @param file Name of the source file where the message was generated (usually filled with the macro __FILE__) + @param function Name of the function where the message was generated (usually filled with the macro __LINE__) + @param line Line Number of the source file, where the message was generated (usually filles with the macro __func__ or __FUNCTION__) + @see LogMessage for a description of the message decoration. + */ + virtual void log(const QtMsgType type, const QString& message, const QString &file="", const QString &function="", const int line=0); + + /** + Clear the thread-local data of the current thread. + This method is thread safe. + @param buffer Whether to clear the backtrace buffer + @param variables Whether to clear the log variables + */ + virtual void clear(const bool buffer=true, const bool variables=true); + + bool getUseFileLogger() const { return useFileFlogger; } + void setUseFileLogger(bool use) { useFileFlogger = use; } + + /** + * Get a file logger settings copy + * @return The current file logger settings + */ + FileLoggerSettings getFileLoggerSettings() const { return fileLogger->getFileLoggerSettings(); } + + /** + * Set new file logger settings data + * @param File logger settings to replace current data + */ + void setFileLoggerSettings(const FileLoggerSettings& settings) { fileLogger->setFileLoggerSettings(settings); } + +private: + /** First console logger */ + Logger* consoleLogger; + + /** Second file logger */ + FileLogger* fileLogger; + + /** Use file logger indicator */ + bool useFileFlogger; +}; + +} // end of namespace + +#endif /* LOGGING_LOGGERWITHFILE_H_ */ diff --git a/logging/logging.pri b/logging/logging.pri index f62338d66..67f5aada8 100644 --- a/logging/logging.pri +++ b/logging/logging.pri @@ -1,6 +1,6 @@ INCLUDEPATH += $$PWD DEPENDPATH += $$PWD -HEADERS += $$PWD/logglobal.h $$PWD/logmessage.h $$PWD/logger.h $$PWD/filelogger.h $$PWD/dualfilelogger.h +HEADERS += $$PWD/logglobal.h $$PWD/logmessage.h $$PWD/logger.h $$PWD/filelogger.h $$PWD/dualfilelogger.h $$PWD/loggerwithfile.h -SOURCES += $$PWD/logmessage.cpp $$PWD/logger.cpp $$PWD/filelogger.cpp $$PWD/dualfilelogger.cpp +SOURCES += $$PWD/logmessage.cpp $$PWD/logger.cpp $$PWD/filelogger.cpp $$PWD/dualfilelogger.cpp $$PWD/loggerwithfile.cpp