1 | <?php |
---|
2 | |
---|
3 | /** |
---|
4 | Inspired by Klooger, |
---|
5 | https://github.com/katzgrau/KLogger/ |
---|
6 | */ |
---|
7 | class t29Log { |
---|
8 | const EMERG = 'emerg'; // Emergency: system is unusable |
---|
9 | const ALERT = 'alert'; // Alert: action must be taken immediately |
---|
10 | const CRIT = 'crit'; // Critical: critical conditions |
---|
11 | const ERR = 'err'; // Error: error conditions |
---|
12 | const WARN = 'warn'; // Warning: warning conditions |
---|
13 | const NOTICE = 'notice'; // Notice: normal but significant condition |
---|
14 | const INFO = 'info'; // Informational: informational messages |
---|
15 | const DEBUG = 'debug'; // Debug: debug messages |
---|
16 | |
---|
17 | /** |
---|
18 | * We need a default argument value in order to add the ability to easily |
---|
19 | * print out objects etc. But we can't use NULL, 0, FALSE, etc, because those |
---|
20 | * are often the values the developers will test for. So we'll make one up. |
---|
21 | */ |
---|
22 | const NO_ARGUMENTS = 't29Log::NO_ARGUMENTS'; |
---|
23 | /* |
---|
24 | log array format: |
---|
25 | [ |
---|
26 | [LEVEL,string], |
---|
27 | [LEVEL,string], |
---|
28 | [LEVEL,string], |
---|
29 | ... |
---|
30 | ] |
---|
31 | */ |
---|
32 | public $entries = array(); |
---|
33 | |
---|
34 | function __construct() { |
---|
35 | // we shall be the PHP error handler |
---|
36 | set_error_handler(array($this, 'log_phperror')); |
---|
37 | |
---|
38 | // and register a final shutdown function |
---|
39 | register_shutdown_function(array($this, 'php_shutdown')); |
---|
40 | } |
---|
41 | |
---|
42 | function log($line, $severity, $args = self::NO_ARGUMENTS) { |
---|
43 | if($args !== self::NO_ARGUMENTS) |
---|
44 | $line .= '; '. var_export($args, true); |
---|
45 | $this->entries[] = array($severity, $line); |
---|
46 | } |
---|
47 | |
---|
48 | function log_phperror($errno, $errstr, $errfile, $errline) { |
---|
49 | switch($errno) { |
---|
50 | case E_WARNING: $errno = self::WARN; break; |
---|
51 | case E_NOTICE: $errno = self::NOTICE; break; |
---|
52 | default: $errno = self::WARN; break; |
---|
53 | } |
---|
54 | |
---|
55 | $this->log("Error on line <tt class='line'>$errline</tt> in file <tt class='file'>$errfile</tt>:\n<pre>".htmlspecialchars($errstr)."</pre>", |
---|
56 | $errno); |
---|
57 | |
---|
58 | /* Don't execute PHP internal error handler */ |
---|
59 | return true; |
---|
60 | } |
---|
61 | |
---|
62 | function php_shutdown() { |
---|
63 | if(!$this->is_empty()) { |
---|
64 | // we still have errors. print them! |
---|
65 | $this->print_all('final shutdown'); |
---|
66 | } |
---|
67 | } |
---|
68 | |
---|
69 | function is_empty() { |
---|
70 | return empty($this->entries); |
---|
71 | } |
---|
72 | |
---|
73 | function print_all($ul_classes='') { |
---|
74 | // causal printing function. Flushes entries afterwards! |
---|
75 | print "<ul class='$ul_classes'>"; |
---|
76 | foreach($this->entries as $entry) { |
---|
77 | printf('<li class="%s">%s</li>'.PHP_EOL, $entry[0], $entry[1]); |
---|
78 | } |
---|
79 | print "</ul>"; |
---|
80 | $this->entries = array(); // flush entries! |
---|
81 | } |
---|
82 | |
---|
83 | // convenience functions |
---|
84 | public function FATAL($line, $args = self::NO_ARGUMENTS){ |
---|
85 | $this->log($line, self::FATAL, $args); |
---|
86 | } |
---|
87 | |
---|
88 | |
---|
89 | |
---|
90 | } // class |
---|