FELIX-3825 Include component ID in logging when known

git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1424314 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/scr/src/main/java/org/apache/felix/scr/impl/BundleComponentActivator.java b/scr/src/main/java/org/apache/felix/scr/impl/BundleComponentActivator.java
index 8fce1f6..52e9ffd 100644
--- a/scr/src/main/java/org/apache/felix/scr/impl/BundleComponentActivator.java
+++ b/scr/src/main/java/org/apache/felix/scr/impl/BundleComponentActivator.java
@@ -138,7 +138,7 @@
                 // 112.4.1 If an XML document specified by the header cannot be located in the bundle and its attached
                 // fragments, SCR must log an error message with the Log Service, if present, and continue.
                 log( LogService.LOG_ERROR, "Component descriptor entry ''{0}'' not found", new Object[]
-                    { descriptorLocation }, null, null );
+                    { descriptorLocation }, null, null, null );
                 continue;
             }
 
@@ -252,7 +252,7 @@
                 {
                     // There is a problem with this particular component, we'll log the error
                     // and proceed to the next one
-                    log( LogService.LOG_ERROR, "Cannot register Component", metadata, t );
+                    log( LogService.LOG_ERROR, "Cannot register Component", metadata, null, t );
 
                     // make sure the name is not reserved any more
                     if ( key != null )
@@ -268,12 +268,12 @@
             // fragments, SCR must log an error message with the Log Service, if present, and continue.
 
             log( LogService.LOG_ERROR, "Problem reading descriptor entry ''{0}''", new Object[]
-                { descriptorLocation }, null, ex );
+                { descriptorLocation }, null, null, ex );
         }
         catch ( Exception ex )
         {
             log( LogService.LOG_ERROR, "General problem with descriptor entry ''{0}''", new Object[]
-                { descriptorLocation }, null, ex );
+                { descriptorLocation }, null, null, ex );
         }
         finally
         {
@@ -306,7 +306,7 @@
         m_active = false;
 
         log( LogService.LOG_DEBUG, "BundleComponentActivator : Bundle [{0}] will destroy {1} instances", new Object[]
-            { new Long( m_context.getBundle().getBundleId() ), new Integer( m_managers.size() ) }, null, null );
+            { new Long( m_context.getBundle().getBundleId() ), new Integer( m_managers.size() ) }, null, null, null );
 
         while ( m_managers.size() != 0 )
         {
@@ -319,7 +319,7 @@
             catch ( Exception e )
             {
                 log( LogService.LOG_ERROR, "BundleComponentActivator : Exception invalidating", holder
-                    .getComponentMetadata(), e );
+                    .getComponentMetadata(), null, e );
             }
             finally
             {
@@ -330,7 +330,7 @@
         }
 
         log( LogService.LOG_DEBUG, "BundleComponentActivator : Bundle [{0}] STOPPED", new Object[]
-            {m_context.getBundle().getBundleId()}, null, null );
+            {m_context.getBundle().getBundleId()}, null, null, null );
 
         if (m_logService != null) {
             m_logService.close();
@@ -394,12 +394,12 @@
         {
             try
             {
-                log( LogService.LOG_DEBUG, "Enabling Component", aHolder.getComponentMetadata(), null );
+                log( LogService.LOG_DEBUG, "Enabling Component", aHolder.getComponentMetadata(), null, null );
                 aHolder.enableComponents( true );
             }
             catch ( Throwable t )
             {
-                log( LogService.LOG_ERROR, "Cannot enable component", aHolder.getComponentMetadata(), t );
+                log( LogService.LOG_ERROR, "Cannot enable component", aHolder.getComponentMetadata(), null, t );
             }
         }
     }
@@ -426,12 +426,12 @@
         {
             try
             {
-                log( LogService.LOG_DEBUG, "Disabling Component", aHolder.getComponentMetadata(), null );
+                log( LogService.LOG_DEBUG, "Disabling Component", aHolder.getComponentMetadata(), null, null );
                 aHolder.disableComponents( true );
             }
             catch ( Throwable t )
             {
-                log( LogService.LOG_ERROR, "Cannot disable component", aHolder.getComponentMetadata(), t );
+                log( LogService.LOG_ERROR, "Cannot disable component", aHolder.getComponentMetadata(), null, t );
             }
         }
     }
@@ -501,7 +501,7 @@
             }
             else
             {
-                log( LogService.LOG_DEBUG, "Component Actor Thread not running, calling synchronously", null, null );
+                log( LogService.LOG_DEBUG, "Component Actor Thread not running, calling synchronously", null, null, null );
                 try
                 {
                     synchronized ( this )
@@ -511,14 +511,14 @@
                 }
                 catch ( Throwable t )
                 {
-                    log( LogService.LOG_WARNING, "Unexpected problem executing task", null, t );
+                    log( LogService.LOG_WARNING, "Unexpected problem executing task", null, null, t );
                 }
             }
         }
         else
         {
             log( LogService.LOG_WARNING, "BundleComponentActivator is not active; not scheduling {0}", new Object[]
-                { task }, null, null );
+                { task }, null, null, null );
         }
     }
 
