<?php
include_once('PEAR.php');
class iowacore2_error
{
private $message;
private $code;
private $line;
private $file;
public function __construct($code = '', $message = '', $file = '', $line = '')
{
$this->code = (int) $code;
$this->message = $message;
$this->file = $file;
$this->line = (int) $line;
}
// get formatted time
private function getTime()
{
return date('F j, Y, g:i a: ');
}
// converts error code to string
private function error2string($value)
{
$level_names = array(
E_ERROR => 'E_ERROR', E_WARNING => 'E_WARNING',
E_PARSE => 'E_PARSE', E_NOTICE => 'E_NOTICE',
E_CORE_ERROR => 'E_CORE_ERROR', E_CORE_WARNING => 'E_CORE_WARNING',
E_COMPILE_ERROR => 'E_COMPILE_ERROR', E_COMPILE_WARNING => 'E_COMPILE_WARNING',
E_USER_ERROR => 'E_USER_ERROR', E_USER_WARNING => 'E_USER_WARNING',
E_USER_NOTICE => 'E_USER_NOTICE');
if (defined('E_STRICT'))
$level_names[E_STRICT] = 'E_STRICT';
$levels = array();
if (($value & E_ALL) == E_ALL)
{
$levels[] = 'E_ALL';
$value &= ~E_ALL;
}
foreach($level_names as $level=>$name)
if (($value & $level) == $level) $levels[] = $name;
return implode(' | ', $levels);
}
// format error message
public function getError()
{
return 'Error in "' . $this->file . '" on line ' . $this->line . ': ' . $this->message;
}
public function displayError($error = null)
{
$error = is_null($error) ? $this->getError() : $error;
if (!preg_match('~(.*)MDB2 Error: connect failed~', $error) && !preg_match('~(.*)mysql_connect\(\)(.*)~', $error))
{
echo $error;
}
}
// write error to log file
public function writeToLog($message = null)
{
if ((int) $this->code < 1 || $this->code <= ERROR_LOG_LEVEL)
{
$log_file = is_writable(IOWACORE2_BASE_PATH_TMP . '/iowacore2.log') ? IOWACORE2_BASE_PATH_TMP . '/iowacore2.log' : 'iowacore2.log';
$message = self::getTime() . (is_null($message) ? $this->getError() . ' (' . $this->error2string($this->code) . ')' : $message) . "\n";
file_put_contents($log_file, $message, FILE_APPEND);
return true;
} else {
return false;
}
}
// these functions are for PEAR_Error
public function errorCallback($err)
{
$this->displayError($err->getMessage());
$this->pearLog($err->getMessage());
}
public function pearLog($error)
{
$this->writeToLog('PEAR Error: ' . $error);
}
}