Skip to content

Commit 840774e

Browse files
committedJun 17, 2017
old error handling lib
yeay it still works XD
1 parent f67c9aa commit 840774e

File tree

3 files changed

+259
-0
lines changed

3 files changed

+259
-0
lines changed
 

‎.err.log

Whitespace-only changes.

‎inc/fun_error.php

+255
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,255 @@
1+
<?
2+
/* --------------------------------------------------------- *\
3+
Plik: fun_error
4+
5+
Zbiór funkcji do przetwarzania/obsługiwania błędów.
6+
7+
Użycie
8+
Wywołania błędów
9+
trigger_error (string error_msg, int error_type)
10+
* Dla SQL
11+
trigger_error ('sql:'.$zapytanie, E_USER_ERROR);
12+
* Inne (DEBUG_MODE = 1)
13+
trigger_error ('tekst błędu', E_USER_ERROR);
14+
trigger_error ('tekst błędu', E_USER_WARNING);
15+
trigger_error ('tekst błędu', E_USER_NOTICE);
16+
* Testowe wyswietlanie zawrtosci zmiennych
17+
trigger_error (myVarDump($zmienna), E_USER_NOTICE);
18+
\* --------------------------------------------------------- */
19+
20+
/*
21+
*/
22+
23+
/*
24+
Stałe wewnętrzne
25+
*/
26+
//
27+
// Parametry pliku do wyświetlania
28+
define ('HANDLER_ERR_LOG', './.err.log'); // scieżka
29+
define ('MAX_LOG_SIZE', 4194304); // maksymalna wielkosc (tu 4MB)
30+
//
31+
// Tryb debugowania
32+
define ('DEBUG_MODE', 0); // 1 - ON, 0 - OFF
33+
// Tryb pomocniczy ("chowa" komunikaty)
34+
define ('SILENT_DEBUG_MODE', 0); // 1 - ON, 0 - OFF
35+
//
36+
//define ('PHP_MANUAL_PATH', 'file:///home/ewa/Documents/prog/php-manual/');
37+
ini_set('docref_root', '/test/php_man/pl/');
38+
ini_set('docref_ext', '.html');
39+
40+
/* --------------------------------------------------------- *\
41+
Funkcja: myVarDump
42+
43+
Zwraca lub wyswietla sformatowany HTML-owo
44+
zrzut wartosci zmiennej (także tablicy)
45+
46+
Parametry:
47+
$var - zmienna do wyswietlenia
48+
$return - domyslnie true
49+
true - kod HTML zostanie zwrócony jako wartosc
50+
false - kod HTML zostanie wyświetlony (zwykłe echo)
51+
\* --------------------------------------------------------- */
52+
function myVarDump($var, $return = true)
53+
{
54+
$txt = '<div style="white-space: pre">'. wordwrap(htmlspecialchars(var_export($var,true)),90) .'</div>';
55+
if ($return)
56+
return $txt;
57+
else
58+
echo $txt;
59+
}
60+
61+
/* --------------------------------------------------------- *\
62+
Funkcja: init_myErrorHandler
63+
64+
Inicjowanie obsługi błędów
65+
\* --------------------------------------------------------- */
66+
function init_myErrorHandler()
67+
{
68+
global $debug_msgtext;
69+
$debug_msgtext = '';
70+
error_reporting(E_ALL);
71+
set_error_handler('myErrorHandler');
72+
}
73+
74+
/* --------------------------------------------------------- *\
75+
Funkcja: myErrorHandler
76+
77+
Funkcja do obsługi błędów. Wywołanie opisane wczesniej.
78+
79+
Opis ogólny i parametrów na stronie:
80+
http://pl.php.net/manual/pl/function.set-error-handler.php
81+
82+
Parametry (globalne):
83+
$debug_msgtext - zmienna przechowująca dotychczasowe błędy
84+
"zerowana" przy inicjalizacji (fun. init_myErrorHandler)
85+
\* --------------------------------------------------------- */
86+
function myErrorHandler($errno, $errmsg, $filename, $linenum)
87+
{
88+
global $debug_msgtext;
89+
90+
$done = false;
91+
//
92+
// Special errors handling
93+
//
94+
if ($errno == E_USER_ERROR ||
95+
$errno == E_USER_WARNING ||
96+
$errno == E_USER_NOTICE)
97+
{
98+
//
99+
// Check for prefix
100+
//
101+
if (strpos($errmsg, 'sql:') === 0)
102+
{
103+
substr($errmsg, 4);
104+
$errmsg = myErrorHandler_sql($matches[2], $filename, $linenum);
105+
if ($errno == E_USER_ERROR)
106+
{
107+
printout_html_msg ($errmsg);
108+
}
109+
else
110+
{
111+
$debug_msgtext .= $errmsg;
112+
$done = true;
113+
}
114+
}
115+
}
116+
//
117+
// Standard error handling
118+
//
119+
if (DEBUG_MODE && !$done)
120+
{
121+
$new_err_msg = myErrorHandler_std($errno, $errmsg, $filename, $linenum);
122+
123+
$debug_msgtext .= $new_err_msg;
124+
}
125+
// tylko zapis do pliku
126+
else if (!$done)
127+
{
128+
myErrorHandler_std($errno, $errmsg, $filename, $linenum);
129+
}
130+
}
131+
132+
/* --------------------------------------------------------- *\
133+
Funkcja: myErrorHandler_sql
134+
135+
Zwraca sformatowany HTML-owo kod błędu i zapisuje
136+
poufne dane do pliku (stała: HANDLER_ERR_LOG).
137+
138+
Parametry:
139+
$sql - kod zapytania SQL (które wywołało bład)
140+
$err_file_name - nazwa pliku, w którym wystapił bład
141+
$err_line_num - numer linii pliku, w miejscu wystapienia błędu
142+
\* --------------------------------------------------------- */
143+
function myErrorHandler_sql ($sql, $err_file_name, $err_line_num)
144+
{
145+
global $db;
146+
/*
147+
global $HTTP_COOKIE_VARS;
148+
149+
if ($HTTP_COOKIE_VARS["blad_sql_wystapil"]!=1) {
150+
$err_msg = str_replace('Something is wrong in your syntax', 'Błąd w składni', mysql_error());
151+
152+
$wnetrze_msgtext = "Nieprawidłowe zapytanie: \n". $sql;
153+
$wnetrze_msgtext.= "\n".'Błąd (' . mysql_errno(). '): '. $err_msg;
154+
155+
emalia('egil@wp.pl', "Błąd SQL", $wnetrze_msgtext, 'viking@megapolis.pl');
156+
157+
}
158+
setcookie ("blad_sql_wystapil", 1);
159+
160+
return $wnetrze_msgtext;
161+
*/
162+
$err_log_file = HANDLER_ERR_LOG;
163+
if (@filesize($err_log_file)<1024*1024) {
164+
/* echo "<br>-".filesize($err_log_file)."-<br>"; */
165+
$err_msg = str_replace('Something is wrong in your syntax', 'Błąd w składni', $db->error());
166+
167+
$log_msg = "\n--------------------------------\n ". date('D d.m.Y H:i:s (T)') . "\n $err_line_num - $err_file_name\n--------------------------------\n";
168+
$log_msg .= "Nieprawidłowe zapytanie: \n". $sql;
169+
$log_msg.= "\nBłąd (" . mysql_errno(). "): $err_msg\n";
170+
@error_log ($log_msg, 3, $err_log_file);
171+
}
172+
173+
if (DEBUG_MODE)
174+
{
175+
return '<div><pre>' .htmlspecialchars($log_msg). '</pre></div>';
176+
}
177+
else
178+
{
179+
return '<div class="mymsgdie"><b>Wystąpił błąd bazy danych!</b><br />Jeśli to się powtórzy, to prosimy o kontakt przez e-mail.</div>';
180+
}
181+
}
182+
183+
/* --------------------------------------------------------- *\
184+
Funkcja: myErrorHandler_std
185+
186+
Zwraca sformatowany HTML-owo kod błędu i zapisuje
187+
poufne dane do pliku (stała: HANDLER_ERR_LOG).
188+
189+
Parametry:
190+
jak w fun. myErrorHandler
191+
\* --------------------------------------------------------- */
192+
function myErrorHandler_std ($errno, $errmsg, $err_file_name, $err_line_num)
193+
{
194+
//
195+
// translation array
196+
//
197+
$errortype = array (
198+
E_WARNING => 'Warning',
199+
E_NOTICE => 'Notice',
200+
E_USER_ERROR => 'User Error',
201+
E_USER_WARNING => 'User Warning',
202+
E_USER_NOTICE => 'User Notice',
203+
);
204+
if (!isset($errortype[$errno]))
205+
{
206+
$errortype[$errno] = 'Unknown';
207+
}
208+
209+
//
210+
// debug_msgtext
211+
//
212+
$file_name = basename($err_file_name);
213+
$dir_name = dirname($err_file_name);
214+
$debug_msgtext = "<div>
215+
<b>{$errortype[$errno]}</b> ($errno): $errmsg<br/>
216+
In [$dir_name/<b>$file_name</b>] at line ($err_line_num)";
217+
218+
/***
219+
// get file's lines
220+
$handle = fopen($err_file_name, 'r');
221+
$i = 0;
222+
$file_lines = '';
223+
while (!feof($handle)) {
224+
$line = fgets($handle, 4096);
225+
$i++;
226+
switch($err_line_num-$i)
227+
{
228+
case -1:
229+
case 1:
230+
$file_lines .= $line;
231+
break;
232+
case 0:
233+
$file_lines .= rtrim($line). "// ($err_line_num)\n";
234+
break;
235+
}
236+
}
237+
$debug_msgtext .= '<br/>'.highlight_string ($file_lines, TRUE);
238+
fclose($handle);
239+
/**/
240+
241+
// close debug div
242+
$debug_msgtext .= "</div>\n\n";
243+
244+
/**/
245+
if (@filesize(HANDLER_ERR_LOG) < MAX_LOG_SIZE)
246+
{
247+
$log_debug_msgtext = "\n----------------------------------------------------\n ".date("Y-m-d H:i:s (T)")."\n {$errortype[$errno]}($errno): $errmsg\n In [$err_file_name] at line ($err_line_num) \n----------------------------------------------------";
248+
error_log ($log_debug_msgtext, 3, HANDLER_ERR_LOG);
249+
}
250+
/**/
251+
252+
return $debug_msgtext;
253+
}
254+
255+
?>

‎index.php

+4
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
require_once './inc/dirHelper.php';
2424
require_once './inc/visitLogger.php';
2525

26+
// setup error handling
27+
include './inc/fun_error.php';
28+
init_myErrorHandler();
29+
2630
//
2731
// Display mode and other params
2832
//

0 commit comments

Comments
 (0)
Please sign in to comment.