@@ -541,16 +541,16 @@
      * @param pattern The <code>java.text.MessageFormat</code> message format
      *      string for preparing the message
      * @param arguments The format arguments for the <code>pattern</code>
-     *      string.
+ *      string.
+     * @param componentId
      * @param ex An optional <code>Throwable</code> whose stack trace is written,
-     *      or <code>null</code> to not log a stack trace.
      */
-    public void log( int level, String pattern, Object[] arguments, ComponentMetadata metadata, Throwable ex )
+    public void log( int level, String pattern, Object[] arguments, ComponentMetadata metadata, Long componentId, Throwable ex )
     {
         if ( isLogEnabled( level ) )
         {
             final String message = MessageFormat.format( pattern, arguments );
-            log( level, message, metadata, ex );
+            log( level, message, metadata, componentId, ex );
         }
     }
 
@@ -562,17 +562,24 @@
      *
      * @param level The log level to log the message at
      * @param message The message to log
+     * @param componentId
      * @param ex An optional <code>Throwable</code> whose stack trace is written,
-     *      or <code>null</code> to not log a stack trace.
      */
-    public void log( int level, String message, ComponentMetadata metadata, Throwable ex )
+    public void log( int level, String message, ComponentMetadata metadata, Long componentId, Throwable ex )
     {
         if ( isLogEnabled( level ) )
         {
             // prepend the metadata name to the message
             if ( metadata != null )
             {
-                message = "[" + metadata.getName() + "] " + message;
+                if ( componentId != null )
+                {
+                    message = "[" + metadata.getName() + "(" + componentId + ")] " + message;
+                }
+                else
+                {
+                    message = "[" + metadata.getName() + "] " + message;
+                }
             }
 
             ServiceTracker logService = m_logService;
diff --git a/scr/src/main/java/org/apache/felix/scr/impl/config/ImmediateComponentHolder.java b/scr/src/main/java/org/apache/felix/scr/impl/config/ImmediateComponentHolder.java
index 33297b6..63220f8 100644
--- a/scr/src/main/java/org/apache/felix/scr/impl/config/ImmediateComponentHolder.java
+++ b/scr/src/main/java/org/apache/felix/scr/impl/config/ImmediateComponentHolder.java
@@ -525,7 +525,7 @@
         BundleComponentActivator activator = getActivator();
         if ( activator != null )
         {
-            activator.log( level, message, getComponentMetadata(), ex );
+            activator.log( level, message, getComponentMetadata(), null, ex );
         }
     }
 
@@ -534,7 +534,7 @@
         BundleComponentActivator activator = getActivator();
         if ( activator != null )
         {
-            activator.log( level, message, arguments, getComponentMetadata(), ex );
+            activator.log( level, message, arguments, getComponentMetadata(), null, ex );
         }
     }
 
diff --git a/scr/src/main/java/org/apache/felix/scr/impl/helper/Logger.java b/scr/src/main/java/org/apache/felix/scr/impl/helper/Logger.java
index 51cea8c..df96e65 100644
--- a/scr/src/main/java/org/apache/felix/scr/impl/helper/Logger.java
+++ b/scr/src/main/java/org/apache/felix/scr/impl/helper/Logger.java
@@ -45,11 +45,12 @@
      * @param pattern The <code>java.text.MessageFormat</code> message format
      *      string for preparing the message
      * @param arguments The format arguments for the <code>pattern</code>
-     *      string.
+ *      string.
+     * @param metadata  component metadata if known
+     * @param componentId  component ID if known
      * @param ex An optional <code>Throwable</code> whose stack trace is written,
-     *      or <code>null</code> to not log a stack trace.
      */
-    void log( int level, String pattern, Object[] arguments, ComponentMetadata metadata, Throwable ex );
+    void log( int level, String pattern, Object[] arguments, ComponentMetadata metadata, Long componentId, Throwable ex );
 
 
     /**
@@ -58,12 +59,12 @@
      * @param level The log level of the messages. This corresponds to the log
      *          levels defined by the OSGi LogService.
      * @param message The message to print
-     * @param metadata The {@link ComponentMetadata} whose processing caused
-     *          the message. This may be <code>null</code> if the component
-     *          metadata is not known or applicable.
+     * @param metadata The {@link org.apache.felix.scr.impl.metadata.ComponentMetadata} whose processing caused
+ *          the message. This may be <code>null</code> if the component
+ *          metadata is not known or applicable.
+     * @param componentId
      * @param ex The <code>Throwable</code> causing the message to be logged.
-     *          This may be <code>null</code>.
      */
