Browse Source

ZF-10537: File & Line info is incorrect for FirePHP messages sent via Zend_Wildfire

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@23066 44c647ce-9c0f-0410-b52a-842ac1e357ba
cadorn 15 years ago
parent
commit
fd58938af4

+ 2 - 1
library/Zend/Log/Writer/Firebug.php

@@ -195,6 +195,7 @@ class Zend_Log_Writer_Firebug extends Zend_Log_Writer_Abstract
         Zend_Wildfire_Plugin_FirePhp::getInstance()->send($message,
                                                           $label,
                                                           $type,
-                                                          array('traceOffset'=>6));
+                                                          array('traceOffset'=>4,
+                                                                'fixZendLogOffsetIfApplicable'=>true));
     }
 }

+ 18 - 2
library/Zend/Wildfire/Plugin/FirePhp.php

@@ -475,7 +475,8 @@ class Zend_Wildfire_Plugin_FirePhp implements Zend_Wildfire_Plugin_Interface
             if (!isset($meta['File']) || !isset($meta['Line'])) {
 
                 if (!$trace) {
-                    $trace = $firephp->_getStackTrace($options);
+                    $trace = $firephp->_getStackTrace(array_merge($options,
+                                                                  array('maxTraceDepth'=>$options['maxTraceDepth']+1)));
                 }
 
                 $meta['File'] = isset($trace[0]['file'])?$trace[0]['file']:'';
@@ -513,7 +514,22 @@ class Zend_Wildfire_Plugin_FirePhp implements Zend_Wildfire_Plugin_Interface
     {
         $trace = debug_backtrace();
 
-        return array_splice($trace, $options['traceOffset'], $options['maxTraceDepth']);
+        $trace = array_splice($trace, $options['traceOffset']);
+        
+        if (!count($trace)) {
+            return $trace;
+        }
+
+        if (isset($options['fixZendLogOffsetIfApplicable']) && $options['fixZendLogOffsetIfApplicable']) {
+            if (count($trace) >=3 &&
+                isset($trace[0]['file']) && substr($trace[0]['file'], -7, 7)=='Log.php' &&
+                isset($trace[1]['function']) && $trace[1]['function']=='__call') {
+
+                $trace = array_splice($trace, 2);
+            }
+        }
+
+        return array_splice($trace, 0, $options['maxTraceDepth']);
     }
 
     /**

+ 52 - 0
tests/Zend/Log/Writer/FirebugTest.php

@@ -297,6 +297,58 @@ class Zend_Log_Writer_FirebugTest extends PHPUnit_Framework_TestCase
         $logger = Zend_Log::factory($cfg['log']);
         $this->assertTrue($logger instanceof Zend_Log);
     }
+
+    /**
+     * @group ZF-10537
+     */
+    public function testFileLineOffsets()
+    {
+        $firephp = Zend_Wildfire_Plugin_FirePhp::getInstance();
+        $channel = Zend_Wildfire_Channel_HttpHeaders::getInstance();
+        $protocol = $channel->getProtocol(Zend_Wildfire_Plugin_FirePhp::PROTOCOL_URI);
+        $firephp->setOption('includeLineNumbers', true);
+        $firephp->setOption('maxTraceDepth', 0);
+
+        $lines = array();
+        // NOTE: Do NOT separate the following pairs otherwise the line numbers will not match for the test
+
+        // Message number: 1
+        $lines[] = __LINE__+1;
+        $this->_logger->log('Hello World', Zend_Log::INFO);
+
+        // Message number: 2
+        $this->_logger->addPriority('TRACE', 8);
+        $this->_writer->setPriorityStyle(8, 'TRACE');
+        $lines[] = __LINE__+1;
+        $this->_logger->trace('Trace to here');
+
+        // Message number: 3
+        $this->_logger->addPriority('TABLE', 9);
+        $this->_writer->setPriorityStyle(9, 'TABLE');
+        $table = array('Summary line for the table',
+                       array(
+                           array('Column 1', 'Column 2'),
+                           array('Row 1 c 1',' Row 1 c 2'),
+                           array('Row 2 c 1',' Row 2 c 2')
+                       )
+                      );
+        $lines[] = __LINE__+1;
+        $this->_logger->table($table);
+
+        // Message number: 4
+        $lines[] = __LINE__+1;
+        $this->_logger->info('Hello World');
+
+        $messages = $protocol->getMessages();
+        $messages = $messages[Zend_Wildfire_Plugin_FirePhp::STRUCTURE_URI_FIREBUGCONSOLE][Zend_Wildfire_Plugin_FirePhp::PLUGIN_URI];
+
+        for( $i=0 ; $i<sizeof($messages) ; $i++ ) {
+            if(!preg_match_all('/FirebugTest\.php","Line":' . $lines[$i] . '/', $messages[$i], $m)) {
+                $this->fail("File and line does not match for message number: " . ($i+1));
+            }
+
+        }
+    }    
 }
 
 class Zend_Log_Writer_FirebugTest_Formatter extends Zend_Log_Formatter_Firebug

+ 46 - 0
tests/Zend/Wildfire/WildfireTest.php

@@ -994,6 +994,52 @@ class Zend_Wildfire_WildfireTest extends PHPUnit_Framework_TestCase
         $firephp = Zend_Wildfire_Plugin_FirePhp::getInstance();
         $firephp->send('This is a log message!');
     }
+
+    /**
+     * @group ZF-10537
+     */
+    public function testFileLineOffsets()
+    {
+        $this->_setupWithoutFrontController();
+
+        $firephp = Zend_Wildfire_Plugin_FirePhp::getInstance();
+        $channel = Zend_Wildfire_Channel_HttpHeaders::getInstance();
+        $protocol = $channel->getProtocol(Zend_Wildfire_Plugin_FirePhp::PROTOCOL_URI);
+        $firephp->setOption('includeLineNumbers', true);
+        $firephp->setOption('maxTraceDepth', 0);
+
+        $lines = array();
+        // NOTE: Do NOT separate the following pairs otherwise the line numbers will not match for the test
+
+        // Message number: 1
+        $lines[] = __LINE__+1;
+        $firephp->send('Hello World');
+
+        // Message number: 2
+        $lines[] = __LINE__+1;
+        $firephp->send('Hello World', null, 'TRACE');
+
+        // Message number: 3
+        $table = array('Summary line for the table',
+                       array(
+                           array('Column 1', 'Column 2'),
+                           array('Row 1 c 1',' Row 1 c 2'),
+                           array('Row 2 c 1',' Row 2 c 2')
+                       )
+                      );
+        $lines[] = __LINE__+1;
+        $firephp->send($table, null, 'TABLE');
+
+        $messages = $protocol->getMessages();
+        $messages = $messages[Zend_Wildfire_Plugin_FirePhp::STRUCTURE_URI_FIREBUGCONSOLE][Zend_Wildfire_Plugin_FirePhp::PLUGIN_URI];
+
+        for( $i=0 ; $i<sizeof($messages) ; $i++ ) {
+            if(!preg_match_all('/WildfireTest\.php","Line":' . $lines[$i] . '/', $messages[$i], $m)) {
+                $this->fail("File and line does not match for message number: " . ($i+1));
+            }
+
+        }
+    }
 }
 
 class Zend_Wildfire_WildfireTest_TestObject1