Klasa do obsługi błędów, pochodząca z mojego engine (Valhalla).
Co robimy: tworzymy plik logs.txt w katalogu głównym i nadajemy mu chmod 777.
<?php
define('ERROR_FILE', './logs.txt');
class Error
{
/**
* konstruktor
* @param string $msg tresc bledu
* @param string $file plik z bledem
* @param string $line linia z bledem
*/
public function __construct($msg, $file, $line)
{
$this -> add2log($msg);
echo '<b>Error on line:</b> '; $this -> showSource($file, $line, $prev = 0, $next = 0);
echo '<b>Kernel Says:</b> '. $msg .' ';
}
/**
* Metoda dodaje informacje o bledzie do logow
* @param string $text tresc bledu
*/
public function add2log($text)
{
$fp = fopen(ERROR_FILE
, \"r\");
$text = \"n\".date('H:i d.m.Y').\" - \". $text;
$fp = fopen(ERROR_FILE
, \"a\"); }
/**
* pobiera logi z pliku
* @param void
*/
public function getlogs()
{
$file = fopen(ERROR_FILE
, \"r\");
while($line = fgets($file, 81
)) {
}
}
/**
* @access: public
* @version 2.0.0
* @license BSD
* @copyright (c) 2003,2004 Cezary Tomczak
* @link http://gosu.pl/software/mygosulib.html
*/
public function showSource($file, $line, $prev = 10000, $next = 10000)
{
if($dir[0] == '..') return false;
{
return trigger_error(\"showSource() failed, file does not exist `$file`\", E_USER_ERROR); return false;
}
$count = count($data) - 1;
$start = $line - $prev;
if ($start < 1)
{
$start = 1;
}
$end = $line + $next;
if ($end > $count) {
$end = $count + 1;
}
$highlight_default = ini_get('highlight.default');
echo '<table cellspacing=\"0\" cellpadding=\"0\"><tr>'; echo '<td style=\"vertical-align: top;\"><code style=\"background-color: black; color: white;\">';
for ($x = $start; $x <= $end; $x++)
{
echo '<a name=\"'.$x.'\"></a>'; echo ($line == $x ?
'<font style=\"background-color: black; color: white;\">' : ''); echo ($line == $x ?
'</font>' : ''); }
echo '</code></td><td style=\"vertical-align: top;\"><code>'; while ($start <= $end)
{
echo ' ' . $data[$start - 1] . '<br />'; ++$start;
}
}
}
?>
Przykład użycia:
<?php
include('Error.php');
echo new Error
('tresc bledu !', __FILE__, __LINE__); ?>
Edit: wersja skrócona, na dysku leży wersja z set_error_handler
Ten post edytował Ociu 8.05.2005, 08:45:24