Browse Source

Conditional handling of debug messages

Sascha Zelzer 13 years ago
parent
commit
cd46fbef03

+ 2 - 0
Libs/PluginFramework/CMakeLists.txt

@@ -33,6 +33,8 @@ SET(KIT_SRCS
   ctkPluginFrameworkFactory.cpp
   ctkPluginFrameworkContext.cpp
   ctkPluginFrameworkContext_p.h
+  ctkPluginFrameworkDebug.cpp
+  ctkPluginFrameworkDebug_p.h
   ctkPluginFrameworkEvent.cpp
   ctkPluginFrameworkLauncher.cpp
   ctkPluginFrameworkListeners.cpp

+ 26 - 9
Libs/PluginFramework/ctkPluginFrameworkContext.cpp

@@ -35,9 +35,11 @@ int ctkPluginFrameworkContext::globalId = 1;
 //----------------------------------------------------------------------------
 ctkPluginFrameworkContext::ctkPluginFrameworkContext(
     const ctkProperties& initProps)
-      : plugins(0), services(0), systemPlugin(new ctkPluginFramework()),
-      storage(0), firstInit(true), props(initProps), initialized(false)
+  : plugins(0), listeners(this), services(0), systemPlugin(new ctkPluginFramework()),
+    storage(0), firstInit(true), props(initProps), debug(props),
+    initialized(false)
 {
+
   {
     QMutexLocker lock(&globalFwLock);
     id = globalId++;
@@ -149,15 +151,21 @@ void ctkPluginFrameworkContext::checkOurPlugin(ctkPlugin* plugin) const
 //----------------------------------------------------------------------------
 QDebug ctkPluginFrameworkContext::log() const
 {
-  QDebug dbg(qDebug());
-  dbg << "Framework instance " << getId() << ": ";
-  return dbg;
+  static QString nirvana;
+  nirvana.clear();
+  if (debug.framework)
+    return qDebug() << "Framework instance " << getId() << ": ";
+  else
+    return QDebug(&nirvana);
 }
 
 //----------------------------------------------------------------------------
 void ctkPluginFrameworkContext::resolvePlugin(ctkPluginPrivate* plugin)
 {
-  qDebug() << "resolve:" << plugin->symbolicName << "[" << plugin->id << "]";
+  if (debug.resolve)
+  {
+    qDebug() << "resolve:" << plugin->symbolicName << "[" << plugin->id << "]";
+  }
 
   // If we enter with tempResolved set, it means that we already have
   // resolved plugins. Check that it is true!
@@ -175,7 +183,10 @@ void ctkPluginFrameworkContext::resolvePlugin(ctkPluginPrivate* plugin)
 
   tempResolved.clear();
 
-  qDebug() << "resolve: Done for" << plugin->symbolicName << "[" << plugin->id << "]";
+  if (debug.resolve)
+  {
+    qDebug() << "resolve: Done for" << plugin->symbolicName << "[" << plugin->id << "]";
+  }
 }
 
 //----------------------------------------------------------------------------
@@ -183,7 +194,10 @@ void ctkPluginFrameworkContext::checkRequirePlugin(ctkPluginPrivate *plugin)
 {
   if (!plugin->require.isEmpty())
   {
-    qDebug() << "checkRequirePlugin: check requiring plugin" << plugin->id;
+    if (debug.resolve)
+    {
+      qDebug() << "checkRequirePlugin: check requiring plugin" << plugin->id;
+    }
 
     QListIterator<ctkRequirePlugin*> i(plugin->require);
     while (i.hasNext())
@@ -214,7 +228,10 @@ void ctkPluginFrameworkContext::checkRequirePlugin(ctkPluginPrivate *plugin)
       if (!ok && pr->resolution == ctkPluginConstants::RESOLUTION_MANDATORY)
       {
         tempResolved.clear();
-        qDebug() << "checkRequirePlugin: failed to satisfy:" << pr->name;
+        if (debug.resolve)
+        {
+          qDebug() << "checkRequirePlugin: failed to satisfy:" << pr->name;
+        }
         throw ctkPluginException(QString("Failed to resolve required plugin: %1").arg(pr->name));
       }
     }

+ 6 - 0
Libs/PluginFramework/ctkPluginFrameworkContext_p.h

@@ -30,6 +30,7 @@
 #include "ctkPluginStorage_p.h"
 #include "ctkPlugins_p.h"
 #include "ctkPluginFrameworkListeners_p.h"
+#include "ctkPluginFrameworkDebug_p.h"
 
 
 class ctkPlugin;
@@ -97,6 +98,11 @@ public:
   ctkProperties props;
 
   /**
+   * Debug handle.
+   */
+  ctkPluginFrameworkDebug debug;
+
+  /**
    * Contruct a framework context
    *
    */

+ 63 - 0
Libs/PluginFramework/ctkPluginFrameworkDebug.cpp

@@ -0,0 +1,63 @@
+/*=============================================================================
+
+  Library: CTK
+
+  Copyright (c) German Cancer Research Center,
+    Division of Medical and Biological Informatics
+
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+
+=============================================================================*/
+
+
+#include "ctkPluginFrameworkDebug_p.h"
+
+QString ctkPluginFrameworkDebug::ERRORS_PROP = "org.commontk.pluginfw.debug.errors";
+QString ctkPluginFrameworkDebug::FRAMEWORK_PROP = "org.commontk.pluginfw.debug.pluginfw";
+QString ctkPluginFrameworkDebug::HOOKS_PROP = "org.commontk.pluginfw.debug.hooks";
+QString ctkPluginFrameworkDebug::LAZY_ACTIVATION_PROP = "org.commontk.pluginfw.debug.lazy_activation";
+QString ctkPluginFrameworkDebug::LDAP_PROP = "org.commontk.pluginfw.debug.ldap";
+QString ctkPluginFrameworkDebug::SERVICE_REFERENCE_PROP = "org.commontk.pluginfw.debug.service_reference";
+QString ctkPluginFrameworkDebug::STARTLEVEL_PROP = "org.commontk.pluginfw.debug.startlevel";
+QString ctkPluginFrameworkDebug::URL_PROP = "org.commontk.pluginfw.debug.url";
+QString ctkPluginFrameworkDebug::RESOLVE_PROP = "org.commontk.pluginfw.debug.url";
+
+//----------------------------------------------------------------------------
+ctkPluginFrameworkDebug::ctkPluginFrameworkDebug(ctkProperties& props)
+{
+  setPropertyIfNotSet(props, ERRORS_PROP, false);
+  setPropertyIfNotSet(props, FRAMEWORK_PROP, false);
+  setPropertyIfNotSet(props, HOOKS_PROP, false);
+  setPropertyIfNotSet(props, LAZY_ACTIVATION_PROP, false);
+  setPropertyIfNotSet(props, LDAP_PROP, false);
+  setPropertyIfNotSet(props, SERVICE_REFERENCE_PROP, false);
+  setPropertyIfNotSet(props, STARTLEVEL_PROP, false);
+  setPropertyIfNotSet(props, URL_PROP, false);
+  errors = props.value(ERRORS_PROP).toBool();
+  framework = props.value(FRAMEWORK_PROP).toBool();
+  hooks = props.value(HOOKS_PROP).toBool();
+  lazy_activation = props.value(LAZY_ACTIVATION_PROP).toBool();
+  ldap = props.value(LDAP_PROP).toBool();
+  service_reference = props.value(SERVICE_REFERENCE_PROP).toBool();
+  startlevel = props.value(STARTLEVEL_PROP).toBool();
+  url = props.value(URL_PROP).toBool();
+}
+
+//----------------------------------------------------------------------------
+void ctkPluginFrameworkDebug::setPropertyIfNotSet(ctkProperties& props, const QString& key, const QVariant& val)
+{
+  if (!props.contains(key))
+  {
+    props.insert(key, val);
+  }
+}

+ 98 - 0
Libs/PluginFramework/ctkPluginFrameworkDebug_p.h

@@ -0,0 +1,98 @@
+/*=============================================================================
+
+  Library: CTK
+
+  Copyright (c) German Cancer Research Center,
+    Division of Medical and Biological Informatics
+
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+
+=============================================================================*/
+
+
+#ifndef CTKPLUGINFRAMEWORKDEBUG_P_H
+#define CTKPLUGINFRAMEWORKDEBUG_P_H
+
+#include "ctkPluginFramework_global.h"
+
+/**
+ * Variables that control debugging of the pluginfw code.
+ */
+class ctkPluginFrameworkDebug
+{
+
+public:
+  ctkPluginFrameworkDebug(ctkProperties& props);
+
+  /**
+   * Report error handling events.
+   */
+  static QString ERRORS_PROP; // = "org.commontk.pluginfw.debug.errors";
+  bool errors;
+
+  /**
+   * Report pluginfw create, init, start, stop
+   */
+  static QString FRAMEWORK_PROP; // = "org.commontk.pluginfw.debug.pluginfw";
+  bool framework;
+
+  /**
+   * Report hooks handling
+   */
+  static QString HOOKS_PROP; // = "org.commontk.pluginfw.debug.hooks";
+  bool hooks;
+
+  /**
+   * Report triggering of lazy activation
+   */
+  static QString LAZY_ACTIVATION_PROP; // = "org.commontk.pluginfw.debug.lazy_activation";
+  bool lazy_activation;
+
+  /**
+   * Report LDAP handling
+   */
+  static QString LDAP_PROP; // = "org.commontk.pluginfw.debug.ldap";
+  bool ldap;
+
+  /**
+   * Print information about service reference lookups
+   * and rejections due to missing permissions
+   * for calling plug-ins.
+   */
+  static QString SERVICE_REFERENCE_PROP; // = "org.commontk.pluginfw.debug.service_reference";
+  bool service_reference;
+
+  /**
+   * Report startlevel.
+   */
+  static QString STARTLEVEL_PROP; // = "org.commontk.pluginfw.debug.startlevel";
+  bool startlevel;
+
+  /**
+   * Report url
+   */
+  static QString URL_PROP; // = "org.commontk.pluginfw.debug.url";
+  bool url;
+
+  /**
+   * Report plug-in resolve progress
+   */
+  static QString RESOLVE_PROP; // = "org.commontk.pluginfw.debug.url";
+  bool resolve;
+
+private:
+
+  void setPropertyIfNotSet(ctkProperties& props, const QString& key, const QVariant& val);
+};
+
+#endif // CTKPLUGINFRAMEWORKDEBUG_P_H

+ 8 - 6
Libs/PluginFramework/ctkPluginFrameworkListeners.cpp

@@ -21,6 +21,7 @@
 
 #include "ctkPluginFrameworkListeners_p.h"
 
+#include "ctkPluginFrameworkContext_p.h"
 #include "ctkPluginConstants.h"
 #include "ctkLDAPExpr_p.h"
 #include "ctkServiceReferencePrivate.h"
@@ -33,7 +34,8 @@ const int ctkPluginFrameworkListeners::SERVICE_ID_IX = 1;
 const int ctkPluginFrameworkListeners::SERVICE_PID_IX = 2;
 
 //----------------------------------------------------------------------------
-ctkPluginFrameworkListeners::ctkPluginFrameworkListeners()
+ctkPluginFrameworkListeners::ctkPluginFrameworkListeners(ctkPluginFrameworkContext* pluginFw)
+  : pluginFw(pluginFw)
 {
   hashedServiceKeys << ctkPluginConstants::OBJECTCLASS.toLower()
       << ctkPluginConstants::SERVICE_ID.toLower()
@@ -114,7 +116,7 @@ QSet<ctkServiceSlotEntry> ctkPluginFrameworkListeners::getMatchingServiceSlots(
     }
   }
 
-  //if (listeners.framework.debug.ldap)
+  if (pluginFw->debug.ldap)
   {
     qDebug() << "Added" << set.size() << "out of" << n
       << "listeners with complicated filters";
@@ -214,7 +216,7 @@ void ctkPluginFrameworkListeners::serviceChanged(
     //}
   }
 
-  //if (framework.debug.ldap)
+  if (pluginFw->debug.ldap)
   {
     qDebug() << "Notified" << n << " listeners";
   }
@@ -274,7 +276,7 @@ void ctkPluginFrameworkListeners::checkSimple(const ctkServiceSlotEntry& sse)
     }
     else
     {
-      //if (listeners.framework.debug.ldap)
+      if (pluginFw->debug.ldap)
       {
         qDebug() << "## DEBUG: Too complicated filter:" << sse.getFilter();
       }
@@ -290,7 +292,7 @@ void ctkPluginFrameworkListeners::addToSet(QSet<ctkServiceSlotEntry>& set,
   QList<ctkServiceSlotEntry>& l = cache[cache_ix][val];
   if (!l.isEmpty())
   {
-    //if (listeners.framework.debug.ldap)
+    if (pluginFw->debug.ldap)
     {
       qDebug() << hashedServiceKeys[cache_ix] << "matches" << l.size();
     }
@@ -301,7 +303,7 @@ void ctkPluginFrameworkListeners::addToSet(QSet<ctkServiceSlotEntry>& set,
   }
   else
   {
-    //if (listeners.framework.debug.ldap)
+    if (pluginFw->debug.ldap)
     {
       qDebug() << hashedServiceKeys[cache_ix] << "matches none";
     }

+ 3 - 1
Libs/PluginFramework/ctkPluginFrameworkListeners_p.h

@@ -43,7 +43,7 @@ class ctkPluginFrameworkListeners : public QObject
 
 public:
 
-  ctkPluginFrameworkListeners();
+  ctkPluginFrameworkListeners(ctkPluginFrameworkContext* pluginFw);
 
   /**
    * Add a slot receiving service envents with filter to the current framework.
@@ -124,6 +124,8 @@ private:
 
   QSet<ctkServiceSlotEntry> serviceSet;
 
+  ctkPluginFrameworkContext* pluginFw;
+
   /**
    * Remove all references to a service slot from the service listener
    * cache.

+ 8 - 2
Libs/PluginFramework/ctkPluginPrivate.cpp

@@ -254,7 +254,10 @@ void ctkPluginPrivate::finalizeActivation()
     //6:
     state = ctkPlugin::STARTING;
     activating = true;
-    qDebug() << "activating #" << this->id;
+    if (fwCtx->debug.lazy_activation)
+    {
+      qDebug() << "activating #" << this->id;
+    }
     //7:
     if (!pluginContext)
     {
@@ -455,7 +458,10 @@ void ctkPluginPrivate::start0()
     throw ctkPluginException("ctkPlugin start failed", ctkPluginException::ACTIVATOR_ERROR, &e);
   }
 
-  qDebug() << "activating #" << id << "completed.";
+  if (fwCtx->debug.lazy_activation)
+  {
+    qDebug() << "activating #" << id << "completed.";
+  }
 
   //10:
   fwCtx->listeners.emitPluginChanged(ctkPluginEvent(ctkPluginEvent::STARTED, this->q_func()));

+ 1 - 1
Libs/PluginFramework/ctkPluginTrackerPrivate.tpp

@@ -24,7 +24,7 @@
 
 //----------------------------------------------------------------------------
 template<class T>
-const bool ctkPluginTrackerPrivate<T>::DEBUG = true;
+const bool ctkPluginTrackerPrivate<T>::DEBUG = false;
 
 //----------------------------------------------------------------------------
 template<class T>

+ 0 - 1
Libs/PluginFramework/ctkPlugins.cpp

@@ -103,7 +103,6 @@ QSharedPointer<ctkPlugin> ctkPlugins::install(const QUrl& location, QIODevice* i
         }
         else
         {
-          qDebug() << QString("Trying to install file:") << location.path();
           localPluginPath = location.toLocalFile();
         }
       }

+ 5 - 3
Libs/PluginFramework/ctkServices.cpp

@@ -167,9 +167,11 @@ ctkServiceReference ctkServices::get(ctkPluginPrivate* plugin, const QString& cl
   QMutexLocker lock(&mutex);
   try {
     QList<ctkServiceReference> srs = get(clazz, QString(), plugin);
-    qDebug() << "get service ref" << clazz << "for plugin"
-             << plugin->location << " = " << srs.size() << "refs";
-
+    if (framework->debug.service_reference)
+    {
+      qDebug() << "get service ref" << clazz << "for plugin"
+               << plugin->location << " = " << srs.size() << "refs";
+    }
     if (!srs.isEmpty()) {
       return srs.front();
     }