XOOPS 2.5.6  Final
 All Classes Namespaces Files Functions Variables Pages
xoopslogger.php
Go to the documentation of this file.
1 <?php
24 defined('XOOPS_ROOT_PATH') or die('Restricted access');
25 
35 {
41  var $queries = array();
42  var $blocks = array();
43  var $extra = array();
44  var $logstart = array();
45  var $logend = array();
46  var $errors = array();
47  var $deprecated = array();
52  var $usePopup = false;
53  var $activated = true;
54 
58  var $renderingEnabled = false;
59 
63  function __construct()
64  {
65  }
66 
70  function XoopsLogger()
71  {
72  }
73 
77  function &instance()
78  {
79  return XoopsLogger::getInstance();
80  }
81 
87  static function &getInstance()
88  {
89  static $instance;
90  if (!isset($instance)) {
91  $instance = new XoopsLogger();
92  // Always catch errors, for security reasons
93  set_error_handler('XoopsErrorHandler_HandleError');
94  }
95  return $instance;
96  }
97 
104  function enableRendering()
105  {
106  if (!$this->renderingEnabled) {
107  ob_start(array(&$this , 'render'));
108  $this->renderingEnabled = true;
109  }
110  }
111 
117  function microtime()
118  {
119  $now = explode(' ', microtime());
120  return (float) $now[0] + (float) $now[1];
121  }
122 
128  function startTime($name = 'XOOPS')
129  {
130  if ($this->activated)
131  $this->logstart[$name] = $this->microtime();
132  }
133 
139  function stopTime($name = 'XOOPS')
140  {
141  if ($this->activated)
142  $this->logend[$name] = $this->microtime();
143  }
144 
152  function addQuery($sql, $error = null, $errno = null, $query_time = null)
153  {
154  if ($this->activated)
155  $this->queries[] = array('sql' => $sql , 'error' => $error , 'errno' => $errno, 'query_time' => $query_time);
156  }
157 
165  function addBlock($name, $cached = false, $cachetime = 0)
166  {
167  if ($this->activated)
168  $this->blocks[] = array('name' => $name , 'cached' => $cached , 'cachetime' => $cachetime);
169  }
170 
177  function addExtra($name, $msg)
178  {
179  if ($this->activated)
180  $this->extra[] = array('name' => $name , 'msg' => $msg);
181  }
182 
190  function addDeprecated($msg)
191  {
192  if ($this->activated)
193  $this->deprecated[] = $msg;
194  }
195 
199  function handleError($errno, $errstr, $errfile, $errline)
200  {
201  if ($this->activated && ($errno & error_reporting())) {
202  // NOTE: we only store relative pathnames
203  $this->errors[] = compact('errno', 'errstr', 'errfile', 'errline');
204  }
205  if ($errno == E_USER_ERROR) {
206  $trace = true;
207  if (substr($errstr, 0, '8') == 'notrace:') {
208  $trace = false;
209  $errstr = substr($errstr, 8);
210  }
211  echo sprintf(_XOOPS_FATAL_MESSAGE, $errstr);
212  if ($trace && function_exists('debug_backtrace')) {
213  echo "<div style='color:#f0f0f0;background-color:#f0f0f0'>" . _XOOPS_FATAL_BACKTRACE . ":<br />";
214  $trace = debug_backtrace();
215  array_shift($trace);
216  foreach ($trace as $step) {
217  if (isset($step['file'])) {
218  echo $this->sanitizePath($step['file']);
219  echo ' (' . $step['line'] . ")\n<br />";
220  }
221  }
222  echo '</div>';
223  }
224  exit();
225  }
226  }
227 
232  function sanitizePath($path)
233  {
234  $path = str_replace(array('\\' , XOOPS_ROOT_PATH , str_replace('\\', '/', realpath(XOOPS_ROOT_PATH))), array('/' , '' , ''), $path);
235  return $path;
236  }
237 
241  function render($output)
242  {
243  global $xoopsUser;
244  if (!$this->activated) {
245  return $output;
246  }
247 
248  $log = $this->dump($this->usePopup ? 'popup' : '');
249  $this->renderingEnabled = $this->activated = false;
250  $pattern = '<!--{xo-logger-output}-->';
251  $pos = strpos($output, $pattern);
252  if ($pos !== false) {
253  return substr($output, 0, $pos) . $log . substr($output, $pos + strlen($pattern));
254  } else {
255  return $output . $log;
256  }
257  }
258 
264  function dump($mode = '')
265  {
266  include XOOPS_ROOT_PATH . '/class/logger/render.php';
267  return $ret;
268  }
269 
277  function dumpTime($name = 'XOOPS', $unset = false)
278  {
279  if (!$this->activated)
280  return null;
281 
282  if (!isset($this->logstart[$name])) {
283  return 0;
284  }
285  $stop = isset($this->logend[$name]) ? $this->logend[$name] : $this->microtime();
286  $start = $this->logstart[$name];
287 
288  if ($unset) {
289  unset($this->logstart[$name]);
290  }
291 
292  return $stop - $start;
293  }
294 
305  function triggerError($errkey = 0, $errStr = '', $errFile = '', $errLine = '', $errNo = 0)
306  {
307  $GLOBALS['xoopsLogger']->addDeprecated('\'$xoopsLogger->triggerError();\' is deprecated since XOOPS 2.5.4');
308 
309  if (!empty($errStr)) {
310  $errStr = sprintf($errStr,$errkey );
311  }
312  $errFile = $this->sanitizePath($errFile);
313  $this->handleError($errNo, $errStr, $errFile, $errLine);
314  }
315 
321  function dumpAll()
322  {
323  $GLOBALS['xoopsLogger']->addDeprecated('\'$xoopsLogger->dumpAll();\' is deprecated since XOOPS 2.5.4, please use \'$xoopsLogger->dump(\'\');\' instead.');
324 
325  return $this->dump('');
326  }
327 
333  function dumpBlocks()
334  {
335  $GLOBALS['xoopsLogger']->addDeprecated('\'$xoopsLogger->dumpBlocks();\' is deprecated since XOOPS 2.5.4, please use \'$xoopsLogger->dump(\'blocks\');\' instead.');
336 
337  return $this->dump('blocks');
338  }
339 
345  function dumpExtra()
346  {
347  $GLOBALS['xoopsLogger']->addDeprecated('\'$xoopsLogger->dumpExtra();\' is deprecated since XOOPS 2.5.4, please use \'$xoopsLogger->dump(\'extra\');\' instead.');
348 
349  return $this->dump('extra');
350  }
351 
357  function dumpQueries()
358  {
359  $GLOBALS['xoopsLogger']->addDeprecated('\'$xoopsLogger->dumpQueries();\' is deprecated since XOOPS 2.5.4, please use \'$xoopsLogger->dump(\'queries\');\' instead.');
360 
361  return $this->dump('queries');
362  }
366 }
367 
377 function XoopsErrorHandler_HandleError($errNo, $errStr, $errFile, $errLine, $errContext = null)
378 {
379  // We don't want every error to come through this will help speed things up'
380  if ($errNo == '2048') {
381  return true;
382  }
383  // XOOPS should always be STRICT compliant thus the above lines makes no sense and will be removed! -- Added by Taiwen Jiang
384  $logger =& XoopsLogger::getInstance();
385  $logger->handleError($errNo, $errStr, $errFile, $errLine, $errContext);
386 }
387 
388 ?>