From de9dd5014461273edaaaa429a701ee6925b9a0c2 Mon Sep 17 00:00:00 2001
From: Kubat <mael.martin31@gmail.com>
Date: Wed, 1 Sep 2021 16:28:13 +0200
Subject: [PATCH] LOG: Add a way to get the sink from the VivyApplication and
 add more possibilities for logging things

---
 src/Lib/Log.cc         | 11 +++++++++++
 src/Lib/Log.hh         | 22 ++++++++++++++--------
 src/Lib/Utils.hh       | 12 ------------
 src/VivyApplication.hh | 10 ++++++----
 4 files changed, 31 insertions(+), 24 deletions(-)

diff --git a/src/Lib/Log.cc b/src/Lib/Log.cc
index 5c97d0c8..d23cabcc 100644
--- a/src/Lib/Log.cc
+++ b/src/Lib/Log.cc
@@ -141,6 +141,17 @@ LogMessage::operator<<(const char *str) noexcept
     return *this;
 }
 
+LogMessage &
+LogMessage::operator<<(const char c) noexcept
+{
+    if (indexInArray < messageBufferLength - 1) {
+        textBuffer[indexInArray] = c;
+        ++indexInArray;
+        textBuffer[indexInArray] = '\0';
+    }
+    return *this;
+}
+
 const std::string_view
 LogMessage::getTextBuffer() const noexcept
 {
diff --git a/src/Lib/Log.hh b/src/Lib/Log.hh
index 805f857b..5b49381f 100644
--- a/src/Lib/Log.hh
+++ b/src/Lib/Log.hh
@@ -3,15 +3,15 @@
 #include "Utils.hh"
 
 // Create a logger with a category
-#define VIVY_GET_LOGGER(sink, cat) std::make_shared<Logger>(sink.get(), cat)
+#define VIVY_GET_LOGGER(sink, cat) std::make_shared<Logger>((sink).get(), std::string_view{ #cat })
 
 // Log something in a logger
 #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)
-#define VIVY_LOG_INFO(log)              VIVY_LOG_WITH_LEVEL(log, LogLevel::Info)
-#define VIVY_LOG_ERR(log)               VIVY_LOG_WITH_LEVEL(log, LogLevel::Error)
-#define VIVY_LOG_FATAL(log)             VIVY_LOG_WITH_LEVEL(log, LogLevel::Critical)
+#define VIVY_LOG_WARN(log)              VIVY_LOG_WITH_LEVEL(log, Vivy::LogLevel::Warning)
+#define VIVY_LOG_DEBUG(log)             VIVY_LOG_WITH_LEVEL(log, Vivy::LogLevel::Debug)
+#define VIVY_LOG_INFO(log)              VIVY_LOG_WITH_LEVEL(log, Vivy::LogLevel::Info)
+#define VIVY_LOG_ERR(log)               VIVY_LOG_WITH_LEVEL(log, Vivy::LogLevel::Error)
+#define VIVY_LOG_FATAL(log)             VIVY_LOG_WITH_LEVEL(log, Vivy::LogLevel::Critical)
 
 // Declare a sink, with the utility function/method `flushLogSink`. This is
 // intended to be used in an object to not polluate namespaces.
@@ -36,10 +36,15 @@
     LogMessage logInfo() const noexcept { return VIVY_LOG_INFO(logger); }    \
     LogMessage logDebug() const noexcept { return VIVY_LOG_DEBUG(logger); }
 
+#define VIVY_LOG_CTOR(classname) logDebug() << #classname "::CTOR "
+#define VIVY_LOG_DTOR(classname) logDebug() << #classname "::DTOR "
+
+// Install logger, use the global LogSink
+#define VIVY_APP_LOGGABLE_OBJECT(name, logger) \
+    VIVY_LOGGABLE_OBJECT(vivyApp->getLogSink(), name, logger)
+
 namespace Vivy
 {
-class VivyApplication;
-class VivyCli;
 class LogSinkDispatcher;
 class LogSink;
 class Logger;
@@ -156,6 +161,7 @@ public:
     LogMessage &operator<<(const std::string &) noexcept;
     LogMessage &operator<<(const QString &) noexcept;
     LogMessage &operator<<(const char *) noexcept;
+    LogMessage &operator<<(const char) noexcept;
     LogMessage &operator<<(const int) noexcept;
     LogMessage &operator<<(const long) noexcept;
 };
diff --git a/src/Lib/Utils.hh b/src/Lib/Utils.hh
index 6346183b..22cbce60 100644
--- a/src/Lib/Utils.hh
+++ b/src/Lib/Utils.hh
@@ -251,16 +251,4 @@ enum class VideoDocumentType : quint64 {
 
 // Ass document types
 enum class AssDocumentType : quint64 { ASS = Utils::toUnderlying(Utils::DocumentType::ASS) };
-
-}
-
-class QMenu;
-class QAction;
-class QTabWidget;
-class QGraphicsPixmapItem;
-
-namespace Vivy
-{
-class VivyApplication;
-class MainWindow;
 }
diff --git a/src/VivyApplication.hh b/src/VivyApplication.hh
index ca1254ed..326fe9fc 100644
--- a/src/VivyApplication.hh
+++ b/src/VivyApplication.hh
@@ -4,10 +4,10 @@
 #error "This is a C++ header"
 #endif
 
-#define vivyApp static_cast<VivyApplication *>(QApplication::instance())
+#define vivyApp (dynamic_cast<::Vivy::VivyApplication *>(VivyApplication::instance()))
 
-#define currentVivyDocument() dynamic_cast<VivyDocument *>(vivyApp->getCurrentDocument())
-#define currentScriptDocument dynamic_cast<ScriptDocument *>(vivyApp->getCurrentDocument())
+#define currentVivyDocument() dynamic_cast<::Vivy::VivyDocument *>(vivyApp->getCurrentDocument())
+#define currentScriptDocument dynamic_cast<::Vivy::ScriptDocument *>(vivyApp->getCurrentDocument())
 
 // Only support dark theme for now
 #define VIVY_ICON_APP     ":icons/vivy.png"
@@ -43,7 +43,7 @@ class VivyApplication : public QApplication {
     Q_OBJECT
     VIVY_DCL_LOG_SINK(logSink)
     VIVY_DCL_LOG_DISPATCH(logSink, stderrLogDispathcer, StderrLogSinkDispatcher)
-    VIVY_LOGGABLE_OBJECT(logSink, std::string_view("APPLICATION"), logger)
+    VIVY_LOGGABLE_OBJECT(logSink, APPLICATION, logger)
 
 public:
     VivyDocumentStore documentStore{};
@@ -83,6 +83,8 @@ public:
 
     void setUseFakeVimEditor(bool) noexcept;
     void setTheme(Theme) noexcept;
+
+    std::shared_ptr<LogSink> getLogSink() noexcept { return logSink; }
 };
 
 }
-- 
GitLab