Source for file dftk_da_trace_manager.php
Documentation is available at dftk_da_trace_manager.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 /** 27 * @package dftk 28 * @author Duck <duck@DuckCorp.org> 29 * @author Rtp <rtp@rtp-net.org> 30 * @copyright Copyright (c)2003 DuckCorp(tm) and RtpNet(tm) 31 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License 32 * @version 0.4.0 33 */ 34 35 36 /** 37 * DFTK Trace Manager Class 38 * 39 * @package dftk 40 * @subpackage dftk-trace 41 * @author Duck <duck@DuckCorp.org> 42 * 43 * @access public 44 */ 45 class DftkDaTraceManager 46 { 47 /** 48 * List of directories where to find traces information 49 * 50 * @access private 51 * @var array 52 */ 53 var $_dirlist; 54 55 /** 56 * Information about all registered events 57 * 58 * @access private 59 * @var array 60 */ 61 var $_events; 62 63 /** 64 * Tree of modules/sections for registered events 65 * 66 * @access private 67 * @var array 68 */ 69 var $_events_tree; 70 71 /** 72 * Language Manager 73 * 74 * @access private 75 * @var object DftkDaLanguageManager 76 */ 77 var $_lm; 78 79 /** 80 * Output object 81 * 82 * @access private 83 * @var object DftkOutput 84 */ 85 var $_output; 86 87 /** 88 * Constructor 89 * 90 * @access public 91 * @param object DftkDaLanguageManager &$lm Language Manager 92 */ 93 function DftkDaTraceManager(&$lm) 94 { 95 $this->_dirlist = array(); 96 $this->_events = array(); 97 $this->_events_tree = array(); 98 $this->_lm =& $lm; 99 $this->_output = null; 100 } 101 102 /** 103 * Set Output 104 * 105 * Set output to 'null' if no output wanted (default). 106 * 107 * @access public 108 * @param object DftkOutput $output Output object 109 */ 110 function set_output(&$output) 111 { 112 $this->_output =& $output; 113 } 114 115 /** 116 * Has Output ? 117 * 118 * @access public 119 * @return boolean $r true if Output defined 120 */ 121 function has_output() 122 { 123 if ($this->_output === null) 124 return false; 125 126 return true; 127 } 128 129 /** 130 * Gen Output message 131 * 132 * @access private 133 * @param string $content Message content 134 * @return object DftkDaTrace $r Trace 135 */ 136 function &_gen_output($content) 137 { 138 $this->_output->clear_content(); 139 $this->_output->add_content($content); 140 return $this->_output->flush_output(); 141 } 142 143 /** 144 * Rebuild the list of events and the modules/sections tree 145 * 146 * @access public 147 * @return boolean $r true if reload was successful 148 */ 149 function reload() 150 { 151 $r = true; 152 153 $this->_events = array(); 154 $this->_events_tree = array(); 155 foreach ($this->_dirlist as $dir) 156 { 157 if (!$this->_load($dir['module'], $dir['dir'])) 158 $r = false; 159 } 160 161 return $r; 162 } 163 164 /** 165 * Register traces 166 * 167 * @access public 168 * @deprecated Use 'register_events' instead, this was a confusing method name. 169 * @param string $module Module name 170 * @param string $dir Directory path where to find traces information 171 * @return boolean $r true if registration was successful 172 */ 173 function register_traces($module, $dir) 174 { 175 return $this->register_events($module, $dir); 176 } 177 178 /** 179 * Register events 180 * 181 * @access public 182 * @param string $module Module name 183 * @param string $dir Directory path where to find traces information 184 * @return boolean $r true if registration was successful 185 */ 186 function register_events($module, $dir) 187 { 188 if (file_exists($dir)) 189 { 190 $this->_dirlist[] = array('module' => $module, 'dir' => $dir); 191 return $this->_load($module, $dir); 192 } 193 else 194 return false; 195 } 196 197 /** 198 * Load traces information 199 * 200 * @access private 201 * @param string $module Module name 202 * @param string $dir Directory path where to find traces information 203 * @return boolean $r true if load was successful 204 */ 205 function _load($module, $dir) 206 { 207 if (file_exists($dir."/msgs") && file_exists($dir."/events")) 208 { 209 $this->_lm->add_trans($dir."/msgs"); 210 211 $handle = opendir($dir."/events"); 212 while ($file = readdir($handle)) 213 { 214 if ($file=='.' || $file=='..') 215 continue; 216 if (is_dir($file)) 217 continue; 218 $z = parse_ini_file($dir."/events/".$file, true); 219 $this->_load_events($module, null, $z); 220 } 221 closedir($handle); 222 223 return true; 224 } 225 else 226 return false; 227 } 228 229 /** 230 * Load events information 231 * 232 * @access private 233 * @param string $modules Modules name 234 * @param string $section Section name 235 * @param array $tab Events information 236 */ 237 function _load_events($module, $section, $tab) 238 { 239 foreach ($tab as $key => $trace) 240 { 241 if (is_array($trace)) 242 $this->_load_events($module, $key, $trace); 243 else 244 { 245 $params = preg_split("/\|/", $trace, 1); 246 if (!preg_match("/^[EWN]$/", $params[0])) 247 continue; 248 249 $this->_events[$key]['module'] = $module; 250 $this->_events[$key]['section'] = $section; 251 $this->_events[$key]['severity'] = $params[0]; 252 253 if ($section == null) 254 { 255 if (!$this->_events_tree[$module]) 256 $this->_events_tree[$module] = array(); 257 } 258 else 259 $this->_events_tree[$module][] = $section; 260 } 261 } 262 } 263 264 /** 265 * Get event information 266 * 267 * Event information fields : 268 * - modules : module name in which the event is registered 269 * - section : section name in which the event is registered 270 * - severity : event severity 271 * 272 * List of severities : 273 * - E : error 274 * - W : warning 275 * - N : notice 276 * 277 * @access public 278 * @param string $key Event Id 279 * @return array $r Information about event (or false if event not existing) 280 */ 281 function get_event_infos($key) 282 { 283 if ($this->_events[$key]) 284 return $this->_events[$key]; 285 286 return false; 287 } 288 289 /** 290 * Get the event readable message 291 * 292 * @access private 293 * @param string $key Event Id 294 * @param array $params Parameters of the message 295 * @return string $r Message (or false if event not existing) 296 */ 297 function _get_event_msg($key, $params) 298 { 299 if ($this->_events[$key]) 300 return $this->_lm->get_msg($key, $params); 301 302 return false; 303 } 304 305 306 /** 307 * Get the event message template 308 * 309 * @access public 310 * @param string $key Event Id 311 * @return string $r Message template (or false if event not existing) 312 */ 313 function get_event_msg_template($key) 314 { 315 if ($this->_events[$key]) 316 return $this->_lm->get_msg($key, null); 317 318 return false; 319 } 320 321 /** 322 * Test if a key is an event Id 323 * 324 * @access public 325 * @param string $key Event Id 326 * @return boolean $r true if the key is an event Id 327 */ 328 function is_event($key) 329 { 330 if ($this->_events[$key]) 331 return true; 332 333 return false; 334 } 335 336 /** 337 * Get the selected Language Manage 338 * 339 * @access public 340 * @return object DftkDaLanguageManager $r Language Manage 341 */ 342 function &get_language_manager() 343 { 344 return $this->_lm; 345 } 346 347 /** 348 * Get a list of registered events 349 * 350 * @access public 351 * @return array $tab List of registered events 352 */ 353 function get_event_list() 354 { 355 $tab = array(); 356 foreach ($this->_events as $key => $val) 357 $tab[] = $key; 358 359 return $tab; 360 } 361 362 /** 363 * Get a list of existing modules 364 * 365 * @access public 366 * @return array $tab List of existing modules 367 */ 368 function get_module_list() 369 { 370 $tab = array(); 371 foreach ($this->_events_tree as $key => $val) 372 $tab[] = $key; 373 374 return $tab; 375 } 376 377 /** 378 * Test if a string is a module name 379 * 380 * @access public 381 * @param string $module Module name 382 * @return boolean $r true if the string is a module name 383 */ 384 function is_module($module) 385 { 386 foreach ($this->_events_tree as $key => $val) 387 if ($key == $module) 388 return true; 389 390 return false; 391 } 392 393 /** 394 * Get a list of existing sections 395 * 396 * @access public 397 * @return array $tab List of existing sections 398 */ 399 function get_section_list($module) 400 { 401 if (!$this->is_module($module)) 402 return false; 403 404 foreach ($this->_events_tree[$module] as $key) 405 $tab[] = $key; 406 407 return $tab; 408 } 409 410 /** 411 * Test if a string is a section name 412 * 413 * @access public 414 * @param string $section Section name 415 * @return boolean $r true if the string is a section name 416 */ 417 function is_section($module, $section) 418 { 419 if (!$this->is_module($module)) 420 return false; 421 422 foreach ($this->_events_tree[$module] as $key) 423 if ($key == $section) 424 return true; 425 426 return false; 427 } 428 429 /** 430 * Creste a new Trace attached to this Manager 431 * 432 * @access public 433 * @return object DftkDaTrace $z Created Trace 434 */ 435 function &create_trace() 436 { 437 $z =& new DftkDaTrace(&$this); 438 return $z; 439 } 440 } 441 442 ?>
|