Location: PHPKode > projects > OpenNitro > trunk/Nitro/RuntimeDebug.inc.php
<?php
//
// +---------------------------------------------------------------------------+
// | Nitro :: RuntimeDebug                                                     |
// +---------------------------------------------------------------------------+
// | Copyright (c) 2003 June Systems BV                                        |
// +---------------------------------------------------------------------------+
// | This library is free software; you can redistribute it and/or modify it   |
// | under the terms of the GNU Lesser General Public License as published by  |
// | the Free Software Foundation; either version 2.1 of the License, or (at   |
// | your option) any later version.                                           |
// |                                                                           |
// | This library is distributed in the hope that it will be useful, but       |
// | WITHOUT ANY WARRANTY; without even the implied warranty of                |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser   |
// | General Public License for more details.                                  |
// |                                                                           |
// | You should have received a copy of the GNU Lesser General Public License  |
// | along with this library; if not, write to the Free Software Foundation,   |
// | Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA            |
// +---------------------------------------------------------------------------+
// | Authors: Siggi Oskarsson <hide@address.com>                          |
// +---------------------------------------------------------------------------+
//
// $Id: RuntimeDebug.inc.php 229 2008-04-17 09:20:31Z oli $
//
// Nitro runtime debug class
//

/**
 * This file contains the Nitro runtime debug function
 *
 * @package	Nitro
 * @subpackage	Debug
 * @author 		Siggi Oskarsson
 * @version 	$Revision: 1.9 $
 * @copyright	2004 June Systems BV
 */

$_runtime_debug_port_closed = FALSE;

/**
 * Runtime debug function
 *
 * This function is used in conjunction with the debuglistener to
 * view debug messages via a console.
 *
 * @param	string	$DebugMessage	Debug message to display
 * @param	int	$DebugLevel	Debug level of message
 * @param	int	$Level	Hierarchical level of message
 * @access	public
 */
function RuntimeDebug($DebugMessage, $DebugLevel = DEBUG_NONE, $Level = 0, $durationMessage = NULL, $color = FALSE)
{
	if (NITRO_RUNTIME_DEBUG) {
		global $__NitroConf, $_debug_sock, $_runtime_debug_timer_start, $_runtime_debug_timer_prev, $_runtime_debug_port_closed;
	
		if (!$_runtime_debug_port_closed) {
			$_debug_level = (array_key_exists('SetDebugLevel', $_COOKIE) ? $_COOKIE["SetDebugLevel"] : eval("return ".$__NitroConf->CONF["Debug"]["debug_level"].";"));
			$_debug_host = (array_key_exists('SetDebugHost', $_COOKIE) ? $_COOKIE["SetDebugHost"] : $__NitroConf->CONF["Debug"]["debug_host"]);
			$_debug_port = (array_key_exists('SetDebugPort', $_COOKIE) ? $_COOKIE["SetDebugPort"] : $__NitroConf->CONF["Debug"]["debug_port"]);
			$_debug_chars = (array_key_exists('SetDebugChars', $_COOKIE) ? $_COOKIE["SetDebugChars"] : $__NitroConf->CONF["Debug"]["debug_chars"]);
			$_debug_output_level = (array_key_exists('SetMaxOutputLevel', $_COOKIE) ? $_COOKIE["SetMaxOutputLevel"] : $__NitroConf->CONF["Debug"]["debug_output_level"]);

			if (!$_debug_level) $_debug_level = DEBUG_NONE;
			if (!$_debug_port) $_debug_port = 12500;
			if (!$_debug_host) $_debug_host = "127.0.0.1";
			if (!$_debug_chars) $_debug_chars = 160;
			if (!$_debug_output_level) $_debug_output_level = FALSE;
		
			$now = NitroGetMicroTime();
			if (!$_runtime_debug_timer_start) $_runtime_debug_timer_start = $now;
			if (!$_runtime_debug_timer_prev) $_runtime_debug_timer_prev[$Level] = $now;

			if (($DebugLevel & $_debug_level)!=0 && ($_debug_output_level === FALSE OR $_debug_output_level >= $Level)) {
		
				if (!$_debug_sock) $_debug_sock = @fsockopen($_debug_host, $_debug_port, $errno, $errstr, 1);
				if ($_debug_sock) {
					$DebugMessage = substr(preg_replace("/\s+/", ' ', $DebugMessage), 0, $_debug_chars - $Level*3);
					$tmp = $DebugMessage;
					$tmp = ereg_replace("\033\[[0-9]+m?;[0-9]+m?(;[0-9]+m?)?", '', $tmp); // remove set codes
					$tmp = ereg_replace("\033\[0m", '', $tmp); // remove reset codes
					$mlength = strlen($tmp);
					//$mlength = strlen($DebugMessage);
	
					if ($color) {
						$DebugMessage = $color.$DebugMessage;
						if (!ereg("\033\[0m", $color)) {
							$DebugMessage.= "\033\[0m";
						} else {
							$mlength += 3;
						}
					}
	
					$DebugMessage.= str_repeat(' ', (int)abs($_debug_chars - $mlength - $Level*3));
					
	
					//terminal color expl:
					//Startcode: \033[40;37;1m
					//Resetcode: \033[0m
					//for codes see http://www-106.ibm.com/developerworks/library/l-tip-prompt/
					$timer = sprintf('% 8.5f', (($now - $_runtime_debug_timer_start))).'s';
					if (isset($durationMessage)) {
						$duration = ' '.$durationMessage;
					} else {
						if ($now - $_runtime_debug_timer_prev[$Level] == 0) {
							$duration = "\n\n\033[42;37;1m ====== Debug started at ".date('Y-m-d H:i:s')." ====== \033[0m";
						} else {
							$duration = sprintf('% 8.3f', (($now - $_runtime_debug_timer_prev[$Level]) * 1000)).'ms';
							$duration = " [\033[".($duration > 1000 ? '41' : '40').";33m".$duration."\033[0m]";
						}
					}
					$timerinfo = "[\033[40;37;1m".$timer."\033[0m] ";
					fputs($_debug_sock, $duration."\n".$timerinfo.str_repeat("\033[32m".chr(179)."\033[0m  ", $Level).$DebugMessage);
				$_runtime_debug_timer_prev[$Level] = $now;
				} else {
					// Stop runtime debugging, the port is closed!
					$_runtime_debug_port_closed = TRUE;
				}
			}
		}
	}
}
?>
Return current item: OpenNitro