<?php

define('LENSDIE',1);

// Die with error reporting
function LensDie($errMsg,$cat1=' ',$severity=LENSWARNING,$cat2=' ',$debugInfo=' ')
{
	$cat = $cat1 . (($cat2) ? "/$cat2" : '');
	if ($cat) $cat .= ': ';
	if (error_reporting() & E_ERROR) print "<p>$cat$errMsg ($severity) $debugInfo</p>";
	if ($severity>LENSWARNING) LensLog($errMsg,$cat1,$severity,$cat2,$debugInfo);
	die();
}

// Log error message
function LensLog($errMsg,$cat1=' ',$severity=LENSWARNING,$cat2=' ',$debugInfo='')
{
global $DB,$PROCAT;
global $HTTP_SERVER_VARS;

	$PHP_SELF = $HTTP_SERVER_VARS['PHP_SELF'];
	$HTTP_HOST = $HTTP_SERVER_VARS['HTTP_HOST'];
	
	if (!empty($cat1) and trim($cat1) and isset($PROCAT)) $cat1 = $PROCAT;
	
	$who = $HTTP_HOST;
	@$who .= ' from '.$HTTP_SERVER_VARS['REMOTE_ADDR'];
	$who .= ":/$PHP_SELF";
	@$who .= '?'.$HTTP_SERVER_VARS["QUERY_STRING"];
	
	$msg = "$errMsg ($severity)";
	error_log("$who: $msg");	

	if ($severity>LENSWARNING && !empty($DB)) {
		$t = time();
		$msg = $DB->qstr($msg);
		$debugInfo = $DB->qstr($debugInfo);
		$who = $DB->qstr($who);
		$cat1 = $DB->qstr($cat1);
		$cat2 = $DB->qstr($cat2);
		$DB->debug = true;
		$rs = $DB->Execute("insert into lens_log (when,server,cat1,cat2,msg,severity,debug) values($t,$who,$cat1,$cat2,$msg,$severity,$debugInfo)");
		if (!$rs) {
			// how do we escalate if database down?
		}
	}
}

?>