[frameworks/kcoreaddons] src/lib/io: KDirWatch: expose additional verbosity as envvar

Pino Toscano <[email protected]> Wed, 5 Aug 2026 03:45:45 +0000 (UTC)
Newsgroups gmane.comp.kde.cvs
Message-ID <[email protected]>
Git commit b8c67cab9c2e9711eb51a0b20574d68fb31b03de by Pino Toscano.
Committed on 02/08/2026 at 18:18.
Pushed by pino into branch 'master'.

KDirWatch: expose additional verbosity as envvar

Rather than enabling the additional logging verbosity only when used
inside two specific tests, expose the toggle as environment variable
(no value, simply setting it will do). This makes it possible to debug
a bit more the internals in any arbitrary test/application.

Also drop the automatic enablement for two tests (kservicetest in
kservice, filetypestest in kde-cli-tools): in case they need the
verbose behaviour, they can set the environment variable.

M  +23   -27   src/lib/io/kdirwatch.cpp
M  +1    -0    src/lib/io/kdirwatch_p.h

https://invent.kde.org/frameworks/kcoreaddons/-/commit/b8c67cab9c2e9711eb51a0b20574d68fb31b03de

diff --git a/src/lib/io/kdirwatch.cpp b/src/lib/io/kdirwatch.cpp
index 8968a0a4..2e68bec7 100644
--- a/src/lib/io/kdirwatch.cpp
+++ b/src/lib/io/kdirwatch.cpp
@@ -78,9 +78,6 @@ Q_DECLARE_LOGGING_CATEGORY(KDIRWATCH)
 // logging category for this framework, default: log stuff >= warning
 Q_LOGGING_CATEGORY(KDIRWATCH, "kf.coreaddons.kdirwatch", QtWarningMsg)
 