-    void log( int level, String message, ComponentMetadata metadata, Throwable ex );
+    void log( int level, String message, ComponentMetadata metadata, Long componentId, Throwable ex );
 
 }
diff --git a/scr/src/main/java/org/apache/felix/scr/impl/manager/AbstractComponentManager.java b/scr/src/main/java/org/apache/felix/scr/impl/manager/AbstractComponentManager.java
index c52693b..719fee7 100644
--- a/scr/src/main/java/org/apache/felix/scr/impl/manager/AbstractComponentManager.java
+++ b/scr/src/main/java/org/apache/felix/scr/impl/manager/AbstractComponentManager.java
@@ -221,7 +221,7 @@
             while ( ceiling  < trackingCount || ( !missing.isEmpty() && missing.iterator().next() < trackingCount))
             {
                 log( LogService.LOG_DEBUG, "waitForTracked trackingCount: {0} ceiling: {1} missing: {2}",
-                        new Object[] {trackingCount, ceiling, missing}, null);
+                        new Object[] {trackingCount, ceiling, missing}, null );
                 try
                 {
                     missing.wait( );
@@ -895,7 +895,7 @@
         BundleComponentActivator activator = getActivator();
         if ( activator != null )
         {
-            activator.log( level, message, getComponentMetadata(), ex );
+            activator.log( level, message, getComponentMetadata(), m_componentId, ex );
         }
     }
 
@@ -904,7 +904,7 @@
         BundleComponentActivator activator = getActivator();
         if ( activator != null )
         {
-            activator.log( level, message, arguments, getComponentMetadata(), ex );
+            activator.log( level, message, arguments, getComponentMetadata(), m_componentId, ex );
         }
     }
 
diff --git a/scr/src/main/java/org/apache/felix/scr/impl/metadata/XmlHandler.java b/scr/src/main/java/org/apache/felix/scr/impl/metadata/XmlHandler.java
index 49304d9..0ded76d 100644
--- a/scr/src/main/java/org/apache/felix/scr/impl/metadata/XmlHandler.java
+++ b/scr/src/main/java/org/apache/felix/scr/impl/metadata/XmlHandler.java
@@ -254,7 +254,7 @@
                 {
                     m_logger.log( LogService.LOG_DEBUG,
                         "Not currently parsing a component; ignoring element {0} (bundle {1})", new Object[]
-                            { localName, m_bundle.getLocation() }, null, null );
+                            { localName, m_bundle.getLocation() }, null, null, null );
                 }
 
                 // 112.4.4 Implementation
@@ -356,7 +356,7 @@
                 else if ( !localName.equals( "components" ) )
                 {
                     m_logger.log( LogService.LOG_DEBUG, "Ignoring unsupported element {0} (bundle {1})", new Object[]
-                        { localName, m_bundle.getLocation() }, null, null );
+                        { localName, m_bundle.getLocation() }, null, null, null );
                 }
             }
             catch ( Exception ex )
@@ -370,7 +370,7 @@
         else if ( !localName.equals( "components" ) )
         {
             m_logger.log( LogService.LOG_DEBUG, "Ignoring unsupported element '{'{0}'}'{1} (bundle {2})", new Object[]
-                { uri, localName, m_bundle.getLocation() }, null, null );
+                { uri, localName, m_bundle.getLocation() }, null, null, null );
         }
     }
 
diff --git a/scr/src/test/java/org/apache/felix/scr/impl/MockLogger.java b/scr/src/test/java/org/apache/felix/scr/impl/MockLogger.java
index 31087ca..c21014b 100644
--- a/scr/src/test/java/org/apache/felix/scr/impl/MockLogger.java
+++ b/scr/src/test/java/org/apache/felix/scr/impl/MockLogger.java
@@ -36,16 +36,16 @@
     }
 
 
-    public void log( int level, String pattern, Object[] arguments, ComponentMetadata metadata, Throwable ex )
+    public void log( int level, String pattern, Object[] arguments, ComponentMetadata metadata, Long componentId, Throwable ex )
     {
         if ( isLogEnabled( level ) )
         {
-            log( level, MessageFormat.format( pattern, arguments ), metadata, ex );
+            log( level, MessageFormat.format( pattern, arguments ), metadata, null, ex );
         }
     }
 
 
-    public void log( int level, String message, ComponentMetadata metadata, Throwable ex )
+    public void log( int level, String message, ComponentMetadata metadata, Long componentId, Throwable ex )
     {
         lastMessage = message;
     }