1 | <?php |
---|
2 | /*********************************************** |
---|
3 | * File : zlog.php |
---|
4 | * Project : Z-Push |
---|
5 | * Descr : Debug and logging |
---|
6 | * |
---|
7 | * Created : 01.10.2007 |
---|
8 | * |
---|
9 | * Copyright 2007 - 2012 Zarafa Deutschland GmbH |
---|
10 | * |
---|
11 | * This program is free software: you can redistribute it and/or modify |
---|
12 | * it under the terms of the GNU Affero General Public License, version 3, |
---|
13 | * as published by the Free Software Foundation with the following additional |
---|
14 | * term according to sec. 7: |
---|
15 | * |
---|
16 | * According to sec. 7 of the GNU Affero General Public License, version 3, |
---|
17 | * the terms of the AGPL are supplemented with the following terms: |
---|
18 | * |
---|
19 | * "Zarafa" is a registered trademark of Zarafa B.V. |
---|
20 | * "Z-Push" is a registered trademark of Zarafa Deutschland GmbH |
---|
21 | * The licensing of the Program under the AGPL does not imply a trademark license. |
---|
22 | * Therefore any rights, title and interest in our trademarks remain entirely with us. |
---|
23 | * |
---|
24 | * However, if you propagate an unmodified version of the Program you are |
---|
25 | * allowed to use the term "Z-Push" to indicate that you distribute the Program. |
---|
26 | * Furthermore you may use our trademarks where it is necessary to indicate |
---|
27 | * the intended purpose of a product or service provided you use it in accordance |
---|
28 | * with honest practices in industrial or commercial matters. |
---|
29 | * If you want to propagate modified versions of the Program under the name "Z-Push", |
---|
30 | * you may only do so if you have a written permission by Zarafa Deutschland GmbH |
---|
31 | * (to acquire a permission please contact Zarafa at trademark@zarafa.com). |
---|
32 | * |
---|
33 | * This program is distributed in the hope that it will be useful, |
---|
34 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
35 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
36 | * GNU Affero General Public License for more details. |
---|
37 | * |
---|
38 | * You should have received a copy of the GNU Affero General Public License |
---|
39 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
40 | * |
---|
41 | * Consult LICENSE file for details |
---|
42 | ************************************************/ |
---|
43 | |
---|
44 | class ZLog { |
---|
45 | static private $devid = ''; |
---|
46 | static private $user = ''; |
---|
47 | static private $authUser = false; |
---|
48 | static private $pidstr; |
---|
49 | static private $wbxmlDebug = ''; |
---|
50 | static private $lastLogs = array(); |
---|
51 | |
---|
52 | /** |
---|
53 | * Initializes the logging |
---|
54 | * |
---|
55 | * @access public |
---|
56 | * @return boolean |
---|
57 | */ |
---|
58 | static public function Initialize() { |
---|
59 | global $specialLogUsers; |
---|
60 | |
---|
61 | // define some constants for the logging |
---|
62 | if (!defined('LOGUSERLEVEL')) |
---|
63 | define('LOGUSERLEVEL', LOGLEVEL_OFF); |
---|
64 | |
---|
65 | if (!defined('LOGLEVEL')) |
---|
66 | define('LOGLEVEL', LOGLEVEL_OFF); |
---|
67 | |
---|
68 | list($user,) = Utils::SplitDomainUser(Request::GetGETUser()); |
---|
69 | if (!defined('WBXML_DEBUG') && $user) { |
---|
70 | // define the WBXML_DEBUG mode on user basis depending on the configurations |
---|
71 | if (LOGLEVEL >= LOGLEVEL_WBXML || (LOGUSERLEVEL >= LOGLEVEL_WBXML && in_array($user, $specialLogUsers))) |
---|
72 | define('WBXML_DEBUG', true); |
---|
73 | else |
---|
74 | define('WBXML_DEBUG', false); |
---|
75 | } |
---|
76 | |
---|
77 | if ($user) |
---|
78 | self::$user = '['. $user .'] '; |
---|
79 | else |
---|
80 | self::$user = ''; |
---|
81 | |
---|
82 | // log the device id if the global loglevel is set to log devid or the user is in and has the right log level |
---|
83 | if (Request::GetDeviceID() != "" && (LOGLEVEL >= LOGLEVEL_DEVICEID || (LOGUSERLEVEL >= LOGLEVEL_DEVICEID && in_array($user, $specialLogUsers)))) |
---|
84 | self::$devid = '['. Request::GetDeviceID() .'] '; |
---|
85 | else |
---|
86 | self::$devid = ''; |
---|
87 | |
---|
88 | return true; |
---|
89 | } |
---|
90 | |
---|
91 | /** |
---|
92 | * Writes a log line |
---|
93 | * |
---|
94 | * @param int $loglevel one of the defined LOGLEVELS |
---|
95 | * @param string $message |
---|
96 | * |
---|
97 | * @access public |
---|
98 | * @return |
---|
99 | */ |
---|
100 | static public function Write($loglevel, $message) { |
---|
101 | self::$lastLogs[$loglevel] = $message; |
---|
102 | $data = self::buildLogString($loglevel) . $message . "\n"; |
---|
103 | |
---|
104 | if ($loglevel <= LOGLEVEL) { |
---|
105 | @file_put_contents(LOGFILE, $data, FILE_APPEND); |
---|
106 | } |
---|
107 | |
---|
108 | if ($loglevel <= LOGUSERLEVEL && self::logToUserFile()) { |
---|
109 | // padd level for better reading |
---|
110 | $data = str_replace(self::getLogLevelString($loglevel), self::getLogLevelString($loglevel,true), $data); |
---|
111 | // only use plain old a-z characters for the generic log file |
---|
112 | @file_put_contents(LOGFILEDIR . self::logToUserFile() . ".log", $data, FILE_APPEND); |
---|
113 | } |
---|
114 | |
---|
115 | if (($loglevel & LOGLEVEL_FATAL) || ($loglevel & LOGLEVEL_ERROR)) { |
---|
116 | @file_put_contents(LOGERRORFILE, $data, FILE_APPEND); |
---|
117 | } |
---|
118 | |
---|
119 | if ($loglevel & LOGLEVEL_WBXMLSTACK) { |
---|
120 | self::$wbxmlDebug .= $message. "\n"; |
---|
121 | } |
---|
122 | } |
---|
123 | |
---|
124 | /** |
---|
125 | * Returns logged information about the WBXML stack |
---|
126 | * |
---|
127 | * @access public |
---|
128 | * @return string |
---|
129 | */ |
---|
130 | static public function GetWBXMLDebugInfo() { |
---|
131 | return trim(self::$wbxmlDebug); |
---|
132 | } |
---|
133 | |
---|
134 | /** |
---|
135 | * Returns the last message logged for a log level |
---|
136 | * |
---|
137 | * @param int $loglevel one of the defined LOGLEVELS |
---|
138 | * |
---|
139 | * @access public |
---|
140 | * @return string/false returns false if there was no message logged in that level |
---|
141 | */ |
---|
142 | static public function GetLastMessage($loglevel) { |
---|
143 | return (isset(self::$lastLogs[$loglevel]))?self::$lastLogs[$loglevel]:false; |
---|
144 | } |
---|
145 | |
---|
146 | /**---------------------------------------------------------------------------------------------------------- |
---|
147 | * private log stuff |
---|
148 | */ |
---|
149 | |
---|
150 | /** |
---|
151 | * Returns the filename logs for a WBXML debug log user should be saved to |
---|
152 | * |
---|
153 | * @access private |
---|
154 | * @return string |
---|
155 | */ |
---|
156 | static private function logToUserFile() { |
---|
157 | global $specialLogUsers; |
---|
158 | |
---|
159 | if (self::$authUser === false) { |
---|
160 | if (RequestProcessor::isUserAuthenticated()) { |
---|
161 | $authuser = Request::GetAuthUser(); |
---|
162 | if ($authuser && in_array($authuser, $specialLogUsers)) |
---|
163 | self::$authUser = preg_replace('/[^a-z0-9]/', '_', strtolower($authuser)); |
---|
164 | } |
---|
165 | } |
---|
166 | return self::$authUser; |
---|
167 | } |
---|
168 | |
---|
169 | /** |
---|
170 | * Returns the string to be logged |
---|
171 | * |
---|
172 | * @access private |
---|
173 | * @return string |
---|
174 | */ |
---|
175 | static private function buildLogString($loglevel) { |
---|
176 | if (!isset(self::$pidstr)) |
---|
177 | self::$pidstr = '[' . str_pad(@getmypid(),5," ",STR_PAD_LEFT) . '] '; |
---|
178 | |
---|
179 | if (!isset(self::$user)) |
---|
180 | self::$user = ''; |
---|
181 | |
---|
182 | if (!isset(self::$devid)) |
---|
183 | self::$devid = ''; |
---|
184 | |
---|
185 | return Utils::GetFormattedTime() ." ". self::$pidstr . self::getLogLevelString($loglevel, (LOGLEVEL > LOGLEVEL_INFO)) ." ". self::$user . self::$devid; |
---|
186 | } |
---|
187 | |
---|
188 | /** |
---|
189 | * Returns the string representation of the LOGLEVEL. |
---|
190 | * String can be padded |
---|
191 | * |
---|
192 | * @param int $loglevel one of the LOGLEVELs |
---|
193 | * @param boolean $pad |
---|
194 | * |
---|
195 | * @access private |
---|
196 | * @return string |
---|
197 | */ |
---|
198 | static private function getLogLevelString($loglevel, $pad = false) { |
---|
199 | if ($pad) $s = " "; |
---|
200 | else $s = ""; |
---|
201 | switch($loglevel) { |
---|
202 | case LOGLEVEL_OFF: return ""; break; |
---|
203 | case LOGLEVEL_FATAL: return "[FATAL]"; break; |
---|
204 | case LOGLEVEL_ERROR: return "[ERROR]"; break; |
---|
205 | case LOGLEVEL_WARN: return "[".$s."WARN]"; break; |
---|
206 | case LOGLEVEL_INFO: return "[".$s."INFO]"; break; |
---|
207 | case LOGLEVEL_DEBUG: return "[DEBUG]"; break; |
---|
208 | case LOGLEVEL_WBXML: return "[WBXML]"; break; |
---|
209 | case LOGLEVEL_DEVICEID: return "[DEVICEID]"; break; |
---|
210 | case LOGLEVEL_WBXMLSTACK: return "[WBXMLSTACK]"; break; |
---|
211 | } |
---|
212 | } |
---|
213 | } |
---|
214 | |
---|
215 | /**---------------------------------------------------------------------------------------------------------- |
---|
216 | * Legacy debug stuff |
---|
217 | */ |
---|
218 | |
---|
219 | // deprecated |
---|
220 | // backwards compatible |
---|
221 | function debugLog($message) { |
---|
222 | ZLog::Write(LOGLEVEL_DEBUG, $message); |
---|
223 | } |
---|
224 | |
---|
225 | // TODO review error handler |
---|
226 | function zarafa_error_handler($errno, $errstr, $errfile, $errline, $errcontext) { |
---|
227 | $bt = debug_backtrace(); |
---|
228 | switch ($errno) { |
---|
229 | case 8192: // E_DEPRECATED since PHP 5.3.0 |
---|
230 | // do not handle this message |
---|
231 | break; |
---|
232 | |
---|
233 | case E_NOTICE: |
---|
234 | case E_WARNING: |
---|
235 | // TODO check if there is a better way to avoid these messages |
---|
236 | if (stripos($errfile,'interprocessdata') !== false && stripos($errstr,'shm_get_var()') !== false) |
---|
237 | break; |
---|
238 | ZLog::Write(LOGLEVEL_WARN, "$errfile:$errline $errstr ($errno)"); |
---|
239 | break; |
---|
240 | |
---|
241 | default: |
---|
242 | ZLog::Write(LOGLEVEL_ERROR, "trace error: $errfile:$errline $errstr ($errno) - backtrace: ". (count($bt)-1) . " steps"); |
---|
243 | for($i = 1, $bt_length = count($bt); $i < $bt_length; $i++) { |
---|
244 | $file = $line = "unknown"; |
---|
245 | if (isset($bt[$i]['file'])) $file = $bt[$i]['file']; |
---|
246 | if (isset($bt[$i]['line'])) $line = $bt[$i]['line']; |
---|
247 | ZLog::Write(LOGLEVEL_ERROR, "trace: $i:". $file . ":" . $line. " - " . ((isset($bt[$i]['class']))? $bt[$i]['class'] . $bt[$i]['type']:""). $bt[$i]['function']. "()"); |
---|
248 | } |
---|
249 | //throw new Exception("An error occured."); |
---|
250 | break; |
---|
251 | } |
---|
252 | } |
---|
253 | |
---|
254 | error_reporting(E_ALL); |
---|
255 | set_error_handler("zarafa_error_handler"); |
---|
256 | |
---|
257 | ?> |
---|