-// set this to true for much more verbose debug output
-static bool s_verboseDebug = false;
-
 static QThreadStorage<KDirWatchPrivate *> dwp_self;
 static KDirWatchPrivate *createPrivate()
 {
@@ -130,6 +127,7 @@ static const char s_envNfsPoll[] = "KDIRWATCH_NFSPOLLINTERVAL";
 static const char s_envPoll[] = "KDIRWATCH_POLLINTERVAL";
 static const char s_envMethod[] = "KDIRWATCH_METHOD";
 static const char s_envNfsMethod[] = "KDIRWATCH_NFSMETHOD";
+static const char s_envVerboseDebug[] = "KDIRWATCH_VERBOSE";
 
 //
 // Class KDirWatchPrivate (singleton)
@@ -174,10 +172,8 @@ KDirWatchPrivate::KDirWatchPrivate()
 #endif
     _isStopped(false)
 {
-    // Debug unittest on CI
-    if (qAppName() == QLatin1String("kservicetest") || qAppName() == QLatin1String("filetypestest")) {
-        s_verboseDebug = true;
-    }
+    verboseDebug = qEnvironmentVariableIsSet(s_envVerboseDebug);
+
     m_statRescanTimer.setObjectName(QStringLiteral("KDirWatchPrivate::timer"));
     connect(&m_statRescanTimer, &QTimer::timeout, this, &KDirWatchPrivate::slotRescan);
 
@@ -601,7 +597,7 @@ QDebug operator<<(QDebug debug, const KDirWatchPrivate &dwp)
                 debug << "    dependent entries:";
                 for (KDirWatchPrivate::Entry *d : e.m_entries) {
                     debug << "      " << d << d->path << (d->m_status == KDirWatchPrivate::NonExistent ? "NonExistent" : "EXISTS this is an ERROR!");
-                    if (s_verboseDebug) {
+                    if (dwp.verboseDebug) {
                         Q_ASSERT(d->m_status == KDirWatchPrivate::NonExistent); // it doesn't belong here otherwise
                     }
                 }
@@ -698,7 +694,7 @@ bool KDirWatchPrivate::useINotify(Entry *e)
 
     if ((e->wd = inotify_add_watch(m_inotify_fd, QFile::encodeName(e->path).data(), mask)) != -1) {
         m_inotify_wd_to_entry.insert(e->wd, e);
-        if (s_verboseDebug) {
+        if (verboseDebug) {
             qCDebug(KDIRWATCH) << "inotify successfully used for monitoring" << e->path << "wd=" << e->wd;
         }
         return true;
@@ -801,12 +797,12 @@ void KDirWatchPrivate::addEntry(KDirWatch *instance, const QString &_path, Entry
         Entry &entry = it.value();
         if (sub_entry) {
             entry.m_entries.append(sub_entry);
-            if (s_verboseDebug) {
+            if (verboseDebug) {
                 qCDebug(KDIRWATCH) << "Added already watched Entry" << path << "(for" << sub_entry->path << ")";
             }
         } else {
             entry.addClient(instance, watchModes);
-            if (s_verboseDebug) {
+            if (verboseDebug) {
                 qCDebug(KDIRWATCH) << "Added already watched Entry" << path << "(now" << entry.clientCount() << "clients)"
                                    << QStringLiteral("[%1]").arg(instance->objectName());
             }
@@ -874,7 +870,7 @@ void KDirWatchPrivate::addEntry(KDirWatch *instance, const QString &_path, Entry
         e->addClient(instance, watchModes);
     }
 
-    if (s_verboseDebug) {
+    if (verboseDebug) {
         qCDebug(KDIRWATCH).nospace() << "Added " << (e->isDir ? "Dir " : "File ") << path << (e->m_status == NonExistent ? " NotExisting" : "") << " for "
                                      << (sub_entry ? sub_entry->path : QString()) << " [" << (instance ? instance->objectName() : QString()) << "]";
     }
@@ -996,14 +992,14 @@ void KDirWatchPrivate::removeWatch(Entry *e)
     if (e->m_mode == INotifyMode) {
         m_inotify_wd_to_entry.remove(e->wd);
         (void)inotify_rm_watch(m_inotify_fd, e->wd);
-        if (s_verboseDebug) {
+        if (verboseDebug) {
             qCDebug(KDIRWATCH).nospace() << "Cancelled INotify (fd " << m_inotify_fd << ", " << e->wd << ") for " << e->path;
         }
     }
 #endif
 #if HAVE_QFILESYSTEMWATCHER
     if (e->m_mode == QFSWatchMode && fsWatcher) {
-        if (s_verboseDebug) {
+        if (verboseDebug) {
             qCDebug(KDIRWATCH) << "fsWatcher->removePath" << e->path;
         }
         fsWatcher->removePath(e->path);
@@ -1056,7 +1052,7 @@ void KDirWatchPrivate::removeEntry(KDirWatch *instance, Entry *e, Entry *sub_ent
         }
     }
 
-    if (s_verboseDebug) {
+    if (verboseDebug) {
         qCDebug(KDIRWATCH).nospace() << "Removed " << (e->isDir ? "Dir " : "File ") << e->path << " for " << (sub_entry ? sub_entry->path : QString()) << " ["
                                      << (instance ? instance->objectName() : QString()) << "]";
     }
@@ -1160,7 +1156,7 @@ bool KDirWatchPrivate::restartEntryScan(KDirWatch *instance, Entry *e, bool noti
                 // we get the latest change of any kind, on any platform.
                 e->m_ctime = qMax(stat_buf.st_ctime, stat_buf.st_mtime);
                 e->m_status = Normal;
-                if (s_verboseDebug) {
+                if (verboseDebug) {
                     qCDebug(KDIRWATCH) << "Setting status to Normal for" << e << e->path;
                 }
                 e->m_nlink = stat_buf.st_nlink;
@@ -1172,7 +1168,7 @@ bool KDirWatchPrivate::restartEntryScan(KDirWatch *instance, Entry *e, bool noti
                 e->m_ctime = invalid_ctime;
                 e->m_status = NonExistent;
                 e->m_nlink = 0;
-                if (s_verboseDebug) {
+                if (verboseDebug) {
                     qCDebug(KDIRWATCH) << "Setting status to NonExistent for" << e << e->path;
                 }
             }
@@ -1258,7 +1254,7 @@ int KDirWatchPrivate::scanEntry(Entry *e)
             e->m_ctime = qMax(stat_buf.st_ctime, stat_buf.st_mtime);
             e->m_status = Normal;
             e->m_ino = stat_buf.st_ino;
-            if (s_verboseDebug) {
+            if (verboseDebug) {
                 qCDebug(KDIRWATCH) << "Setting status to Normal for just created" << e << e->path;
             }
             // We need to make sure the entry isn't listed in its parent's subentries... (#222974, testMoveTo)
@@ -1268,7 +1264,7 @@ int KDirWatchPrivate::scanEntry(Entry *e)
         }
 
 #if 1 // for debugging the if() below
-        if (s_verboseDebug) {
+        if (verboseDebug) {
             struct tm *tmp = localtime(&e->m_ctime);
             char outstr[200];
             strftime(outstr, sizeof(outstr), "%H:%M:%S", tmp);
@@ -1338,7 +1334,7 @@ void KDirWatchPrivate::emitEvent(Entry *e, int event, const QString &fileName)
         }
     }
 
-    if (s_verboseDebug) {
+    if (verboseDebug) {
         qCDebug(KDIRWATCH) << event << path << e->m_clients.size() << "clients";
     }
 
@@ -1410,7 +1406,7 @@ void KDirWatchPrivate::slotRemoveDelayed()
  */
 void KDirWatchPrivate::slotRescan()
 {
-    if (s_verboseDebug) {
+    if (verboseDebug) {
         qCDebug(KDIRWATCH);
     }
 
@@ -1460,7 +1456,7 @@ void KDirWatchPrivate::slotRescan()
         }
 
         const int ev = scanEntry(entry);
-        if (s_verboseDebug) {
+        if (verboseDebug) {
             qCDebug(KDIRWATCH) << "scanEntry for" << entry->path << "says" << ev;
         }
 
@@ -1468,7 +1464,7 @@ void KDirWatchPrivate::slotRescan()
 #if HAVE_SYS_INOTIFY_H
         case INotifyMode:
             if (ev == Deleted) {
-                if (s_verboseDebug) {
+                if (verboseDebug) {
                     qCDebug(KDIRWATCH) << "scanEntry says" << entry->path << "was deleted";
                 }
 
@@ -1477,7 +1473,7 @@ void KDirWatchPrivate::slotRescan()
                     addEntry(nullptr, entry->parentDirectory(), entry, true);
                 }
             } else if (ev == Created) {
-                if (s_verboseDebug) {
+                if (verboseDebug) {
                     qCDebug(KDIRWATCH) << "scanEntry says" << entry->path << "was created. wd=" << entry->wd;
                 }
                 if (entry->wd < 0) {
@@ -1506,7 +1502,7 @@ void KDirWatchPrivate::slotRescan()
             QStringList pendingFileChanges = entry->m_pendingFileChanges;
             pendingFileChanges.removeDuplicates();
             for (const QString &changedFilename : std::as_const(pendingFileChanges)) {
-                if (s_verboseDebug) {
+                if (verboseDebug) {
                     qCDebug(KDIRWATCH) << "processing pending file change for" << changedFilename;
                 }
                 emitEvent(entry, Changed, changedFilename);
@@ -1610,7 +1606,7 @@ QString KDirWatchPrivate::inotifyEventName(const inotify_event *event) const
 // Slot for QFileSystemWatcher
 void KDirWatchPrivate::fswEventReceived(const QString &path)
 {
-    if (s_verboseDebug) {
+    if (verboseDebug) {
         qCDebug(KDIRWATCH) << path;
     }
 
@@ -1619,7 +1615,7 @@ void KDirWatchPrivate::fswEventReceived(const QString &path)
         Entry *entry = &it.value();
         entry->dirty = true;
         const int ev = scanEntry(entry);
-        if (s_verboseDebug) {
+        if (verboseDebug) {
             qCDebug(KDIRWATCH) << "scanEntry for" << entry->path << "says" << ev;
         }
         if (ev != NoChange) {
diff --git a/src/lib/io/kdirwatch_p.h b/src/lib/io/kdirwatch_p.h
index 63f1e9e6..b0bd4105 100644
--- a/src/lib/io/kdirwatch_p.h
+++ b/src/lib/io/kdirwatch_p.h
@@ -235,6 +235,7 @@ public:
 #endif
 
     bool _isStopped;
+    bool verboseDebug;
 
 private:
     // Public objects that reference this thread-local private instance.