dftk
[ class tree: dftk ] [ index: dftk ] [ all elements ]

Source for file dftk_da_trace.php

Documentation is available at dftk_da_trace.php


1 <?php
2 /*
3
4 Copyright (c)2003 DuckCorp(tm) and RtpNet(tm)
5
6
7
8 This file is part of DFTK.
9
10 DFTK is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
14
15 DFTK is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with DFTK; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24
25 /**
26 * @package dftk
27 * @author Duck <duck@DuckCorp.org>
28 * @author Rtp <rtp@rtp-net.org>
29 * @copyright Copyright (c)2003 DuckCorp(tm) and RtpNet(tm)
30 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License
31 * @version 0.4.0
32 */
33
34
35 /**
36 * DFTK Trace Class
37 *
38 * @package dftk
39 * @subpackage dftk-trace
40 * @author Duck <duck@DuckCorp.org>
41 *
42 * @access public
43 */
44 class DftkDaTrace
45 {
46 /**
47 * Trace Manager
48 *
49 * @access private
50 * @var object DftkDaTraceManager
51 */
52 var $_manager;
53
54 /**
55 * List of Events
56 *
57 * @access private
58 * @var array
59 */
60 var $_events;
61
62 /**
63 * List of Results
64 *
65 * @access private
66 * @var array
67 */
68 var $_results;
69
70 /**
71 * Constructor
72 *
73 * @access public
74 * @param object DftkDaTraceManager &$manager Trace Manager
75 */
76 function DftkDaTrace(&$manager)
77 {
78 $this->_manager =& $manager;
79 $this->_events = array();
80 $this->_results = array();
81 }
82
83 /**
84 * Add an Event to the list
85 *
86 * @access public
87 * @param string $key Event Id
88 * @param array $parals List of message parameters
89 */
90 function add_event($key, $params = array())
91 {
92 if (!$this->_manager->is_event($key))
93 return false;
94
95 if (!is_array($params))
96 $params = array($params);
97 $tab = array();
98 $tab['timestamp'] = date("Y-m-d\TH:i:s");
99 $tab['event'] = $key;
100 $tab['params'] = $params;
101 $this->_events[] = $tab;
102 }
103
104 /**
105 * Remove every result in the list
106 *
107 * @access public
108 */
109 function clear_results()
110 {
111 $this->_results = array();
112 }
113
114 /**
115 * Unset a result's value
116 *
117 * @access public
118 * @param string $key Result Id
119 */
120 function unset_result($key)
121 {
122 unset($this->_results[$key]);
123 }
124
125 /**
126 * Set a result's value
127 *
128 * @access public
129 * @param string $key Result Id
130 * @param mixed $obj Value
131 */
132 function set_result($key, $obj)
133 {
134 $this->_results[$key] =& $obj;
135 }
136
137 /**
138 * Get a result's value
139 *
140 * @access public
141 * @param string $key Result Id
142 * @return mixed $r Value
143 */
144 function &get_result($key)
145 {
146 if ($this->_results[$key])
147 return $this->_results[$key];
148
149 return false;
150 }
151
152 /**
153 * Search for events
154 *
155 * List of severities :
156 * - E : error
157 * - W : warning
158 * - N : notice
159 *
160 * Each element of the returned array contains these entries :
161 * - event : event Id
162 * - timestamp : ISO8601 date+time format when the event occured
163 * - module : module name in which the event occured
164 * - section : section name in which the event occured
165 * - msg : readable message
166 * - fullmsg : readable message with full information
167 *
168 * @access public
169 * @param string $severity Severity criteria ( 'E'/'W'/'N' or null if not a criteria)
170 * @param string $module Module name criteria (or null if not a criteria)
171 * @param string $section Section name criteria (or null if not a criteria) (if a criteria, $module must not be null)
172 * @return array $ret Array of found events
173 */
174 function get_events($severity = null, $module = null, $section = null)
175 {
176 if ($severity != null)
177 if (!preg_match("/^[EWN]$/", $severity))
178 return array();
179
180 if ($module != null)
181 {
182 if ($section != null)
183 {
184 if (!$this->_manager->is_section($module, $section))
185 return array();
186 }
187 else
188 {
189 if (!$this->_manager->is_module($module))
190 return array();
191 }
192 }
193
194 $ret = array();
195
196 foreach ($this->_events as $ev)
197 {
198 $info = $this->_manager->get_event_infos($ev['event']);
199
200 if ($severity != null)
201 if ($info['severity'] != $severity)
202 continue;
203 if ($module != null)
204 {
205 if ($info['module'] != $module)
206 continue;
207
208 if ($section != null)
209 if ($info['section'] != $section)
210 continue;
211 }
212
213 $tab = array();
214 $tab['event'] = $ev['event'];
215 $tab['timestamp'] = $ev['timestamp'];
216 $tab['module'] = $info['module'];
217 $tab['section'] = $info['section'];
218 $tab['msg'] = $this->_manager->_get_event_msg($ev['event'], $ev['params']);
219 if ($tab['section'])
220 $tab['fullmsg'] = $tab['timestamp']." ".$tab['module']."::".$tab['section']."::".$tab['event']." '".$tab['msg']."'";
221 else
222 $tab['fullmsg'] = $tab['timestamp']." ".$tab['module']."::".$tab['event']." '".$tab['msg']."'";
223 $ret[] = $tab;
224 }
225
226 return $ret;
227 }
228
229 /**
230 * Create a debug message
231 *
232 * @access public
233 * @param boolean $html true for HTML output, false for plain text
234 * @return string $msg
235 */
236 function get_debug_msg($html = false)
237 {
238 $msg = "";
239 if ($html)
240 $msg = "<table border='1'><tr><th>Timestamp</th><th>Severity</th><th>Module</th><th>Section</th><th>Message</th></tr>";
241
242 foreach ($this->_events as $ev)
243 {
244 $info = $this->_manager->get_event_infos($ev['event']);
245
246 $tab = array();
247 $tab['event'] = $ev['event'];
248 $tab['timestamp'] = $ev['timestamp'];
249 $tab['module'] = $info['module'];
250 $tab['section'] = $info['section'];
251 $tab['severity'] = $info['severity'];
252 $tab['msg'] = $this->_manager->_get_event_msg($ev['event'], $ev['params']);
253
254 if ($html)
255 $msg.="<tr><td>".$tab['timestamp']."</td><td align='center'>".$tab['severity']."</td><td align='center'>".$tab['module']."</td><td align='center'>".$tab['section']."</td><td>".$tab['msg']."</td></tr>\n";
256 else
257 {
258 if ($tab['section'])
259 $tab['fullmsg'] = $tab['timestamp']." ".$tab['severity']." ".$tab['module']."::".$tab['section']." '".$tab['msg']."'";
260 else
261 $tab['fullmsg'] = $tab['timestamp']." ".$tab['severity']." ".$tab['module']." '".$tab['msg']."'";
262
263 if ($msg == "")
264 $msg=$tab['fullmsg'];
265 else
266 $msg.="\n".$tab['fullmsg'];
267 }
268 }
269
270 if ($html)
271 $msg.="</table>";
272
273 return $msg;
274 }
275
276 /**
277 * Generate output
278 *
279 * @access public
280 * @return object DftkDaTrace $r Trace
281 */
282 function &gen_output()
283 {
284 if (!$this->_manager->has_output())
285 return;
286
287 $content = "";
288 foreach ($this->_events as $ev)
289 $content.= $this->_manager->_get_event_msg($ev['event'], $ev['params'])."\n";
290
291 return $this->_manager->_gen_output($content);
292 }
293
294 /**
295 * Look if a special event has occured
296 *
297 * @access public
298 * @param string $key Event Id
299 * @return boolean $r true if event occured
300 */
301 function test_if_event($key)
302 {
303 if (!$this->_manager->is_event($key))
304 return false;
305
306 foreach ($this->_events as $ev)
307 if ($ev['event'] == $key)
308 return true;
309
310 return false;
311 }
312
313 /**
314 * Merge Traces
315 *
316 * Merge current trace with given trace :
317 * - results are merged (beware of collisions)
318 * - events are sort-merged
319 *
320 * @access public
321 * @param object DftkDaTrace &$trace
322 */
323 function merge_traces(&$trace)
324 {
325 if ($this->_manager == $trace->_manager)
326 {
327 $this->_results = array_merge($this->_results, $trace->_results);
328
329 $tab = array_merge($this->_events, $trace->_events);
330 $this->_events = array();
331 if (count($tab))
332 {
333 $tab2 = array();
334 for ($i=0; $i<count($tab); $i++)
335 {
336 $tab2[0][] = $tab[$i]['timestamp'];
337 $tab2[1][] = $i;
338 }
339 array_multisort($tab2[0], $tab2[1]);
340 for ($i=0; $i<count($tab2[1]); $i++)
341 $this->_events[] = $tab[$tab2[1][$i]];
342 }
343 }
344 }
345
346 /**
347 * Test if events with an 'error' severity has occured
348 *
349 * @access public
350 * @return boolean $r true if such event occured
351 */
352 function has_error()
353 {
354 if (count($this->get_events('E'))>0)
355 return true;
356
357 return false;
358 }
359
360 /**
361 * Test if events with an 'warning' severity has occured
362 *
363 * @access public
364 * @return boolean $r true if such event occured
365 */
366 function has_warning()
367 {
368 if (count($this->get_events('W'))>0)
369 return true;
370
371 return false;
372 }
373 }
374
375 ?>

Documentation generated on Sat, 6 Dec 2003 13:47:34 +0100 by phpDocumentor 1.2.3