From 3f1e12d7c36755e14aa05a476a5791f36cc8d0e7 Mon Sep 17 00:00:00 2001 From: Kubat <mael.martin31@gmail.com> Date: Tue, 31 Aug 2021 16:09:28 +0200 Subject: [PATCH] LOG: Fix the VIVY_LOGGABLE_OBJECT macro --- PreCompiledHeaders.cmake | 1 + src/Lib/Log.cc | 11 +++++-- src/Lib/Log.hh | 70 +++++++++++++++++----------------------- src/Lib/Utils.hh | 2 -- src/VivyApplication.hh | 3 ++ 5 files changed, 41 insertions(+), 46 deletions(-) diff --git a/PreCompiledHeaders.cmake b/PreCompiledHeaders.cmake index 4509f913..c5102852 100644 --- a/PreCompiledHeaders.cmake +++ b/PreCompiledHeaders.cmake @@ -31,6 +31,7 @@ set(EXT_INC PRIVATE set(QT_STRUCT_INC PRIVATE + <qglobal.h> <QtGlobal> <QObject> <QRegularExpression> diff --git a/src/Lib/Log.cc b/src/Lib/Log.cc index 7942c1d5..020add3d 100644 --- a/src/Lib/Log.cc +++ b/src/Lib/Log.cc @@ -3,7 +3,6 @@ using namespace Vivy; LogSink::~LogSink() noexcept {} - LogMessage::~LogMessage() noexcept { parentLogger->sendLogMessage(*this); } std::shared_ptr<LogSink> @@ -15,6 +14,12 @@ LogSink::newSink() noexcept return std::make_shared<makeSharedEnabler>(); } +void +LogSink::registerLoggerClient(std::shared_ptr<Logger> logger) noexcept +{ + clientLoggers.push_back(logger); +} + void LogSink::closeLoggerClient(Logger *const logger) noexcept { @@ -41,8 +46,8 @@ Logger::logEvent(const char *fileName, const char *functionName, const int lineN } void -Logger::sendLogMessage(const LogMessage &msg) const noexcept -{ +Logger::sendLogMessage(const LogMessage &msg) const noexcept { + TODO(Write the code to send the event...) } LogMessage::LogMessage(Logger *const logger, const LogMessage::Header hdr) noexcept diff --git a/src/Lib/Log.hh b/src/Lib/Log.hh index 97f0697a..b5a4e6f4 100644 --- a/src/Lib/Log.hh +++ b/src/Lib/Log.hh @@ -1,10 +1,9 @@ #pragma once #include "Utils.hh" -#include <sstream> #define VIVY_NEW_LOG_SINK() LogSink::newSink() -#define VIVY_GET_LOGGER(sink, cat) sink->createClient(cat) +#define VIVY_GET_LOGGER(sink, cat) std::make_shared<Logger>(sink.get(), cat) #define VIVY_LOG_WITH_LEVEL(log, level) log->logEvent(__FILE__, __func__, __LINE__, level) #define VIVY_LOG_WARN(log) VIVY_LOG_WITH_LEVEL(log, LogLevel::Warning) #define VIVY_LOG_DEBUG(log) VIVY_LOG_WITH_LEVEL(log, LogLevel::Debug) @@ -41,6 +40,26 @@ enum class LogLevel : int { None = std::numeric_limits<int>::min() // In option setup to disable logs }; +// LogSink is the parent logger. It will recieve messages and display them to a +// console or to std::cerr or to a file, etc. +class LogSink { + VIVY_UNMOVABLE_OBJECT(LogSink) + + LogSink() noexcept = default; + +public: + static std::shared_ptr<LogSink> newSink() noexcept; + ~LogSink() noexcept; + + void registerLoggerClient(std::shared_ptr<Logger>) noexcept; + void closeLoggerClient(Logger *const) noexcept; + void closeLoggerClient(const std::shared_ptr<Logger> &) noexcept; + +private: + std::vector<LogMessage> messageQueue; + std::vector<std::shared_ptr<Logger>> clientLoggers; +}; + // Message to be logged, constructed by a logger then send class LogMessage final { friend class Logger; @@ -74,7 +93,7 @@ public: // A logger class, a client to LogSink. Will generate and send instances of // LogMessage. -class Logger { +class Logger : public std::enable_shared_from_this<Logger> { VIVY_UNMOVABLE_OBJECT(Logger) friend class LogSink; @@ -84,59 +103,28 @@ class Logger { static std::string anyStringToStdString(StringType auto const &str) noexcept { std::string ret; + std::size_t i; const std::size_t size = str.size(); ret.resize(size); - for (std::size_t i = 0; i < size; ++i) + for (i = 0; i < size && str[i]; ++i) ret[i] = str[i]; + ret.resize(i); return ret; } +public: // Templated constructor, construct from anything that can be considered as // a string. - Logger(LogSink *const sink, StringType auto const &category) noexcept + Logger(LogSink *const sink, const std::string_view category) noexcept : parentLogSink(sink) , logCategory(anyStringToStdString(category)) { + std::shared_ptr<Logger> self = shared_from_this(); + sink->registerLoggerClient(self); } - // Shortcut because of std::make_shared - static std::shared_ptr<Logger> createAndAttachClient(LogSink *const sink, - StringType auto const &category) noexcept - { - struct makeSharedEnabler : public Logger { - // NOTE: For make_shared with private CTor - }; - return std::make_shared<makeSharedEnabler>(sink, category); - } - -public: void sendLogMessage(const LogMessage &) const noexcept; LogMessage logEvent(const char *fileName, const char *functionName, const int lineNumber, const LogLevel) noexcept; }; - -// LogSink is the parent logger. It will recieve messages and display them to a -// console or to std::cerr or to a file, etc. -class LogSink { - VIVY_UNMOVABLE_OBJECT(LogSink) - - LogSink() noexcept = default; - -public: - static std::shared_ptr<LogSink> newSink() noexcept; - ~LogSink() noexcept; - - void closeLoggerClient(Logger *const) noexcept; - void closeLoggerClient(const std::shared_ptr<Logger> &) noexcept; - std::shared_ptr<Logger> createClient(StringType auto const &category) noexcept - { - auto ret = Logger::createAndAttachClient(this, category); - clientLoggers.push_back(ret); - return ret; - } - -private: - std::vector<LogMessage> messageQueue; - std::vector<std::shared_ptr<Logger>> clientLoggers; -}; } diff --git a/src/Lib/Utils.hh b/src/Lib/Utils.hh index c042365b..da89c510 100644 --- a/src/Lib/Utils.hh +++ b/src/Lib/Utils.hh @@ -4,8 +4,6 @@ #error "This is a C++ header" #endif -#include <qglobal.h> - #define VIVY_PRAGMA(x) _Pragma(#x) #define TODO(x) VIVY_PRAGMA(message("\"TODO: " #x "\"")) diff --git a/src/VivyApplication.hh b/src/VivyApplication.hh index 0bfa830a..493ab004 100644 --- a/src/VivyApplication.hh +++ b/src/VivyApplication.hh @@ -31,6 +31,7 @@ #include "Lib/Script/ScriptStore.hh" #include "Lib/Document/VivyDocumentStore.hh" +#include "Lib/Log.hh" #include "UI/MainWindow.hh" namespace Vivy @@ -40,6 +41,8 @@ class MainWindow; // Vivy application class class VivyApplication : public QApplication { Q_OBJECT + VIVY_DCL_LOG_SINK(logSink) + VIVY_LOGGABLE_OBJECT(logSink, std::string_view("APPLICATION"), logger) public: VivyDocumentStore documentStore{}; -- GitLab