debug_backtrace and FILE/LINE indices

This is a discussion on debug_backtrace and FILE/LINE indices within the PHP Language forums, part of the PHP Programming Forums category; Hi all, I've been using debug_backtrace() for a while to provide stack traces for a custom logging function. Today ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-24-2007
Taras_96
 
Posts: n/a
Default debug_backtrace and FILE/LINE indices

Hi all,

I've been using debug_backtrace() for a while to provide stack traces
for a custom logging function. Today was the first time I've got the
following error:

"Notice: Undefined index: file in E:\webroot\lib\errors.php on line
341

Notice: Undefined index: line in E:\webroot\lib\errors.php on line
341"

related to the following code:

$vDebug = debug_backtrace();
$vFiles = array();
for ($i=0;$i<count($vDebug);$i++) {
// skip the first one, since it's always this func
if ($i==0) { continue; }
$aFile = $vDebug[$i];
var_dump($aFile);
$vFiles[] = '('.basename($aFile['file']).':'.
$aFile['line'].')';
} // for
return implode(',',$vFiles);

I checked the PHP documentation for this function, and to my surprise
found that:

"The *possible* returned elements are as follows:", ie: 'file' and
'line' wont always be returned. When are these elements returned, so
that I can make the function do something useful when file/line isn't
returned?

Thanks

Taras

Reply With Quote
  #2 (permalink)  
Old 04-24-2007
Thomas Mlynarczyk
 
Posts: n/a
Default Re: debug_backtrace and FILE/LINE indices

Also sprach Taras_96:

> $vDebug = debug_backtrace();
> $vFiles = array();
> for ($i=0;$i<count($vDebug);$i++) {
> // skip the first one, since it's always this func
> if ($i==0) { continue; }
> $aFile = $vDebug[$i];
> var_dump($aFile);
> $vFiles[] = '('.basename($aFile['file']).':'.
> $aFile['line'].')';
> } // for
> return implode(',',$vFiles);
>
> I checked the PHP documentation for this function, and to my surprise
> found that:
>
> "The *possible* returned elements are as follows:", ie: 'file' and
> 'line' wont always be returned. When are these elements returned, so
> that I can make the function do something useful when file/line isn't
> returned?


A workaround: If 'file' and 'line' are not returned, use the respective
values from the next entry in the backtrace array. Here's what I use:

$aTrace = debug_backtrace();
// Loop backwards (!) through array
for ( $sFile = '', $iLine = 0, $i = count( $aTrace ); $i--; )
{
// Make sure all fields are set
$aTrace[$i]
= array_merge(
array(
'function' => '',
'type' => '',
'class' => '',
'object' => array(),
'args' => array(),
'file' => $sFile,
'line' => $iLine
),
$aTrace[$i]
);

// Add this for convenience
$aTrace[$i]['call']
= $aTrace[$i]['class']
. $aTrace[$i]['type']
. $aTrace[$i]['function'];

// Missing file or line? Copy from previous item
$sFile = $aTrace[$i]['file'];
$iLine = $aTrace[$i]['line'];
}

// Remove first item ("this function")
array_shift( $aTrace );

This way, all possible fields are set to useful values. If you're only
interested in file and line, you can optimize the code of course.

Greetings,
Thomas



Reply With Quote
  #3 (permalink)  
Old 04-28-2007
Taras_96
 
Posts: n/a
Default Re: debug_backtrace and FILE/LINE indices

Thanks Thomans, I'll try out your suggestion and see how it goes :).

Taras

On Apr 25, 5:01 am, "Thomas Mlynarczyk" <tho...@mlynarczyk-
webdesign.de> wrote:
> Also sprach Taras_96:
>
>
>
> > $vDebug = debug_backtrace();
> > $vFiles = array();
> > for ($i=0;$i<count($vDebug);$i++) {
> > // skip the first one, since it's always this func
> > if ($i==0) { continue; }
> > $aFile = $vDebug[$i];
> > var_dump($aFile);
> > $vFiles[] = '('.basename($aFile['file']).':'.
> > $aFile['line'].')';
> > } // for
> > return implode(',',$vFiles);

>
> > I checked the PHP documentation for this function, and to my surprise
> > found that:

>
> > "The *possible* returned elements are as follows:", ie: 'file' and
> > 'line' wont always be returned. When are these elements returned, so
> > that I can make the function do something useful when file/line isn't
> > returned?

>
> A workaround: If 'file' and 'line' are not returned, use the respective
> values from the next entry in the backtrace array. Here's what I use:
>
> $aTrace = debug_backtrace();
> // Loop backwards (!) through array
> for ( $sFile = '', $iLine = 0, $i = count( $aTrace ); $i--; )
> {
> // Make sure all fields are set
> $aTrace[$i]
> = array_merge(
> array(
> 'function' => '',
> 'type' => '',
> 'class' => '',
> 'object' => array(),
> 'args' => array(),
> 'file' => $sFile,
> 'line' => $iLine
> ),
> $aTrace[$i]
> );
>
> // Add this for convenience
> $aTrace[$i]['call']
> = $aTrace[$i]['class']
> . $aTrace[$i]['type']
> . $aTrace[$i]['function'];
>
> // Missing file or line? Copy from previous item
> $sFile = $aTrace[$i]['file'];
> $iLine = $aTrace[$i]['line'];
> }
>
> // Remove first item ("this function")
> array_shift( $aTrace );
>
> This way, all possible fields are set to useful values. If you're only
> interested in file and line, you can optimize the code of course.
>
> Greetings,
> Thomas



Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 07:14 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0