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

Last change on this file since 7753 was 7753, checked in by ddboer, 13 years ago

Fixed error

  • Property svn:eol-style set to native
File size: 3.4 KB
Line 
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 {
38 /*
39 * Function: __construct (constructor)
40 * Parameters: -
41 * Function: Creating a LogHandler object
42 */
43 public function __construct() {
44 }
45
46 /*
47 * Function: logEntry
48 * Parameters: int $logno, string $logstr, string $logfile, int $logline
49 * Function: Handling with log entries, decides what to do with it
50 */
51 public function logEntry($logno, $logstr, $logfile, $logline) {
52 $errorString = 'SysLog ';
53
54 switch ($logno) {
55 case SYSLOG_EMERG:
56 $errorString = 'Emergency';
57 break;
58 case SYSLOG_ALERT:
59 $errorString = 'Alert';
60 break;
61 case SYSLOG_CRIT:
62 $errorString = 'Critical';
63 break;
64 case SYSLOG_ERR:
65 $errorString = 'Error';
66 break;
67 case SYSLOG_WARNING:
68 $errorString = 'Warning';
69 break;
70 case SYSLOG_NOTICE:
71 $errorString = 'Notice';
72 break;
73 case SYSLOG_INFO:
74 $errorString = 'Information';
75 break;
76 case SYSLOG_DEBUG:
77 $errorString = 'Debug';
78 break;
79 default:
80 $errorString = 'SYSLOG_DEFAULT';
81 break;
82 }
83
84 $time = date( "H:i.s m/d/y" );
85 $errorString .= ' (' . $logno .' | '.$time .' | ): ' . $logstr . "\r\n\t" . 'In file "' . $logfile . '" on line "' . $logline . '"' . "\r\n";
86
87 if ($logno <= LOG_LEVEL_ECHO) {
88 echo '<!-- ' . $errorString . ' -->';
89 }
90 if ($logno <= LOG_LEVEL_WRITE) {
91 $filename = 'logfile.txt';
92 if (is_writable($filename)) {
93
94 //Check if the file can be opened
95 if (!$handle = fopen($filename, 'w')) {
96 define('LOG_LEVEL_WRITE', LOG_LEVEL_NONE);
97 trigger_log(SYSLOG_EMERG, 'Cannot open file "' . $filename . '"', __FILE__, __LINE__);
98 exit;
99 }
100
101 // Write $errorString to file.
102 if (fwrite($handle, $errorString) === FALSE) {
103 define('LOG_LEVEL_WRITE', LOG_LEVEL_NONE);
104 trigger_log(SYSLOG_EMERG, 'Cannot write to "' . $filename . '"', __FILE__, __LINE__);
105 exit;
106 }
107
108 fclose($handle);
109
110 } else {
111 define('LOG_LEVEL_WRITE', LOG_LEVEL_NONE);
112 trigger_log(SYSLOG_EMERG, 'Cannot write to "' . $filename . '"', __FILE__, __LINE__);
113 }
114 }
115
116 if ($logno <= LOG_LEVEL_MAIL) {
117 mail('test@test.com', 'Nodemap error', $errorString);
118 }
119 }
120}
121
122$LOG_HANDLER = new LogHandler();
123
124/*
125 * Function: trigger_log
126 * Parameters: int $logno, string $logstr, string $logfile, int $logline
127 * Function: Handling with log entries, forwarding them to our $LOG_HANDLER object.
128 */
129function trigger_log($logno, $logstr, $logfile, $logline) {
130 global $LOG_HANDLER;
131
132 $LOG_HANDLER->logEntry($logno, $logstr, $logfile, $logline);
133}
134?>
Note: See TracBrowser for help on using the repository browser.