Source for file debugger.php

Documentation is available at debugger.php

  1. <?php
  2. /* ******************************************************************** */
  3. /* CATALYST PHP Source Code */
  4. /* -------------------------------------------------------------------- */
  5. /* This program is free software; you can redistribute it and/or modify */
  6. /* it under the terms of the GNU General Public License as published by */
  7. /* the Free Software Foundation; either version 2 of the License, or */
  8. /* (at your option) any later version. */
  9. /* */
  10. /* This program is distributed in the hope that it will be useful, */
  11. /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
  12. /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
  13. /* GNU General Public License for more details. */
  14. /* */
  15. /* You should have received a copy of the GNU General Public License */
  16. /* along with this program; if not, write to: */
  17. /* The Free Software Foundation, Inc., 59 Temple Place, Suite 330, */
  18. /* Boston, MA 02111-1307 USA */
  19. /* -------------------------------------------------------------------- */
  20. /* */
  21. /* Filename: debugger.php */
  22. /* Author: Paul Waite */
  23. /* Description: Definitions for the debugger module. */
  24. /* */
  25. /* ******************************************************************** */
  26. /** @package core */// some control over what messages appear on output..
  27.  
  28. /** Dummy value */
  29. ("DBG_UNDEFINED", -1);
  30. /** No debugging (redundant) */
  31. ("DBG_NONE", 0);
  32. /** Ad-hoc debugging output */
  33. ("DBG_DEBUG", 1);
  34. /** Diagnostic output */
  35. ("DBG_DIAGNOSTIC", 2);
  36. /** SQL queries to database */
  37. ("DBG_SQL", 4);
  38. /** SQL SELECT data from database */
  39. ("DBG_SQLDATA", 8);
  40. /** Dump HTTP and PHP page vars */
  41. ("DBG_DUMP", 16);
  42. /** Provide debug traceback info */
  43. ("DBG_TRACE", 32);
  44. /** Show table borders and validation checks */
  45. ("DBG_TABLES", 64);
  46. /** Show debugging profiler output */
  47. ("DBG_PROFILE", 128);
  48. /** DEBUG and DIAGNOSTICS as default setting */
  49. ("DBG_DEFAULT", 3);
  50. /** Everything. Warning, this can be VERBOSE! */
  51. ("DBG_ALL", 255);
  52.  
  53. // Defined output options. Where debug output goes..
  54. /** No output */
  55. ("DBG_O_NONE", 0);
  56. /** Output stored in $content */
  57. ("DBG_O_STORED", 1);
  58. /** Output via direct echo */
  59. ("DBG_O_ECHO", 2);
  60. /** Output for CLI: echoed raw ASCII, LF end-of-line */
  61. ("DBG_O_CLI", 4);
  62. /** Output to system log */
  63. ("DBG_O_LOG", 8);
  64. /** To logfile (not implemented yet) */
  65. ("DBG_O_LOGFILE", 16);
  66. /** Default output mode (stored) */
  67. ("DBG_O_DEFAULT", 1);
  68. /** Ubiquitous output mode */
  69. ("DBG_O_ALL", 255);
  70.  
  71. //-----------------------------------------------------------------------
  72. /**
  73. * The debugger class. Responsible for accumulating, filtering and then
  74. * rendering debug content to various outputs. This class is automatically
  75. * instantiated by the system as $RESPONSE->debugger; and is used by various
  76. * modules (eg. query) to log information. It can also be used as either
  77. * an ad-hoc debugging aid, or as an analysis tool where debug output
  78. * statements are inserted into code and left there permanently, their output
  79. * only switched on when problems arise. Various classes of debug output can
  80. * be enabled in any combination by OR'ing the following constants when the
  81. * debug_on() function is called:
  82. * DBG_DEBUG Ad-hoc debugging output
  83. * DBG_DIAGNOSTIC Diagnostic output
  84. * DBG_SQL SQL queries to database
  85. * DBG_SQLDATA SQL SELECT data from database
  86. * DBG_DUMP Dump HTTP and PHP page vars
  87. * DBG_TRACE Provide debug traceback info
  88. * DBG_TABLES Show table borders and validate cells
  89. * DBG_DEFAULT DEBUG and DIAGNOSTICS only
  90. * DBG_ALL Everything (verbose!)
  91. * eg. debug_on(DBG_SQL | DBG_DIAGNOSTIC | DBG_DUMP);
  92. * @package core
  93. */
  94. class webdebugger extends RenderableObject {
  95. // Public
  96. /** Accumulated debugging output content */
  97.  
  98. var $content = "";
  99. /** Classes of debugging to accumulate */
  100.  
  101. var $class = DBG_DEFAULT;
  102. /** Status of the debugger */
  103.  
  104. var $enabled = false;
  105. /** Modes of output for debugging content */
  106.  
  107. var $output_mode = DBG_O_DEFAULT;
  108.  
  109. // Private
  110. /** Array of traced locations
  111. @access private */
  112. var $trace = array();
  113. /** Depth of tracing
  114. @access private */
  115. var $tracedepth = 0;
  116. /** Microtimer object for profiling
  117. @access private */
  118. var $profile_timer;
  119. /** Profiler data array
  120. @access private */
  121. var $profile = array();
  122.  
  123. // ................................................................
  124. /**
  125. * Constructor
  126. * @param integer $class The classes of output to render
  127. */
  128. function webdebugger($class=DBG_DEFAULT) {
  129. $this->debug_class($class);
  130. }
  131. // ................................................................
  132. /**
  133. * Return whether we have any content, true or false.
  134. */
  135. function debug_hascontent() {
  136. return (
  137. $this->enabled &&
  138. (
  139. (strlen($this->content) > 0)
  140. || ($this->class & DBG_DUMP)
  141. || ($this->profiling() && count($this->profile) > 0)
  142. )
  143. );
  144. }
  145. // ................................................................
  146. /**
  147. * Set debug output mode
  148. * This function starts/stops the debugger from echoing
  149. * the content directly as well as storing it for a
  150. * subsequent display by webpage-defs.php. The values which
  151. * can be OR'ed together are as follows:
  152. * DBG_O_NONE No output
  153. * DBG_O_STORED Output stored in $content
  154. * DBG_O_ECHO Output via direct echo
  155. * DBG_O_CLI Output for CLI: echoed raw ASCII, LF end-of-line
  156. * DBG_O_LOG Output to system log
  157. * DBG_O_LOGFILE To logfile (not implemented yet)
  158. * DBG_O_DEFAULT Default output mode (echoed)
  159. * DBG_O_ALL Ubiquitous output mode
  160. * @param integer $mode Output mode of debugger
  161. */
  162. function debug_output($mode=DBG_O_DEFAULT) {
  163. $this->output_mode = $mode;
  164. }
  165. // ................................................................
  166. /**
  167. * Set debug output mode
  168. * Register content for debugging output. Check that it is
  169. * of an acceptable class and direct it all to the right place(s).
  170. * @param string $content Content to add to debug output buffer
  171. * @param integer $class Class of output
  172. */
  173. function debug_write($content, $class=DBG_DIAGNOSTIC) {
  174. if ($this->enabled) {
  175. // Are we providing traceback info..
  176. if ($this->class & DBG_TRACE) {
  177. if ($this->tracedepth > 0) {
  178. $content .= "[ " . $this->traceback() . " ] ";
  179. }
  180. }
  181. // Direct debugging content where we should..
  182. if ($class & $this->class) {
  183. if ($this->output_mode & DBG_O_STORED) {
  184. $this->content .= $content;
  185. }
  186. if ($this->output_mode & DBG_O_ECHO) {
  187. echo $content;
  188. }
  189. if ($this->output_mode & DBG_O_CLI) {
  190. $s = str_replace("<br>", "\n", $content);
  191. echo $s;
  192. }
  193. if ($this->output_mode & DBG_O_LOG) {
  194. error_log(strip_tags($content), 0);
  195. }
  196. }
  197. }
  198. return $this;
  199. }
  200. // ................................................................
  201. /**
  202. * Set debug class(es) to accept
  203. * @param integer $class Class of output
  204. */
  205. function debug_class($class) {
  206. if (isset($class)) {
  207. $this->class = $class & DBG_ALL;
  208. if ($this->class == DBG_NONE)
  209. $this->enabled = false;
  210. }
  211. return $this;
  212. }
  213. // ................................................................
  214. /**
  215. * Set debug mode
  216. * Set debug output to be enabled(default)/disabled
  217. * @param boolean $enabled If true enable debugger, else disable it
  218. * @param integer $class Class of output to accept
  219. */
  220. function debug_mode($enabled=true, $class=DBG_UNDEFINED) {
  221. $this->enabled = $enabled;
  222. if ($enabled) {
  223. $this->debug_class($class);
  224. }
  225. return $this;
  226. }
  227. // ................................................................
  228. /**
  229. * Flag debugger to dump global variables
  230. * Sets a flag for the debugger to dump all global vars at output time.
  231. * @access private
  232. */
  233. function debug_dump() {
  234. $this->class |= DBG_DUMP;
  235. return $this;
  236. }
  237. // ................................................................
  238. /**
  239. * Profile breakpoint.
  240. * Profiling allows you to ascertain the elapsed time between two
  241. * breakpoints. To do this just call this method twice with the same
  242. * label.
  243. * @param string $label A label for this profile breakpoint
  244. */
  245. function profile($label) {
  246. if (!isset($this->profile_timer)) {
  247. $this->profile_timer = new microtimer();
  248. $this->profile_timer->start();
  249. }
  250. $ms = $this->profile_timer->millisecs();
  251. if (isset($this->profile[$label])) {
  252. $bits = explode("|", $this->profile[$label]);
  253. $elapsed = $bits[0];
  254. $started = $bits[1];
  255. $finished = $bits[2];
  256. $calls = $bits[3];
  257. // Ending a profiling section
  258. if ($finished == "") {
  259. $finished = $ms;
  260. $elapsed += ($finished - $started);
  261. $calls += 1;
  262. }
  263. // Starting a profiling section..
  264. else {
  265. $started = $ms;
  266. $finished = "";
  267. }
  268. }
  269. else {
  270. $elapsed = 0;
  271. $started = $ms;
  272. $finished = "";
  273. $calls = 0;
  274. }
  275. $this->profile[$label] = "$elapsed|$started|$finished|$calls";
  276. }
  277. // ................................................................
  278. /** This makes sure the profiles are all finished.
  279. * @access private
  280. */
  281. function profile_close() {
  282. if (isset($this->profile_timer)) {
  283. $this->profile_timer->stop();
  284. foreach ($this->profile as $label => $prof) {
  285. $bits = explode("|", $prof);
  286. if ($bits[2] == "") {
  287. $this->profile($label);
  288. }
  289. }
  290. }
  291. }
  292. // ................................................................
  293. /** Returns true if this debugger is in profiling mode. */
  294.  
  295. function profiling() {
  296. return $this->class & DBG_PROFILE;
  297. }
  298. // ................................................................
  299. /**
  300. * List classes debugger is accepting
  301. * @return string List of classes acceptable to the debugger
  302. */
  303. function report_classes() {
  304. $s = "";
  305. if ($this->class & DBG_TRACE) $s .= "TRACEBACK ";
  306. if ($this->class & DBG_SQL) $s .= "SQL ";
  307. if ($this->class & DBG_DEBUG) $s .= "DEBUG ";
  308. if ($this->class & DBG_DIAGNOSTIC) $s .= "DIAGNOSTIC ";
  309. if ($this->class & DBG_DUMP) $s .= "DUMP ";
  310. if ($this->class & DBG_TABLES) $s .= "TABLES ";
  311. if ($this->class & DBG_PROFILE) $s .= "PROFILER ";
  312. $s = trim($s);
  313. $s = str_replace(" ", ",", $s);
  314. return $s;
  315. }
  316. // ................................................................
  317. /**
  318. * Push a trace on stack
  319. * Pushes a traceback label onto our trace stack. We return the
  320. * depth of trace we are at.
  321. * @param mixed $traceobj Thing to push on the stack
  322. * @return integer Depth of tracing so far
  323. * @access private
  324. */
  325. function pushtrace($traceobj) {
  326. if ($this->class & DBG_TRACE) {
  327. // First determine the trace label..
  328. if (is_object($traceobj)) {
  329. $label = "<font color=blue>" . get_class($traceobj) . "</font>";
  330. }
  331. elseif (is_string($traceobj)) {
  332. $label = "<font color=green>" . $traceobj . "</font>";
  333. }
  334. else {
  335. $label = "unknown";
  336. }
  337. array_push($this->trace, $label);
  338. $this->tracedepth = count($this->trace);
  339. // Automatic trace profiling..
  340. if ($this->profiling()) {
  341. $this->profile(strip_tags($label));
  342. }
  343. }
  344. return $this->tracedepth;
  345. }
  346. // ................................................................
  347. /**
  348. * Pop a trace off stack
  349. * Pop a trace label off our trace stack
  350. * @access private
  351. */
  352. function poptrace() {
  353. if ($this->class & DBG_TRACE) {
  354. if ($this->tracedepth > 0) {
  355. $label = array_pop($this->trace);
  356. if ($this->profiling()) {
  357. $this->profile(strip_tags($label));
  358. }
  359. return $label;
  360. }
  361. else {
  362. return "";
  363. }
  364. $this->tracedepth = count($this->trace);
  365. }
  366. }
  367. // ................................................................
  368. /**
  369. * Traceback list
  370. * Return a string of the form "label->label->label"
  371. * forming the traceback level labels in order.
  372. * @return string Traceback list
  373. */
  374. function traceback() {
  375. $traceback = "";
  376. if ($this->tracedepth > 0) {
  377. $ditto = "";
  378. foreach ($this->trace as $tracelabel) {
  379. if ($tracelabel != $ditto) {
  380. if ($traceback != "") $traceback .= "->";
  381. $traceback .= $tracelabel;
  382. $ditto = $tracelabel;
  383. }
  384. }
  385. }
  386. return $traceback;
  387. }
  388. // ................................................................
  389. /**
  390. * Use render() to render the debugger output.
  391. * Renders the debugger output content as HTML.
  392. * @see render()
  393. * @return string HTML rendering of debug output content
  394. */
  395. function html() {
  396. if (!$this->debug_hascontent()) return "";
  397.  
  398. // Get names of globals we will use. These depend on the
  399. // version of Php, since we won't rely on the old ones
  400. // sticking around forever..
  401. $vname_get = "HTTP_GET_VARS";
  402. $vname_post = "HTTP_POST_VARS";
  403. $vname_cookie = "HTTP_COOKIE_VARS";
  404. $vbits = explode(".", phpversion());
  405. $v1 = $vbits[0]; $v2 = $vbits[1];
  406. if (($v1 > 4) || ($v1 == 4 && $v2 >= 1)) {
  407. $vname_get = "_GET";
  408. $vname_post = "_POST";
  409. $vname_cookie = "_COOKIE";
  410. }
  411. // Bring them in..
  412. global $$vname_cookie;
  413. global $$vname_get;
  414. global $$vname_post;
  415. global $PHP_SELF;
  416. global $GLOBALS;
  417.  
  418. // Header stuff.,
  419. $header = "<h4>PHP Web Page Debugger</h4>";
  420. $header .= "<p>";
  421. $header .= "<b>File: $PHP_SELF</b><br>";
  422. $header .= "<b>Debug filter: " . $this->report_classes() . "</b><br>";
  423. $header .= "</p>";
  424. $footer = "+++ END OF DEBUG OUTPUT +++<br>";
  425. if ($this->class & DBG_DUMP) {
  426. $s ="<p>";
  427. $s .= "<table border=1 cellpadding=2 cellspacing=0>";
  428. if (isset($$vname_cookie)) {
  429. $s .= "<tr><td colspan=2><h4>Cookie Vars</h4></td></tr>";
  430. reset($$vname_cookie);
  431. while (list($key, $val) = each($$vname_cookie)) {
  432. $s .= "<tr><td>$key</td><td>" . displayvar($val) . "</td></tr>";
  433. }
  434. }
  435. if (isset($$vname_get)) {
  436. $s .= "<tr><td colspan=2><h4>GET Vars</h4></td></tr>";
  437. reset($$vname_get);
  438. while (list($key, $val) = each($$vname_get)) {
  439. $s .= "<tr><td>$key</td><td>" . displayvar($val) . "</td></tr>";
  440. }
  441. }
  442. if (isset($$vname_post)) {
  443. $s .= "<tr><td colspan=2><h4>POSTed Vars</h4></td></tr>";
  444. reset($$vname_post);
  445. while (list($key, $val) = each($$vname_post)) {
  446. $s .= "<tr><td>$key</td><td>" . displayvar($val) . "</td></tr>";
  447. }
  448. }
  449. if (isset($GLOBALS)) {
  450. $s .= "<tr><td colspan=2><h4>Globals</h4></td></tr>";
  451. reset($GLOBALS);
  452. while (list($key, $val) = each($GLOBALS)) {
  453. $s .= "<tr><td>$key</td><td>" . displayvar($val) . "</td></tr>";
  454. }
  455. }
  456. $s .= "</table>";
  457. $s .= "</p>";
  458. $this->content .= $s;
  459. }
  460. // Profiler output..
  461. if ($this->profiling()) {
  462. $s ="<p>";
  463. $s .= "<table border=1 cellpadding=2 cellspacing=0>\n";
  464. $s .= "<tr><td colspan=6><h4>Profiler</h4></td></tr>\n";
  465. $s .= "<tr>";
  466. $s .= "<td>Label</td>";
  467. $s .= "<td align=center>Calls</td>";
  468. $s .= "<td align=center>mS/Call</td>";
  469. $s .= "<td align=center>mS</td>";
  470. $s .= "<td align=right>Rel%</td>";
  471. $s .= "<td>&nbsp</td>";
  472. $s .= "</tr>\n";
  473.  
  474. // Make sure all profiles are finished..
  475. $this->profile_close();
  476.  
  477. // Find max value first..
  478. $maxms = 0;
  479. $profiles = array();
  480. $calls = array();
  481. foreach ($this->profile as $label => $prof) {
  482. $bits = explode("|", $prof);
  483. if (isset($bits[0])) {
  484. $ms = (float) $bits[0];
  485. $profiles[$label] = $ms;
  486. if ($ms > $maxms) $maxms = $ms;
  487. $calls[$label] = $bits[3];
  488. }
  489. }
  490. $maxwidth = 50;
  491. if ($maxms > 0) {
  492. $scalefactor = $maxwidth / $maxms;
  493. }
  494. else {
  495. $scalefactor = 1;
  496. }
  497. //arsort($profiles);
  498. foreach ($profiles as $label => $ms) {
  499. $calltot = $calls[$label];
  500. $mspercall = number_format($ms/$calltot, 1);
  501. $width = (int) ceil($scalefactor * $ms);
  502. $pct = number_format(($ms / $maxms) * 100, 1);
  503. $bar = str_repeat("*", $width);
  504. $ms = number_format($ms, 2);
  505. $s .= "<tr>";
  506. $s .= "<td>$label</td>";
  507. $s .= "<td align=right>$calltot</td>";
  508. $s .= "<td align=right>$mspercall</td>";
  509. $s .= "<td align=right>$ms</td>";
  510. $s .= "<td align=right>$pct</td>";
  511. $s .= "<td valign=middle>$bar</td>";
  512. $s .= "</tr>\n";
  513. }
  514. $s .= "</table>\n";
  515. $s .= "</p>";
  516. $this->content .= $s;
  517. }
  518. return $header . $this->content . $footer;
  519. }
  520. // ................................................................
  521. /**
  522. * Use render() to render the debugger output.
  523. * Renders the debugger output content as WML.
  524. * @see render()
  525. * @return string WML rendering of debug output content
  526. */
  527. function wml() {
  528. if (!$this->debug_hascontent()) return "";
  529.  
  530. // Get names of globals we will use. These depend on the
  531. // version of Php, since we won't rely on the old ones
  532. // sticking around forever..
  533. if (version_compare(phpversion(), "4.1.0", "ge")) {
  534. $vname_get = "_GET";
  535. $vname_post = "_POST";
  536. }
  537. else {
  538. $vname_get = "HTTP_GET_VARS";
  539. $vname_post = "HTTP_POST_VARS";
  540. }
  541. global $$vname_get;
  542. global $$vname_post;
  543. global $GLOBALS;
  544. $s = "";
  545. if ($this->class & DBG_DUMP) {
  546. $s .= "<b>Variables</b><br/>";
  547. if (isset($$vname_get)) {
  548. $s .= "GET Vars:<br/>";
  549. reset($$vname_get);
  550. while (list($key, $val) = each($$vname_get)) {
  551. $s .= "$key=" . displayvar($val) . "<br/>";
  552. }
  553. }
  554. if (isset($$vname_post)) {
  555. $s .= "POSTed Vars:<br/>";
  556. reset($$vname_post);
  557. while (list($key, $val) = each($$vname_post)) {
  558. $s .= "$key=" . displayvar($val) . "<br/>";
  559. }
  560. }
  561. if (isset($GLOBALS)) {
  562. $s .= "Globals:<br/>";
  563. reset($GLOBALS);
  564. while (list($key, $val) = each($GLOBALS)) {
  565. $s .= "$key=" . displayvar($val) . "<br/>";
  566. }
  567. }
  568. }
  569. // Massage content to make it vanilla WML compliant..
  570. $content = $this->content . $s;
  571. $content = str_replace("<br>", "<br/>", $content);
  572. $content = strip_tags($content, "<br/><p><b><small>");
  573. $this->content = $content;
  574. return $this->content;
  575. }
  576. // ................................................................
  577. /**
  578. * Send debug output to a file.
  579. * @param string $name Filename to put output into
  580. * @param string $dir Directory/path to put file into
  581. */
  582. function send_to_file($name, $dir="") {
  583. if (!$this->debug_hascontent()) return;
  584. $s = $this->render();
  585. $f = new outputfile($name, $dir);
  586. if ($f->opened) {
  587. $f->write($this->content);
  588. $f->closefile();
  589. }
  590. }
  591. } // webdebugger class
  592. // ------------------------------------------------------------------
  593. // FUNCTIONS DEBUGGER INTERFACE
  594. // Debugging functions. These are just an interface
  595. // to our debugger for the client..
  596. // ------------------------------------------------------------------
  597.  
  598. /**
  599. * Add content to debug content
  600. * Adds the string content to the debugger output. This is done
  601. * in raw fashion without any <br> or linefeed chars appended.
  602. * The output will then appear as per the output settings.
  603. * @see debugbr()
  604. * @param string $content Content to add to debug output buffer
  605. * @param integer $class Class of output
  606. */
  607. function debug($content, $class=DBG_DIAGNOSTIC) {
  608. global $RESPONSE;
  609. if (isset($RESPONSE)) {
  610. $RESPONSE->debugger->debug_write($content, $class);
  611. }
  612. }
  613. // ..................................................................
  614. /**
  615. * Add content to debug content with <br>
  616. * Adds the string content to the debugger output and appends
  617. * <br> to it. This is intended for output to HTML pages.
  618. * @see debug()
  619. * @param string $content Content to add to debug output buffer
  620. * @param integer $class Class of output
  621. */
  622. function debugbr($content, $class=DBG_DIAGNOSTIC) {
  623. debug($content . "<br>", $class);
  624. }
  625. // ..................................................................
  626. /**
  627. * Add string to debug content as hexdump
  628. * Add string content to output as a hexdump. This is for
  629. * string data only. use this when you want to see 'inside'
  630. * a string variable and view the characters as hexadecimal
  631. * highlighted according to hex range. Chars are highlighted
  632. * as follows:
  633. * ASCII value = 32 (Spaces) ......... Blue
  634. * ASCII values < 32 (Control chars) .. Red
  635. * ASCII values > 127 ................. Green
  636. * @param string $str String to view as hex
  637. * @param string $msg Message to title the dump with
  638. * @param integer $class Class of output
  639. */
  640. function debug_hex($str, $msg="", $class=DBG_DIAGNOSTIC) {
  641. if (is_string($str)) {
  642. if ($msg != "") {
  643. debugbr($msg, $class);
  644. }
  645. debug("<pre>", $class);
  646. for ($i=0; $i < strlen($str); $i++) {
  647. $c = ord(substr($str, $i, 1));
  648. $xc = sprintf("%02x", $c);
  649. if ($c == 32) $xc = "<font color=blue>$xc</font>";
  650. elseif ($c < 32) $xc = "<font color=red>$xc</font>";
  651. elseif ($c > 127) $xc = "<font color=green>$xc</font>";
  652. debug("$xc|", $class);
  653. if (($i + 1) % 16 == 0) debugbr("", $class);
  654. }
  655. debug("</pre><br>", $class);
  656. }
  657. }
  658. // ..................................................................
  659. /**
  660. * Set debugging class(es)
  661. * Sets the class or classes (OR'ed together) of debug output
  662. * which will be accepted/rendered on ouput.
  663. * @param integer $class Class of output
  664. */
  665. function debug_class($class="") {
  666. global $RESPONSE;
  667. $currentclass = DBG_UNDEFINED;
  668. if (isset($RESPONSE)) {
  669. if ($class != "") {
  670. $RESPONSE->debugger->debug_class($class);
  671. }
  672. $currentclass = $RESPONSE->debugger->class;
  673. }
  674. // Return current class setting..
  675. return $currentclass;
  676. }
  677. // ..................................................................
  678. /**
  679. * Set debugging on
  680. * Sets the debugging on. Sets class(es) to accept.
  681. * @param integer $class Class of output
  682. */
  683. function debug_on($class=DBG_DEFAULT) {
  684. global $RESPONSE;
  685. if (isset($RESPONSE)) {
  686. $RESPONSE->debugger->debug_mode(ON, $class);
  687. }
  688. }
  689. // ..................................................................
  690. /**
  691. * Set debugging off
  692. */
  693. function debug_off() {
  694. global $RESPONSE;
  695. if (isset($RESPONSE)) {
  696. $RESPONSE->debugger->debug_mode(OFF);
  697. }
  698. }
  699. // ..................................................................
  700. /**
  701. * Sets flag for debugger to include global vars in output.
  702. */
  703. // Causes page vars to be dumped..
  704. function debug_dump() {
  705. global $RESPONSE;
  706. if (isset($RESPONSE)) {
  707. $RESPONSE->debugger->debug_dump();
  708. }
  709. }
  710. // ..................................................................
  711. /**
  712. * Render the debug output as a string.
  713. * Normally debug output is taken care of by the system, however
  714. * you might need to get hold of the output for some reason, and
  715. * this is the function to do it.
  716. * @return string Debugger content as a string
  717. */
  718. function debug_render() {
  719. global $RESPONSE;
  720. if (isset($RESPONSE) && debugging()) {
  721. return $RESPONSE->debugger->render();
  722. }
  723. else return "";
  724. }
  725. // ..................................................................
  726. /**
  727. * Return debugger status
  728. * Function for external routines to determine
  729. * whether debugging is enabled or not..
  730. * @return boolean True if debugging is enabled
  731. */
  732. function debugging() {
  733. global $RESPONSE;
  734. if (isset($RESPONSE)) {
  735. return $RESPONSE->debugger->enabled;
  736. }
  737. }
  738. // ..................................................................
  739. /**
  740. * Set debugger output mode
  741. * This function allows setting of the debugger output
  742. * mode which determines where output goes.
  743. * @param integer $mode The debugger output mode
  744. */
  745. function debug_output($mode=DBG_O_DEFAULT) {
  746. global $RESPONSE;
  747. if (isset($RESPONSE)) {
  748. $RESPONSE->debugger->debug_output($mode);
  749. }
  750. }
  751. // ..................................................................
  752. /**
  753. * Calls Php phpinfo() function
  754. */
  755. function debug_phpinfo() {
  756. phpinfo(INFO_GENERAL|INFO_ENVIRONMENT|INFO_VARIABLES);
  757. }
  758. // ..................................................................
  759. /**
  760. * Display a variable nicely
  761. * Variables might be other than simple scalars. This function
  762. * is used internally by the debugger to make sure we show
  763. * them off in their best light.
  764. * @param mixed $var The variable to show off
  765. */
  766. function displayvar($var) {
  767. $s = "";
  768. if (is_array($var)) {
  769. foreach ($var as $elem) {
  770. $s .= "[$elem] ";
  771. }
  772. }
  773. elseif (is_bool($var)) {
  774. if ($var === true) $s .= "true";
  775. else $s .= "false";
  776. }
  777. else $s .= $var;
  778. return $s;
  779. }
  780. // ..................................................................
  781. /**
  782. * Insert an entry into the profiler. This label will have a line in
  783. * the profile output against the time elapsed since the last line.
  784. */
  785. function debug_profile($label) {
  786. global $RESPONSE;
  787. if (isset($RESPONSE) && $RESPONSE->debugger->profiling()) {
  788. $RESPONSE->debugger->profile($label);
  789. }
  790. }
  791. // ..................................................................
  792. /** Returns true if the RESPONSE is in profiling mode */
  793. debug_profiling() {
  794. global $RESPONSE;
  795. return ( isset($RESPONSE) && $RESPONSE->debugger->profiling() );
  796. }
  797. // ..................................................................
  798. /**
  799. * DEBUG TRACEBACK
  800. * Usage: In your function you bracket the statements you want to
  801. * label for tracing as in the following example..
  802. * function thing() {
  803. * debug_trace("mymodule"); // pushes "mymodule" on trace stack
  804. * ...blah blah // program statements
  805. * debug_trace(); // pops current trace off trace stack
  806. * }
  807. * NOTE: You can also use the object identifier for a class instead
  808. * of a simple string like "mymodule". For example:
  809. * function thing() {
  810. * debug_trace($this); // pushes name of object class on trace stack
  811. * ...blah blah // program statements
  812. * debug_trace(); // pops current trace off trace stack
  813. * }
  814. * @param mixed $traceobj Optional object trace is called in
  815. */
  816. function debug_trace($traceobj="") {
  817. global $RESPONSE;
  818. if (isset($RESPONSE)) {
  819. if ($traceobj == "") {
  820. return $RESPONSE->debugger->poptrace();
  821. }
  822. else {
  823. return $RESPONSE->debugger->pushtrace($traceobj);
  824. }
  825. }
  826. }
  827.  
  828. // ------------------------------------------------------------------
  829. ?>

Documentation generated by phpDocumentor 1.3.0RC3