Login   Register  
PHP Classes
elePHPant
Icontem

File: debug.inc.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of David Phillips  >  debug  >  debug.inc.php  >  Download  
File: debug.inc.php
Role: ???
Content type: text/plain
Description: Debug class
Class: debug
Debugging class with log file or screen output
Author: By
Last change:
Date: 2000-10-08 03:17
Size: 5,856 bytes
 

Contents

Class file image Download
<?php
/*
	debug.inc.php -- debugging class for PHP

	usage:
		debug (
			$logfile
			$halt = "NO" | "yes"
			$show = "BOTH" | "file" | "screen" | "off"
		)
		debug->report ($message, __LINE__, __FILE__, {$var_name, $var_value}...);

	(c) Copyleft 2000 eBoing, Inc.
	David Phillips

	$Id: debug.inc.php,v 0.1 2000/10/08 01:13:00 $
*/ 
class debug {

	var $classname = "debug";

	function debug ($logfile = "", $halt = "no", $show = "both") {
	/*
		public: Constructor
		logfile: optional file in which to record messages
		halt: optional setting for halt action
			 = "NO" | "yes"
		show: optional setting for where (and whether) to output debug->report calls
			 = "BOTH" | "file" | "screen" | "off"
	*/
		$this->set_log ($logfile);
		$this->set_halt ($halt);
		$this->set_show ($show);
	}

	function set_log ($logfile) {
	/*
		public: set_log (filename $logfile)
		logfile: optional file for logging error messages
	*/
		if (empty ($logfile)) {
			$this->logfile = "";
		} else {
			$this->logfile = $logfile;
			$f = fopen ($this->logfile, 'a'); // check whether we can use the file
			if (!$f) {
				printf ("<b>Debug:</b> unable to open %s as log file<br>\n", $this->logfile);
				return false;
			}
		}
		return true;
	}

	function set_halt ($halt = "no") {
	/*
		public: set_halt (string $halt)
		halt: optional setting for halt action
			"no" => report error, continue; default
			"yes" => report error, halt
	*/
		$this->halt = strtolower ($halt);
	}

	function set_show ($show) {
	/*
		public: set_show (string $show)
		show: optional setting for where (and whether) to output debug->report calls
			= "BOTH" | "file" | "screen" | "off"
	*/
		$this->show = strtolower ($show);
	}

	function report ($msg = "", $line = "", $file = "") {
	/*
		public: report ($msg, $line, $file, {$var_name, $var_value}...)
		$msg: debug message to show
		$line: optional line number (use __LINE__)
		$file: optional executing file name (use __FILE__)
		Optional repeating pairs {
			$var_name
			$var_value
		}
	*/
		static $debug_calls = 0; // track how many times we've been called

		$debug_calls++;
		if ($this->show == "off") return;

		$args = func_get_args (); // get an array of all the function arguments we were called with
		$n_args = count ($args) - 1; // and a count of all but one (the message)

		if ($this->show == "both" or $this->show == "screen") {
			$s = "<p><hr>\n<b>Debug</b> $debug_calls: ";
			$l = $line != "" ? "on line <b>$line</b>: " : "";
			$f = $file != "" ? "in file <b>$file</b>, " : "";
			echo "$s $f $l $msg <br>\n";
			if ($n_args % 2) { // remaining arguments are unpaired (n/2 != 0)
				echo "&nbsp; &nbsp; usage: \$debug->report (\$msg, \$line, \$file, {\$var, \$value}...)<br>\n";
			} else { // paired arguments (n/2 = 0)
				for ($i = 3; $i < $n_args; $i += 2) {
					echo "&nbsp; &nbsp; \$$args[$i]: " . $args[$i + 1];
					echo " ... (" . gettype ($args[$i + 1]) . ")<br>\n";
				}
			}
			echo "<hr><p></p>\n";
		} // show screen
		
		if ($this->show == "both" or $this->show == "file") {
			// Repeat for logfile output.
			if (!empty ($this->logfile)) {
				$fh = fopen ($this->logfile, 'a'); // append to log file
				if ($fh) {
					$s = "Debug $debug_calls: ";
					$l = $line != "" ? "on line $line: " : "";
					$f = $file != "" ? "in file $file, " : "";
					fputs ($fh, "$s$f$l$msg\n");
					if ($n_args % 2) {
						fputs ($fh, "  usage: \$debug->report (\$msg, \$line, \$file, {\$var, \$value}...)\n");
					} else {
						for ($i = 3; $i < $n_args; $i += 2) {
							fputs ($fh, "  \$$args[$i]: " . $args[$i + 1]);
							fputs ($fh, " ... (" . gettype ($args[$i + 1]) . ")\n");
						}
					}
					fclose ($fh);
				}
			} // logfile
		} // show file
		
		if ($this->halt == "yes") die ("<b>Halt</b>");

	} // report
	
	// Deprecated :)
	function do_report ($msg, $var, $value = "") {
	/*
		public, override: do_report ($msg)
		msg: error message to show
		var: variable name to show
		value: variable value to show
	*/
		printf ("<b>Debug:</b> %s<br>\n", $msg);
		if (is_array ($var)) {
			reset ($var);
			while (list ($k, $v) = each ($var)) {
				if (!empty ($k))
					printf ("key: %s = value: %s<br>\n", $k, $v);
			}
		} else {
			if (!empty ($var)) printf ("%s = %s<br>\n", $var, $value);
		}

		// Repeat for logfile output.
		if (!empty ($this->logfile)) {
			$f = fopen ($this->logfile, 'a'); // append to log file
			if ($f) {
				fputs ($f, $msg);

				if (is_array ($var)) {
					reset ($var);
					while (list ($k, $v) = each ($var)) {
						if (!empty ($k))
							fputs ($f, "key: $k = value: $v<br>\n");
					}
				} else {
					if (!empty ($var)) fputs ($f, "$var = $value<br>\n");
				}

				fclose ($f);
			}
		}
	} // do_report

} // debug.inc.php
?>

<?php
require "./include/debug.inc.php";

$dbg = new debug ("foo.txt");
$dbg->report ();
$dbg->report ("Zeroth message");
$dbg->report ("First message", __LINE__);
$dbg->report ("Second message", __LINE__, __FILE__);
$test = 55;
$dbg->report ("Third message", __LINE__, __FILE__, "test", $test);
$test = 66;
$test2 = $test / 2;
$dbg->report ("Fourth message", __LINE__, __FILE__, "test", $test, "test2", $test2);
$dbg->report ("Fifth message", __LINE__, __FILE__, "test", $test, "test2");
$dbg->set_show ("File");
$test3 = "Brillig";
$dbg->report ("Sixth message, show=file", __LINE__, __FILE__, "test3", $test3);
$dbg->set_show ("SCREEN");
$dbg->report ("Seventh message, show=screen");
$dbg->set_show ("off");
$dbg->report ("Eighth message, show=off");
$dbg->set_show ("both");
$dbg->report ("Nineth message, show=both");
$dbg->set_halt ("yes");
$dbg->report ("Tenth message, halt=yes");
$dbg->report ("Eleventh message");
?>