Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

php5: debug_backtrace()

Empfohlene Antworten

Veröffentlicht

Hallo Allerseits :P

Hat jemand erfahrung mit der Funktion debug_backtrace()?

Also ich verwende diese für debuggen und error reporting, leider muss ich imme viel zu viel zeilen Coden.

Hier ein kleine Codeausschnit:

<?php
//debug.php
class log {
private $log;
private $err;
function __construct() {
$this->log[0] = "create class log: ".__file__;
ob_start();
function err() {
global $log;
$args = func_get_args();
$temp = debug_backtrace();
$temp = $temp[0][file] . " on " . $temp[0][line];
$log->err($temp, $args);
}
function debug() {
global $log;
$args = func_get_args();
$temp = debug_backtrace();
$temp = $temp[0][file] . " on " . $temp[0][line];
$log->debug($temp, $args);
}
}
public function save() {
echo "<!-- /*\r\n# BEGIN DEBUG\r\n\r\n";
echo implode($this->log, "\r\n" );
echo "\r\n\r\n# END DEBUG\r*/// -->\r\n";
}
public function debug($ziel, $args) {
if( count($args) != 0) {
$this->log[] = "DEBUG ". $ziel;
$temp = array();
for($i = 0; $i < count($args); $i++) {
$temp[] = var_export($args[$i], true);
}
$this->log[] = str_replace("\n", "\n\t", "\t".implode($temp, "\r\n" ));
}
}
public function err($ziel, $args) {
if( count($args) != 0) {
$this->log[] = "ERROR ". $ziel;
$this->err[] = "ERROR ". $ziel;
$temp = array();
for($i = 0; $i < count($args); $i++) {
$temp[] = var_export($args[$i], true);
}
$this->log[] = str_replace("\n", "\n\t", "\t".implode($temp, "\r\n" ));
$this->err[] = str_replace("\n", "\n\t", "\t".implode($temp, "\r\n" ));
}
}
function __destruct() {
$PAGE_OUTPUT = ob_get_contents();
ob_end_clean();
$this->save();
echo $PAGE_OUTPUT;
}
}
$log = new log;
?>[/PHP]

[PHP]<?php
//testClass.php
class testClass {
private $vars;
private function __set($Name, $Value) {

/* DEBUG */$temp = debug_backtrace();
/* DEBUG */$temp = $temp[0][file].' on '.$temp[0][line];
/* DEBUG */debug('__set() '.$temp, $Name, $Value);

$this->vars[$Name] = &$Value;
}
private function __get( $Name ) {
if( isset( $this->vars[$Name] ) ) {

/* DEBUG */$temp = debug_backtrace();
/* DEBUG */$temp = $temp[0][file].' on '.$temp[0][line];
/* DEBUG */debug('__get() '.$temp, $Name);

return $this->vars[$Name];
} else {

/* DEBUG */$temp = debug_backtrace();
/* DEBUG */$temp = $temp[0][file].' on '.$temp[0][line];
/* DEBUG */err('__get() '.$temp, '"'.$Name.'" not exist');

return NULL;
}
}
}
?>

<?php
//index.php
require_once('debug.php');
require_once('testClass.php');

$test = new testClass;
$test->var1 = "my var 1";
echo $test->var1;
echo $test->var2;
?>[/PHP]

HTML Ausgabe Quelltext

[code]<!-- /* # BEGIN DEBUG create class log: /var/www/web4/html/test/debug.php DEBUG /var/www/web4/html/test/testClass.php on 8 '__set() /var/www/web4/html/test/index.php on 6' 'var1' 'my var 1' DEBUG /var/www/web4/html/test/testClass.php on 17 '__get() /var/www/web4/html/test/index.php on 7' 'var1' ERROR /var/www/web4/html/test/testClass.php on 24 '__get() /var/www/web4/html/test/index.php on 8' '"var2" not exist' # END DEBUG */// --> my var 1[/code]

bis jetz muste ich immer umständlich in alle functionen und methoden die Function debug_backtrace(); verwenden um vom caller __FILE__ und __LINE__ zu erfahren und dann an debug() bzw. err() übergeben.

Kennt einer vieleicht eine möglichkei wie ich nur debug($verte) und err($werte) verwende und trotzdem den gesamte ablauf rausfinde?

ich Dank schonmal in voraus ^^

lg

nein, ich sehe keine möglichkeit an den caller zu kommen.

Allerdings wird mir der Sinn nicht ganz klar,

debuggen ;) gute idee, aber du willst jeden __set und __get Aufruf mitloggen ?

Ich würde eher Exceptions empfehlen... ich bezweifle das du jeden Anwendungsfall so detailiert durchtesten wirst, mit jeder Variablenkombination o.ä. ^^ Für den fall der Fälle können exceptions dann zur Laufzeit verarbeitet werden.

  • Autor
aber du willst jeden __set und __get Aufruf mitloggen ?
Ja aber das auch nur für tests bzw. in den fällen wo ich keine ausgabe habe wie kommunikation zwischen 2 servern.

Ich würde eher Exceptions empfehlen... ich bezweifle das du jeden Anwendungsfall so detailiert durchtesten wirst, mit jeder Variablenkombination o.ä. ^^ Für den fall der Fälle können exceptions dann zur Laufzeit verarbeitet werden.
Exceptions werden bereits in allen functionen/klassen/methoden verwendet und verarbeitet.

leider hab ich noch keine lösung gefunden für loggen bei fatal errors. Ich recherschiere gerade wie ich mit hilfe von bash scripten die PHP scripte unter aufsicht zu halten, aber leider momentan erfolglos.

Ich dank dir aufjede Fall für Antwort, ich versuch troztdem ein guten debbuger weiter zu entwickelt ;) und werd dann ma ins Forum Posten wenn was guter rauskomt :D

Liebe Grüße

J.Schmidt

es gibt eine möglichkeit für Fatal errors, "register_shutdown_function" und "error_get_last"

da gab es mal ein Beispiel im netz, du musst nur den error-type prüfen

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.