From cb2d019f4b081ebef421057be79fae8641a6c62d Mon Sep 17 00:00:00 2001
From: Kubat <mael.martin31@gmail.com>
Date: Thu, 9 Sep 2021 16:23:16 +0200
Subject: [PATCH] LOG: Continue cleanup of qDebugs

---
 src/Lib/Utils.cc                 |  14 ----
 src/Lib/Utils.hh                 |   4 --
 src/UI/FakeVim/FakeVimHandler.cc | 109 +++++--------------------------
 src/UI/FakeVim/FakeVimHandler.hh |  10 +--
 4 files changed, 20 insertions(+), 117 deletions(-)

diff --git a/src/Lib/Utils.cc b/src/Lib/Utils.cc
index 34365f77..9679378c 100644
--- a/src/Lib/Utils.cc
+++ b/src/Lib/Utils.cc
@@ -297,17 +297,3 @@ Utils::writeAssertLocation(const char *msg)
 {
     qFatal("ASSERT: %s", msg);
 }
-
-QDebug &
-operator<<(QDebug &stream, const std::string str) noexcept
-{
-    stream << str.c_str();
-    return stream;
-}
-
-QDebug &
-operator<<(QDebug &stream, const std::string_view strv) noexcept
-{
-    stream << std::string(strv);
-    return stream;
-}
diff --git a/src/Lib/Utils.hh b/src/Lib/Utils.hh
index 8e954575..39a6bc79 100644
--- a/src/Lib/Utils.hh
+++ b/src/Lib/Utils.hh
@@ -59,10 +59,6 @@ using chrono::duration_cast;
 // QStringLiteral but for regexes
 #define QRegExpLiteral(str) QRegExp(QStringLiteral(str))
 
