source: trunk/src/inc/LogHandler.class.php@ 7786

Last change on this file since 7786 was 7773, checked in by Pieter Naber, 15 years ago

Created interlinks!
Fixed LogHandler to output at the end (so we have a valid KML file)

  • Property svn:eol-style set to native
File size: 3.5 KB
RevLine 
[7725]1<?php
2/*
3 * Project: NodeMap2.0
4 * File: LogHandler.class.php
5 * Purpose: Handling our log file containing log entries generated by the application
6 */
7
8/*
9 * Log levels:
10 * SYSLOG_EMERG System is unusable
11 * SYSLOG_ALERT Action must be taken immediately
12 * SYSLOG_CRIT Critical conditions
13 * SYSLOG_ERR Error conditions
14 * SYSLOG_WARNING Warning conditions
15 * SYSLOG_NOTICE Normal, but significant, condition
16 * SYSLOG_INFO Informational message
17 * SYSLOG_DEBUG Debug-level message
18 */
19
20define('SYSLOG_NONE', 0);
21define('SYSLOG_EMERG', 1);
22define('SYSLOG_ALERT', 2);
23define('SYSLOG_CRIT', 3);
24define('SYSLOG_ERR', 4);
25define('SYSLOG_WARNING', 5);
26define('SYSLOG_NOTICE', 6);
27define('SYSLOG_INFO', 7);
28define('SYSLOG_DEBUG', 8);
29
30/*
31 * Actions on log levels
32 */
33define('LOG_LEVEL_ECHO', SYSLOG_DEBUG);
34define('LOG_LEVEL_WRITE', SYSLOG_DEBUG);
35define('LOG_LEVEL_MAIL', SYSLOG_DEBUG);
36
37class LogHandler {
[7773]38 private $logString; // For caching purposes
39
[7725]40 /*
41 * Function: __construct (constructor)
42 * Parameters: -
43 * Function: Creating a LogHandler object
44 */
45 public function __construct() {
[7773]46 $logString = '';
[7725]47 }
48
49 /*
50 * Function: logEntry
51 * Parameters: int $logno, string $logstr, string $logfile, int $logline
52 * Function: Handling with log entries, decides what to do with it
53 */
54 public function logEntry($logno, $logstr, $logfile, $logline) {
55 $errorString = 'SysLog ';
56
57 switch ($logno) {
58 case SYSLOG_EMERG:
59 $errorString = 'Emergency';
60 break;
61 case SYSLOG_ALERT:
62 $errorString = 'Alert';
63 break;
64 case SYSLOG_CRIT:
65 $errorString = 'Critical';
66 break;
67 case SYSLOG_ERR:
68 $errorString = 'Error';
69 break;
70 case SYSLOG_WARNING:
71 $errorString = 'Warning';
72 break;
73 case SYSLOG_NOTICE:
74 $errorString = 'Notice';
75 break;
76 case SYSLOG_INFO:
77 $errorString = 'Information';
78 break;
79 case SYSLOG_DEBUG:
80 $errorString = 'Debug';
81 break;
82 default:
83 $errorString = 'SYSLOG_DEFAULT';
84 break;
85 }
86
[7773]87 $errorString .= ' (' . $logno . ' | ' . date('Y/d/m H:i:s') . '): ' . $logstr . "\r\n\t" . 'In file "' . $logfile . '" on line "' . $logline . '"' . "\r\n";
[7725]88
89 if ($logno <= LOG_LEVEL_ECHO) {
[7773]90 $this->logString .= $errorString;
[7725]91 }
[7765]92 if ((!defined('IGNORE_LOG_LEVEL_WRITE')) && ($logno <= LOG_LEVEL_WRITE)) {
[7737]93 $filename = 'logfile.txt';
94 if (is_writable($filename)) {
[7765]95
[7737]96 //Check if the file can be opened
[7765]97 if (!$handle = fopen($filename, 'w')) {
98 define('IGNORE_LOG_LEVEL_WRITE', true);
99 trigger_log(SYSLOG_EMERG, 'Cannot open file "' . $filename . '"', __FILE__, __LINE__);
100 exit;
[7737]101 }
[7765]102
[7737]103 // Write $errorString to file.
104 if (fwrite($handle, $errorString) === FALSE) {
[7749]105 define('LOG_LEVEL_WRITE', LOG_LEVEL_NONE);
106 trigger_log(SYSLOG_EMERG, 'Cannot write to "' . $filename . '"', __FILE__, __LINE__);
[7737]107 exit;
108 }
[7765]109
[7737]110 fclose($handle);
[7765]111
[7737]112 } else {
[7765]113 define('IGNORE_LOG_LEVEL_WRITE', true);
114 trigger_log(SYSLOG_EMERG, 'Cannot write to "' . $filename . '"', __FILE__, __LINE__);
[7737]115 }
[7725]116 }
[7765]117
[7725]118 if ($logno <= LOG_LEVEL_MAIL) {
[7737]119 mail('test@test.com', 'Nodemap error', $errorString);
[7725]120 }
121 }
[7773]122
123 function __destruct() {
124 echo '<!-- ' . $this->logString . ' -->';
125 }
[7725]126}
127
128$LOG_HANDLER = new LogHandler();
129
130/*
131 * Function: trigger_log
132 * Parameters: int $logno, string $logstr, string $logfile, int $logline
133 * Function: Handling with log entries, forwarding them to our $LOG_HANDLER object.
134 */
135function trigger_log($logno, $logstr, $logfile, $logline) {
136 global $LOG_HANDLER;
137
138 $LOG_HANDLER->logEntry($logno, $logstr, $logfile, $logline);
139}
[7618]140?>
Note: See TracBrowser for help on using the repository browser.