Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Error Handler, Klasa, PHP 5
Ociu
post
Post #1





Grupa: Moderatorzy
Postów: 1 566
Pomógł: 37
Dołączył: 14.05.2003
Skąd: Kraków




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.

  1. <?php
  2. define('ERROR_FILE', './logs.txt');
  3.  
  4. class Error
  5. {
  6.  
  7.  /**
  8. * konstruktor
  9. * @param string $msg tresc bledu
  10. * @param string $file plik z bledem
  11. * @param string $line linia z bledem
  12. */ 
  13. public function __construct($msg, $file, $line)
  14. {
  15. if($msg == null) die();
  16. $this -> add2log($msg);
  17. echo '<b>Error on line:</b> ';
  18. $this -> showSource($file, $line, $prev = 0, $next = 0);
  19. echo '<b>Kernel Says:</b> '. $msg .' ';
  20.  
  21. }
  22.  
  23.  /**
  24. * Metoda dodaje informacje o bledzie do logow
  25. * @param string $text tresc bledu
  26. */ 
  27. public function add2log($text)
  28. {
  29. if(!file_exists(ERROR_FILE)) die ('File logs.txt does not exists');
  30.  
  31. $fp = fopen(ERROR_FILE, &#092;"r\");
  32. $oldErrors = fread($fp, filesize(ERROR_FILE));
  33. fclose($fp);
  34.  
  35. $text = &#092;"n\".date('H:i d.m.Y').\" - \". $text;
  36. $fp = fopen(ERROR_FILE, &#092;"a\");
  37. fputs($fp, $text);
  38. fclose($fp);
  39. }
  40.  
  41.  /**
  42. * pobiera logi z pliku
  43. * @param void
  44. */ 
  45. public function getlogs()
  46. {
  47. $file = fopen(ERROR_FILE, &#092;"r\");
  48. flock($file, 1);
  49.  
  50. while($line = fgets($file, 81))
  51. {
  52. echo $line. '<br />';
  53. }
  54.  
  55. flock($file, 3);
  56. fclose($file);
  57. }
  58.  
  59.  /**
  60. * @access: public
  61. * @version 2.0.0
  62. * @license BSD
  63. * @copyright (c) 2003,2004 Cezary Tomczak
  64. * @link http://gosu.pl/software/mygosulib.html
  65. */
  66.  
  67. public function showSource($file, $line, $prev = 10000, $next = 10000)
  68. {
  69.  
  70. $dir = explode(&#092;"/\", $file);
  71. if($dir[0] == '..') return false;
  72. if (!(file_exists($file) && is_file($file)))
  73. {
  74. return trigger_error(&#092;"showSource() failed, file does not exist `$file`\", E_USER_ERROR);
  75. return false;
  76. }
  77.  
  78. $data = ob_get_contents();
  79.  
  80. $data = explode('<br />', $data);
  81. $count = count($data) - 1;
  82.  
  83. $start = $line - $prev;
  84. if ($start < 1)
  85. {
  86. $start = 1;
  87. }
  88. $end = $line + $next;
  89. if ($end > $count) {
  90. $end = $count + 1;
  91. }
  92.  
  93. $highlight_default = ini_get('highlight.default');
  94.  
  95. echo '<table cellspacing=\"0\" cellpadding=\"0\"><tr>';
  96. echo '<td style=\"vertical-align: top;\"><code style=\"background-color: black; color: white;\">';
  97.  
  98. for ($x = $start; $x <= $end; $x++)
  99. {
  100. echo '<a name=\"'.$x.'\"></a>';
  101. echo ($line == $x ? '<font style=\"background-color: black; color: white;\">' : '');
  102. echo str_repeat('&nbsp;', (strlen($end) - strlen($x)) + 1);
  103. echo $x;
  104. echo '&nbsp;';
  105. echo ($line == $x ? '</font>' : '');
  106. echo '<br />';
  107. }
  108. echo '</code></td><td style=\"vertical-align: top;\"><code>';
  109. while ($start <= $end)
  110. {
  111. echo '&nbsp;' . $data[$start - 1] . '<br />';
  112. ++$start;
  113. }
  114. echo '</code></td>';
  115. echo '</tr></table>';
  116.  
  117.  
  118.  
  119. }
  120. } 
  121. ?>


Przykład użycia:
  1. <?php
  2. include('Error.php');
  3. echo new Error('tresc bledu !', __FILE__, __LINE__);
  4. ?>


Edit: wersja skrócona, na dysku leży wersja z set_error_handler winksmiley.jpg

Ten post edytował Ociu 8.05.2005, 08:45:24
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 20.08.2025 - 17:39