From f4a50481a887de1984c1996c44d53ba357ad8886 Mon Sep 17 00:00:00 2001
From: Kubat <mael.martin31@gmail.com>
Date: Wed, 1 Sep 2021 23:14:28 +0200
Subject: [PATCH] LOG: First level of std/Lib uses the new LOG API

---
 PreCompiledHeaders.cmake        |  1 +
 src/Lib/AbstractDocument.hh     |  7 +++++--
 src/Lib/AbstractMediaContext.hh | 19 +++++++++++--------
 src/Lib/Audio.cc                | 10 +++++-----
 src/Lib/CRTPStore.hh            |  6 +++++-
 src/Lib/Log.cc                  | 30 ++++++++++++++++++++++++++++++
 src/Lib/Log.hh                  | 13 +++++++++++++
 src/UI/MainWindow.cc            | 10 ++++++----
 src/UI/VivyDocumentView.cc      |  3 ++-
 src/VivyApplication.hh          |  9 +++++----
 10 files changed, 83 insertions(+), 25 deletions(-)

diff --git a/PreCompiledHeaders.cmake b/PreCompiledHeaders.cmake
index dce8f03b..f09e72ab 100644
--- a/PreCompiledHeaders.cmake
+++ b/PreCompiledHeaders.cmake
@@ -23,6 +23,7 @@ set(STL_INC
     <climits>
     <ctype.h>
     <mutex>
+    <iomanip>
 )
 
 set(EXT_INC PRIVATE
diff --git a/src/Lib/AbstractDocument.hh b/src/Lib/AbstractDocument.hh
index dfec447f..d7761f44 100644
--- a/src/Lib/AbstractDocument.hh
+++ b/src/Lib/AbstractDocument.hh
@@ -4,14 +4,17 @@
 #error "This is a C++ header"
 #endif
 
+#include "../VivyApplication.hh"
 #include "Utils.hh"
 #include "Uuid.hh"
+#include "Log.hh"
 
 namespace Vivy
 {
 class AbstractDocument : public QObject {
     Q_OBJECT
     VIVY_UNMOVABLE_OBJECT(AbstractDocument)
+    VIVY_APP_LOGGABLE_OBJECT(AbstractDocument, logger)
 
     void copyOrRenameWith(const QFileInfo &newFile, auto action, auto success)
     {
@@ -21,7 +24,7 @@ class AbstractDocument : public QObject {
         QDir dirOp;
         const QString newAbsDirPath = newFile.dir().absolutePath();
         if (!dirOp.exists(newAbsDirPath)) {
-            qInfo() << "Create folder " << newAbsDirPath;
+            logInfo() << "Create folder " << VIVY_LOG_QUOTED(newAbsDirPath);
             dirOp.mkpath(newAbsDirPath);
         }
 
@@ -30,7 +33,7 @@ class AbstractDocument : public QObject {
         }
 
         if (newFile.exists()) {
-            qWarning() << "Deleting the already existing" << newFile;
+            logWarning() << "Deleting the already existing file " << VIVY_LOG_QUOTED(newFile);
             if (!dirOp.remove(newFile.absoluteFilePath()))
                 throw std::runtime_error("Failed to remove " +
                                          newFile.absoluteFilePath().toStdString());
diff --git a/src/Lib/AbstractMediaContext.hh b/src/Lib/AbstractMediaContext.hh
index f4a09ed6..a15609de 100644
--- a/src/Lib/AbstractMediaContext.hh
+++ b/src/Lib/AbstractMediaContext.hh
@@ -10,10 +10,11 @@ extern "C" {
 #include <libavformat/avformat.h>
 #include <libswresample/swresample.h>
 #include <libavcodec/avfft.h>
-#include <memory.h>
 }
 
+#include "../VivyApplication.hh"
 #include "Utils.hh"
+#include "Log.hh"
 
 namespace Vivy
 {
@@ -53,6 +54,7 @@ public:
     static inline constexpr AVMediaType avMediaType = static_cast<AVMediaType>(AVMEDIA_TYPE);
 
 protected:
+    VIVY_APP_LOGGABLE_OBJECT(AbstractMediaStream, logger)
     using Super = AbstractMediaStream<AVMEDIA_TYPE>;
 
 protected:
@@ -78,10 +80,10 @@ protected:
         if (avcodec_open2(codecContext.get(), codec, nullptr) < 0)
             throw std::runtime_error("failed to open audio decoder for a stream");
 
-        qDebug() << "[Stream] Codec" << codec->name << "id:" << codecId;
-        qDebug() << "[Stream] sample rate:" << codecParams->sample_rate;
-        qDebug() << "[Stream] bit rate:   " << codecParams->bit_rate;
-        qDebug() << "[Stream] channels:   " << codecParams->channels;
+        VIVY_LOG_CTOR() << "Codec: " << VIVY_LOG_QUOTED(codec->name) << ", id: " << codecId;
+        VIVY_LOG_CTOR() << "Sample rate: " << codecParams->sample_rate;
+        VIVY_LOG_CTOR() << "Bit rate: " << codecParams->bit_rate;
+        VIVY_LOG_CTOR() << "Channels: " << codecParams->channels;
     }
 
 public:
@@ -121,6 +123,7 @@ public:
     using StreamWeakPtr                             = std::weak_ptr<Stream>;
 
 protected:
+    VIVY_APP_LOGGABLE_OBJECT(AbstractMediaContext, logger)
     using Super = AbstractMediaContext<Stream>;
 
 public:
@@ -160,10 +163,10 @@ public:
                                                  -1, // We don't want related streams
                                                  nullptr, 0);
         if (defaultStreamIndex < 0)
-            qCritical() << "Could not find the best stream";
+            logError() << "Could not find the best stream";
 
-        qDebug() << "Opened context for" << path << "with duration" << formatPtr->duration
-                 << "and default stream index" << defaultStreamIndex;
+        logDebug() << "Opened context for " << VIVY_LOG_QUOTED(path) << " with duration "
+                   << formatPtr->duration << " and default stream index " << defaultStreamIndex;
     }
 
     virtual ~AbstractMediaContext() {}
diff --git a/src/Lib/Audio.cc b/src/Lib/Audio.cc
index 218763c2..fdf79c0f 100644
--- a/src/Lib/Audio.cc
+++ b/src/Lib/Audio.cc
@@ -46,10 +46,10 @@ AudioStream::AudioStream(AVCodec *streamCodec, AVFormatContext *formatPtr, AVStr
 AudioStream::~AudioStream() noexcept
 {
     if (dataPtr) {
-        qDebug() << "Free data ptr";
+        VIVY_LOG_DTOR() << "Free data ptr for audio stream";
         free(dataPtr);
     }
-    qDebug() << "Delete stream object";
+    VIVY_LOG_DTOR() << "Delete audio stream object";
 }
 
 QJsonObject
@@ -68,7 +68,7 @@ AudioStream::decodeData()
 {
     if (isDecoded())
         throw std::logic_error("audio stream is already resampled");
-    qDebug() << "Launch decoding of stream" << streamIndexInContext;
+    logDebug() << "Launch decoding of stream " << streamIndexInContext;
 
     AVPacket packet;
     av_init_packet(&packet);
@@ -137,8 +137,8 @@ AudioStream::decodeData()
         av_packet_unref(&packet);
     }
 
-    qDebug() << "Decoding data finished for stream" << streamIndexInContext
-             << "dataPtr =" << dataPtr << "with dataSize =" << dataSize;
+    logDebug() << "Decoding data finished for stream " << streamIndexInContext << " with dataPtr "
+               << dataPtr << " and dataSize " << dataSize;
 }
 
 // Delete decoded data, clean up thing
diff --git a/src/Lib/CRTPStore.hh b/src/Lib/CRTPStore.hh
index 8324d9c6..2aa6da2c 100644
--- a/src/Lib/CRTPStore.hh
+++ b/src/Lib/CRTPStore.hh
@@ -4,6 +4,8 @@
 #error "This is a C++ header"
 #endif
 
+#include "../VivyApplication.hh"
+#include "Log.hh"
 #include "Utils.hh"
 #include "Uuid.hh"
 
@@ -21,6 +23,8 @@ template <class Store, class Document> class CRTPStore {
     VIVY_UNMOVABLE_OBJECT(CRTPStore)
 
 protected:
+    VIVY_APP_LOGGABLE_OBJECT(CRTPStore, logger)
+
     std::map<Uuid, std::shared_ptr<Document>> documents{};
     uint newDocumentNumber{ 1 };
     static inline const QString newDocumentBaseName = QStringLiteral("Untitled ");
@@ -56,7 +60,7 @@ public:
         auto *self = static_cast<Store *>(this);
 
         if (!self->documents.contains(uuid)) {
-            qCritical() << "Couldn't find the document " << uuid;
+            logError() << "Couldn't find the document " << uuid;
             throw std::runtime_error("Can't find the document");
         }
 
diff --git a/src/Lib/Log.cc b/src/Lib/Log.cc
index f26e77b3..b8d71f45 100644
--- a/src/Lib/Log.cc
+++ b/src/Lib/Log.cc
@@ -150,6 +150,36 @@ LogMessage::operator<<(const QString &msg) noexcept
     return (*this << msg.toStdString());
 }
 
+LogMessage &
+LogMessage::operator<<(const QFileInfo &fileInfo) noexcept
+{
+    return (*this << "QFileInfo{ " << fileInfo.absoluteFilePath() << "}");
+}
+
+LogMessage &
+LogMessage::operator<<(const double *ptr) noexcept
+{
+    return (*this << "Pointer{ double, " << pointerToString<double>(ptr) << " }");
+}
+
+LogMessage &
+LogMessage::operator<<(const unsigned char c) noexcept
+{
+    return (*this << static_cast<char>(c));
+}
+
+LogMessage &
+LogMessage::operator<<(const unsigned int i) noexcept
+{
+    return (*this << static_cast<unsigned long>(i));
+}
+
+LogMessage &
+LogMessage::operator<<(const unsigned long i) noexcept
+{
+    return (*this << std::to_string(i));
+}
+
 LogMessage &
 LogMessage::operator<<(const int i) noexcept
 {
diff --git a/src/Lib/Log.hh b/src/Lib/Log.hh
index 042df469..b559571d 100644
--- a/src/Lib/Log.hh
+++ b/src/Lib/Log.hh
@@ -169,6 +169,14 @@ private:
     std::size_t indexInArray{ 0 };
     const Logger *parentLogger{ nullptr };
 
+    static std::string pointerToString(const auto *ptr) noexcept
+    {
+        std::stringstream stream;
+        stream << "0x" << std::setfill('0') << std::setw(sizeof(std::intptr_t) * 2) << std::hex
+               << reinterpret_cast<const std::intptr_t>(ptr);
+        return stream.str();
+    }
+
 public:
     VIVY_DISABLE_COPY_CTOR(LogMessage)
     VIVY_DISABLE_ASSIGN_OPERATORS(LogMessage)
@@ -186,10 +194,15 @@ public:
     LogMessage &operator<<(const std::string &) noexcept;
     LogMessage &operator<<(const std::string_view) noexcept;
     LogMessage &operator<<(const QString &) noexcept;
+    LogMessage &operator<<(const QFileInfo &) noexcept;
     LogMessage &operator<<(const char *) noexcept;
+    LogMessage &operator<<(const double *) noexcept;
     LogMessage &operator<<(const char) noexcept;
     LogMessage &operator<<(const int) noexcept;
     LogMessage &operator<<(const long) noexcept;
+    LogMessage &operator<<(const unsigned char) noexcept;
+    LogMessage &operator<<(const unsigned int) noexcept;
+    LogMessage &operator<<(const unsigned long) noexcept;
 };
 
 // A logger class, a client to LogSink. Will generate and send instances of
diff --git a/src/UI/MainWindow.cc b/src/UI/MainWindow.cc
index d6384702..0130076f 100644
--- a/src/UI/MainWindow.cc
+++ b/src/UI/MainWindow.cc
@@ -1,3 +1,5 @@
+#include "../Lib/Document/VivyDocumentStore.hh"
+#include "../Lib/Script/ScriptStore.hh"
 #include "../Lib/Utils.hh"
 #include "../VivyApplication.hh"
 #include "MainWindow.hh"
@@ -312,7 +314,7 @@ MainWindow::newDocument() noexcept
 {
     try {
         addTab(new VivyDocumentView(
-            vivyApp->documentStore.newDocument(VivyDocument::UntouchedByDefault), documents));
+            vivyApp->documentStore->newDocument(VivyDocument::UntouchedByDefault), documents));
     } catch (const std::runtime_error &e) {
         logError() << "Failed to create a new empty document: " << e.what();
     }
@@ -339,11 +341,11 @@ MainWindow::openDocument() noexcept
     // Handle the different types here
     try {
         if (fileType == Utils::DocumentType::Vivy)
-            addTab(new VivyDocumentView(vivyApp->documentStore.loadDocument(filename), documents));
+            addTab(new VivyDocumentView(vivyApp->documentStore->loadDocument(filename), documents));
 
         else if (fileType == Utils::DocumentType::VivyScript) {
-            auto scriptDocument = vivyApp->scriptStore.loadDocument(filename);
-            auto errorTuple     = vivyApp->scriptStore.executeScript(scriptDocument->getUuid());
+            auto scriptDocument = vivyApp->scriptStore->loadDocument(filename);
+            auto errorTuple     = vivyApp->scriptStore->executeScript(scriptDocument->getUuid());
             ScriptDocumentView *newView = new ScriptDocumentView(scriptDocument, documents);
 
             if (errorTuple.has_value()) {
diff --git a/src/UI/VivyDocumentView.cc b/src/UI/VivyDocumentView.cc
index fff3ef46..86e155af 100644
--- a/src/UI/VivyDocumentView.cc
+++ b/src/UI/VivyDocumentView.cc
@@ -6,6 +6,7 @@
 #include "DocumentViews/AssLinesModel.hh"
 #include "../VivyApplication.hh"
 #include "../Lib/Document/VivyDocument.hh"
+#include "../Lib/Document/VivyDocumentStore.hh"
 
 using namespace Vivy;
 
@@ -149,7 +150,7 @@ void
 VivyDocumentView::closeDocument() noexcept
 {
     logDebug() << "Closing document " << document->getUuid();
-    vivyApp->documentStore.closeDocument(document->getUuid());
+    vivyApp->documentStore->closeDocument(document->getUuid());
     allowToCloseAllDocks();
 }
 
diff --git a/src/VivyApplication.hh b/src/VivyApplication.hh
index 85e715d8..4a9985a3 100644
--- a/src/VivyApplication.hh
+++ b/src/VivyApplication.hh
@@ -29,13 +29,14 @@
 #define VIVY_MACOS
 #endif
 
-#include "Lib/Script/ScriptStore.hh"
-#include "Lib/Document/VivyDocumentStore.hh"
 #include "Lib/Log.hh"
 
 namespace Vivy
 {
 class MainWindow;
+class ScriptStore;
+class VivyDocumentStore;
+class AbstractDocument;
 
 // Vivy application class
 class VivyApplication : public QApplication {
@@ -45,8 +46,8 @@ class VivyApplication : public QApplication {
     VIVY_LOGGABLE_OBJECT(logSink, APPLICATION, logger)
 
 public:
-    VivyDocumentStore documentStore{};
-    ScriptStore scriptStore{};
+    std::shared_ptr<VivyDocumentStore> documentStore{};
+    std::shared_ptr<ScriptStore> scriptStore{};
 
     enum class Font {
         Monospace,
-- 
GitLab