-// Log things for qDebug()
-QDebug &operator<<(QDebug &, const std::string) noexcept;
-QDebug &operator<<(QDebug &, const std::string_view) noexcept;
-
 namespace Vivy
 {
 // Concept for classes that can be viewd inside a property view
diff --git a/src/UI/FakeVim/FakeVimHandler.cc b/src/UI/FakeVim/FakeVimHandler.cc
index bac3cf83..2cede1aa 100644
--- a/src/UI/FakeVim/FakeVimHandler.cc
+++ b/src/UI/FakeVim/FakeVimHandler.cc
@@ -35,14 +35,14 @@
 
 //#define DEBUG_KEY  1
 #if defined(DEBUG_KEY) && DEBUG_KEY
-#define KEY_DEBUG(s) qDebug() << s
+#define KEY_DEBUG(s) logDebug() << s
 #else
 #define KEY_DEBUG(s)
 #endif
 
 //#define DEBUG_UNDO  1
 #if defined(DEBUG_UNDO) && DEBUG_UNDO
-#define UNDO_DEBUG(s) qDebug() << "REV" << revision() << s
+#define UNDO_DEBUG(s) logDebug() << "REV" << revision() << s
 #else
 #define UNDO_DEBUG(s)
 #endif
@@ -196,12 +196,6 @@ struct CursorPosition {
     int column = -1; // Position on line.
 };
 
-VIVY_UNUSED static QDebug
-operator<<(QDebug ts, const CursorPosition &pos)
-{
-    return ts << "(line: " << pos.line << ", column: " << pos.column << ")";
-}
-
 // vi style configuration
 
 class Mark {
@@ -281,12 +275,6 @@ struct Column {
     int logical;  // Column on screen.
 };
 
-VIVY_UNUSED static QDebug
-operator<<(QDebug ts, const Column &col)
-{
-    return ts << "(p: " << col.physical << ", l: " << col.logical << ")";
-}
-
 struct Register {
     Register() = default;
     Register(const QString &c)
@@ -302,12 +290,6 @@ struct Register {
     RangeMode rangemode = RangeCharMode;
 };
 
-VIVY_UNUSED static QDebug
-operator<<(QDebug ts, const Register &reg)
-{
-    return ts << reg.contents;
-}
-
 struct SearchData {
     QString needle;
     bool forward          = true;
@@ -950,12 +932,6 @@ Range::isValid() const
     return beginPos >= 0 && endPos >= 0;
 }
 
-static QDebug
-operator<<(QDebug ts, const Range &range)
-{
-    return ts << '[' << range.beginPos << ',' << range.endPos << ']';
-}
-
 ExCommand::ExCommand(const QString &c, const QString &a, const Range &r)
     : cmd(c)
     , args(a)
@@ -969,20 +945,6 @@ ExCommand::matches(const QString &min, const QString &full) const
     return cmd.startsWith(min) && full.startsWith(cmd);
 }
 
-VIVY_UNUSED static QDebug
-operator<<(QDebug ts, const ExCommand &cmd)
-{
-    return ts << cmd.cmd << ' ' << cmd.args << ' ' << cmd.range;
-}
-
-VIVY_UNUSED static QDebug
-operator<<(QDebug ts, const QList<QTextEdit::ExtraSelection> &sels)
-{
-    foreach(const QTextEdit::ExtraSelection &sel, sels) ts << "SEL: " << sel.cursor.anchor()
-                                                           << sel.cursor.position();
-    return ts;
-}
-
 static QString
 quoteUnprintable(const QString &ba)
 {
@@ -1004,7 +966,6 @@ static bool
 startsWithWhitespace(const QString &str, int col)
 {
     if (col > str.size()) {
-        qWarning("Wrong column");
         return false;
     }
     for (int i = 0; i < col; ++i) {
@@ -1200,11 +1161,6 @@ public:
         return key;
     }
 
-    QDebug dump(QDebug ts) const
-    {
-        return ts << m_key << '-' << m_modifiers << '-' << quoteUnprintable(m_text);
-    }
-
 private:
     int m_key                         = 0;
     int m_xkey                        = 0;
@@ -1289,12 +1245,6 @@ dotCommandFromSubMode(SubMode submode)
     return QString();
 }
 
-VIVY_UNUSED static QDebug
-operator<<(QDebug ts, const Input &input)
-{
-    return input.dump(ts);
-}
-
 class Inputs : public QVector<Input> {
 public:
     Inputs() = default;
@@ -1734,6 +1684,8 @@ struct MappingState {
 };
 
 class FakeVimHandler::Private : public QObject {
+    VIVY_APP_LOGGABLE_OBJECT(FakeVimHandler::Private, logger)
+
 public:
     Private(FakeVimHandler *parent, QWidget *widget);
     ~Private() override;
@@ -1963,7 +1915,7 @@ public:
     void movePageUp(int count = 1) { movePageDown(-count); }
     void dump(const char *msg) const
     {
-        qDebug() << msg << "POS: " << anchor() << position() << "VISUAL: " << g.visualMode;
+        logDebug() << msg << "POS: " << anchor() << position() << "VISUAL: " << g.visualMode;
     }
     void moveRight(int n = 1)
     {
@@ -2519,8 +2471,7 @@ void
 FakeVimHandler::Private::enterFakeVim()
 {
     if (m_inFakeVim) {
-        qWarning("enterFakeVim() shouldn't be called recursively!");
-        return;
+        qFatal("enterFakeVim() shouldn't be called recursively!");
     }
 
     if (!m_buffer->currentHandler)
@@ -2541,8 +2492,7 @@ void
 FakeVimHandler::Private::leaveFakeVim(bool needUpdate)
 {
     if (!m_inFakeVim) {
-        qWarning("enterFakeVim() not called before leaveFakeVim()!");
-        return;
+        qFatal("enterFakeVim() not called before leaveFakeVim()!");
     }
 
     // The command might have destroyed the editor.
@@ -2627,10 +2577,6 @@ FakeVimHandler::Private::handleEvent(QKeyEvent *ev)
     if (g.passing) {
         passShortcuts(false);
         KEY_DEBUG("PASSING PLAIN KEY..." << ev->key() << ev->text());
-        //if (input.is(',')) { // use ',,' to leave, too.
-        //    qDebug() << "FINISHED...";
-        //    return EventHandled;
-        //}
         KEY_DEBUG("   PASS TO CORE");
         return EventPassedToCore;
     }
@@ -3801,7 +3747,6 @@ FakeVimHandler::Private::updateSelection()
             selections.append(sel);
         }
     }
-    //qDebug() << "SELECTION: " << selections;
     q->selectionChanged(selections);
 }
 
@@ -3903,7 +3848,6 @@ FakeVimHandler::Private::updateMiniBuffer()
 void
 FakeVimHandler::Private::showMessage(MessageLevel level, const QString &msg)
 {
-    //qDebug() << "MSG: " << msg;
     g.currentMessage      = msg;
     g.currentMessageLevel = level;
 }
@@ -3911,7 +3855,7 @@ FakeVimHandler::Private::showMessage(MessageLevel level, const QString &msg)
 void
 FakeVimHandler::Private::notImplementedYet()
 {
-    qDebug() << "Not implemented in FakeVim";
+    logDebug() << "Not implemented in FakeVim";
     showMessage(MessageError, Tr::tr("Not implemented in FakeVim."));
 }
 
@@ -4448,9 +4392,6 @@ FakeVimHandler::Private::handleCommandMode(const Input &input)
         saveLastVisualMode();
     } else {
         leaveCurrentMode();
-        //qDebug() << "IGNORED IN COMMAND MODE: " << key << text
-        //    << " VISUAL: " << g.visualMode;
-
         // if a key which produces text was pressed, don't mark it as unhandled
         // - otherwise the text would be inserted while being in command mode
         if (input.text().isEmpty())
@@ -4493,8 +4434,6 @@ FakeVimHandler::Private::handleNoSubMode(const Input &input)
     } else if (input.is(',')) {
         passShortcuts(true);
     } else if (input.is('.')) {
-        //qDebug() << "REPEATING" << quoteUnprintable(g.dotCommand) << count()
-        //    << input;
         dotCommand.clear();
         QString savedCommand = g.dotCommand;
         g.dotCommand.clear();
@@ -5673,7 +5612,7 @@ FakeVimHandler::Private::handleExMode(const Input &input)
         handleExCommand(g.commandBuffer.contents());
         g.commandBuffer.clear();
     } else if (!g.commandBuffer.handleInput(input)) {
-        qDebug() << "IGNORED IN EX-MODE: " << input.key() << input.text();
+        logDebug() << "IGNORED IN EX-MODE: " << input.key() << input.text();
         return EventUnhandled;
     }
 
@@ -5720,7 +5659,6 @@ FakeVimHandler::Private::handleSearchSubSubMode(const Input &input)
     } else if (input.isKey(Key_Tab)) {
         g.searchBuffer.insertChar(QChar(9));
     } else if (!g.searchBuffer.handleInput(input)) {
-        //qDebug() << "IGNORED IN SEARCH MODE: " << input.key() << input.text();
         return EventUnhandled;
     }
 
@@ -5738,7 +5676,6 @@ FakeVimHandler::Private::handleSearchSubSubMode(const Input &input)
 int
 FakeVimHandler::Private::parseLineAddress(QString *cmd)
 {
-    //qDebug() << "CMD: " << cmd;
     if (cmd->isEmpty())
         return -1;
 
@@ -6185,7 +6122,6 @@ FakeVimHandler::Private::handleExMapCommand(const ExCommand &cmd0) // :map
     }
 
     Inputs key(lhs);
-    //qDebug() << "MAPPING: " << modes << lhs << rhs;
     switch (type) {
     case Unmap: foreach(char c, modes) MappingsIterator(&g.mappings, c, key).remove(); break;
     case Map: Q_FALLTHROUGH();
@@ -6305,7 +6241,6 @@ FakeVimHandler::Private::handleExNormalCommand(const ExCommand &cmd)
     // :norm[al]
     if (!cmd.matches("norm", "normal"))
         return false;
-    //qDebug() << "REPLAY NORMAL: " << quoteUnprintable(reNormal.cap(3));
     replay(cmd.args);
     return true;
 }
@@ -6467,12 +6402,8 @@ FakeVimHandler::Private::handleExWriteCommand(const ExCommand &cmd)
         beginLine = 0;
     if (endLine == -1)
         endLine = linesInDocument();
-    //qDebug() << "LINES: " << beginLine << endLine;
-    //QString prefix = cmd.args;
     const bool forced = cmd.hasBang;
-    //const bool quit = prefix.contains('q') || prefix.contains('x');
-    //const bool quitAll = quit && prefix.contains('a');
-    QString fileName = replaceTildeWithHome(cmd.args);
+    QString fileName  = replaceTildeWithHome(cmd.args);
     if (fileName.isEmpty())
         fileName = m_currentFileName;
     QFile file1(fileName);
@@ -6563,7 +6494,6 @@ FakeVimHandler::Private::handleExBangCommand(const ExCommand &cmd) // :!
         setPosition(targetPosition);
         endEditBlock();
         leaveVisualMode();
-        //qDebug() << "FILTER: " << command;
         showMessage(MessageInfo, Tr::tr("%n lines filtered.", nullptr, input.count('\n')));
     } else if (!result.isEmpty()) {
         q->extraInformationChanged(result);
@@ -6712,12 +6642,10 @@ FakeVimHandler::Private::handleExSourceCommand(const ExCommand &cmd)
         }
 
         if (line.startsWith("function")) {
-            //qDebug() << "IGNORING FUNCTION" << line;
             inFunction = true;
         } else if (inFunction && line.startsWith("endfunction")) {
             inFunction = false;
         } else if (!line.isEmpty() && !inFunction) {
-            //qDebug() << "EXECUTING: " << line;
             ExCommand cmdLocal;
             QString commandLine = QString::fromLocal8Bit(line);
             while (parseExCommand(&commandLine, &cmdLocal)) {
@@ -6755,8 +6683,6 @@ FakeVimHandler::Private::handleExCommand(const QString &line0)
         return;
     }
 
-    //qDebug() << "CMD: " << cmd;
-
     enterCommandMode(g.returnToMode);
 
     beginLargeEditBlock();
@@ -6802,7 +6728,6 @@ FakeVimHandler::Private::handleExPluginCommand(const ExCommand &cmd)
     int pos      = m_cursor.position();
     commitCursor();
     q->handleExCommandRequested(&handled, cmd);
-    //qDebug() << "HANDLER REQUEST: " << cmd.cmd << handled;
     if (handled && (m_textedit || m_plaintextedit)) {
         pullCursor();
         if (m_cursor.position() != pos)
@@ -7083,15 +7008,12 @@ void
 FakeVimHandler::Private::moveToTargetColumn()
 {
     const QTextBlock &bl = block();
-    //Column column = cursorColumn();
-    //int logical = logical
-    const int pos = lastPositionInLine(bl.blockNumber() + 1, false);
+    const int pos        = lastPositionInLine(bl.blockNumber() + 1, false);
     if (m_targetColumn == -1) {
         setPosition(pos);
         return;
     }
     const int physical = bl.position() + logicalToPhysicalColumn(m_targetColumn, bl.text());
-    //qDebug() << "CORRECTING COLUMN FROM: " << logical << "TO" << m_targetColumn;
     setPosition(qMin(pos, physical));
 }
 
@@ -7750,7 +7672,7 @@ void
 FakeVimHandler::Private::insertText(const Register &reg)
 {
     if (reg.rangemode != RangeCharMode) {
-        qWarning() << "WRONG INSERT MODE: " << reg.rangemode;
+        logWarning() << "WRONG INSERT MODE: " << reg.rangemode;
         return;
     }
     setAnchor();
@@ -8417,7 +8339,7 @@ FakeVimHandler::Private::endEditBlock()
 {
     UNDO_DEBUG("END EDIT BLOCK" << m_buffer->editBlockLevel);
     if (m_buffer->editBlockLevel <= 0) {
-        qWarning("beginEditBlock() not called before endEditBlock()!");
+        logWarning() << "beginEditBlock() not called before endEditBlock()!";
         return;
     }
     --m_buffer->editBlockLevel;
@@ -8682,7 +8604,7 @@ void
 FakeVimHandler::Private::enterInsertOrReplaceMode(Mode mode)
 {
     if (mode != InsertMode && mode != ReplaceMode) {
-        qWarning("Unexpected mode");
+        logWarning() << "Unexpected mode";
         return;
     }
     if (g.mode == mode)
@@ -8897,7 +8819,6 @@ FakeVimHandler::Private::replay(const QString &command, int repeat)
     if (repeat <= 0)
         return;
 
-    //qDebug() << "REPLAY: " << quoteUnprintable(command);
     clearCurrentMode();
     const Inputs inputs(command);
     for (int i = 0; i < repeat; ++i) {
@@ -9190,7 +9111,7 @@ FakeVimHandler::Private::changeNumberTextObject(int count)
     else
         value = num.toLongLong(&ok, base);
     if (!ok) {
-        qWarning() << "Cannot parse number:" << num << "base:" << base;
+        logWarning() << "Cannot parse number:" << num << "base:" << base;
         return false;
     }
 
diff --git a/src/UI/FakeVim/FakeVimHandler.hh b/src/UI/FakeVim/FakeVimHandler.hh
index 2839fbc7..eb8fea4e 100644
--- a/src/UI/FakeVim/FakeVimHandler.hh
+++ b/src/UI/FakeVim/FakeVimHandler.hh
@@ -1,8 +1,8 @@
 #pragma once
 
-// #include "../../VivyApplication.hh"
-// #include "../../Lib/Log.hh"
-// #include "../../Lib/Utils.hh"
+#include "../../VivyApplication.hh"
+#include "../../Lib/Log.hh"
+#include "../../Lib/Utils.hh"
 
 #define FAKEVIM_STANDALONE
 
@@ -80,8 +80,8 @@ private:
 
 class FakeVimHandler : public QObject {
     Q_OBJECT
-    // VIVY_UNMOVABLE_OBJECT(FakeVimHandler)
-    // VIVY_APP_LOGGABLE_OBJECT(FakeVimHandler, logger)
+    VIVY_UNMOVABLE_OBJECT(FakeVimHandler)
+    VIVY_APP_LOGGABLE_OBJECT(FakeVimHandler, logger)
 
 public:
     explicit FakeVimHandler(QWidget *widget, QObject *parent = nullptr);
-- 
GitLab