Hoa central
Log.php
Go to the documentation of this file.
1 <?php
2 
37 namespace {
38 
39 from('Hoa')
40 
41 
44 -> import('Log.Exception')
45 
49 -> import('Log.Backtrace.~')
50 
54 -> import('Tree.Visitor.Dump');
55 
56 }
57 
58 namespace Hoa\Log {
59 
70 class Log implements \Hoa\Core\Event\Source {
71 
79  const EMERGENCY = 1;
80 
86  const ALERT = 2;
87 
93  const CRITICAL = 4;
94 
100  const ERROR = 8;
101 
107  const WARNING = 16;
108 
114  const NOTICE = 32;
115 
121  const INFORMATIONAL = 64;
122 
128  const DEBUG = 128;
129 
135  const TEST = 256;
136 
142  const STACK_TIMESTAMP = 'timestamp';
143 
149  const STACK_MESSAGE = 'message';
150 
156  const STACK_PRIORITY = 'priority';
157 
163  const STACK_MEMORY = 'memory';
164 
170  const STACK_MEMORY_PEAK = 'memory_peak';
171 
177  private static $_instances = null;
178 
184  private static $_currentId = null;
185 
191  protected $_stack = array();
192 
198  protected $_backtrace = null;
199 
205  protected $_filters = null;
206 
212  protected $_stackInfos = array();
213 
214 
215 
222  private function __construct ( ) {
223 
224  return;
225  }
226 
235  public static function getChannel ( $id = null ) {
236 
237  if(null === self::$_currentId && null === $id)
238  throw new Exception(
239  'Must precise a singleton index once.', 0);
240 
241  if(!isset(self::$_instances[$id])) {
242 
243  self::$_instances[$id] = new self();
244  \Hoa\Core\Event::register(
245  'hoa://Event/Log/' . $id,
246  self::$_instances[$id]
247  );
248  }
249 
250  if(null !== $id)
251  self::$_currentId = $id;
252 
253  $handle = self::$_instances[self::$_currentId];
254 
255  return $handle;
256  }
257 
265  public function accept ( $filter ) {
266 
267  if(null === $this->_filters)
268  return $this->_filters = $filter;
269 
270  $old = $this->_filters;
271  $this->_filters = $old | $filter;
272 
273  return $old;
274  }
275 
282  public function acceptAll ( ) {
283 
284  $old = $this->_filters;
285  $this->_filters = null;
286 
287  return $old;
288  }
289 
297  public function setFilter ( $filter ) {
298 
299  $old = $this->_filters;
300  $this->_filters = $filter;
301 
302  return $old;
303  }
304 
312  public function drop ( $filter ) {
313 
314  $old = $this->_filters;
315  $this->_filters = $old & ~$filter;
316 
317  return $old;
318  }
319 
326  public function dropAll ( ) {
327 
328  $old = $this->_filters;
329  $this->_filters = self::EMERGENCY
330  & self::ALERT
331  & self::CRITICAL
332  & self::ERROR
333  & self::WARNING
334  & self::NOTICE
335  & self::INFORMATIONAL
336  & self::DEBUG
337  & self::TEST;
338 
339  return $old;
340  }
341 
349  public function addStackInformations ( Array $stackInfos ) {
350 
351  foreach($stackInfos as $key => $value)
352  $this->addStackInformation($key, $value);
353 
354  return $this->getAddedStackInformations();
355  }
356 
365  public function addStackInformation ( $key, $value ) {
366 
367  $this->_stackInfos[$key] = $value;
368 
369  return $this->getAddedStackInformations();
370  }
371 
378  public function getAddedStackInformations ( ) {
379 
380  return $this->_stackInfos;
381  }
382 
393  public function log ( $message, $type = self::DEBUG, $extra = array() ) {
394 
395  $filters = $this->getFilters();
396 
397  if(null !== $filters && !($type & $filters))
398  return;
399 
400  $handle = $this->_stack[] = array_merge(
401  array(
402  self::STACK_TIMESTAMP => microtime(true),
403  self::STACK_MESSAGE => $message,
404  self::STACK_PRIORITY => $type,
405  self::STACK_MEMORY => memory_get_usage(),
406  self::STACK_MEMORY_PEAK => memory_get_peak_usage()
407  ),
408  $this->getAddedStackInformations(),
409  $extra
410  );
411 
412  \Hoa\Core\Event::notify(
413  'hoa://Event/Log/' . self::$_currentId,
414  $this,
415  new \Hoa\Core\Event\Bucket(array('log' => $handle))
416  );
417 
418  if($type & self::DEBUG) {
419 
420  if(null === $this->_backtrace)
421  $this->_backtrace = new Backtrace();
422 
423  $this->_backtrace->debug();
424  }
425 
426  return;
427  }
428 
435  public function getFilters ( ) {
436 
437  return $this->_filters;
438  }
439 
446  public function getBacktrace ( ) {
447 
448  return $this->_backtrace;
449  }
450 
457  public function getLogStack ( ) {
458 
459  return $this->_stack;
460  }
461 
469  public function typeAsString ( $type ) {
470 
471  switch($type) {
472 
473  case self::EMERGENCY:
474  return 'EMERGENCY';
475  break;
476 
477  case self::ALERT:
478  return 'ALERT';
479  break;
480 
481  case self::CRITICAL:
482  return 'CRITICAL';
483  break;
484 
485  case self::ERROR:
486  return 'ERROR';
487  break;
488 
489  case self::WARNING:
490  return 'WARNING';
491  break;
492 
493  case self::NOTICE:
494  return 'NOTICE';
495  break;
496 
497  case self::INFORMATIONAL:
498  return 'INFORMATIONAL';
499  break;
500 
501  case self::DEBUG:
502  return 'DEBUG';
503  break;
504 
505  default:
506  return 'unknown';
507  }
508  }
509 
516  public function __toString ( ) {
517 
518  return $this->getBacktrace()->__toString();
519  }
520 }
521 
522 }
523 
524 namespace {
525 
536 if(!function_exists('hlog')) {
537 function hlog ( $message, $type = \Hoa\Log::DEBUG, $extra = array() ) {
538 
539  return \Hoa\Log::getChannel()->log($message, $type, $extra);
540 }}
541 
542 }
543 
544 namespace {
545 
549 Hoa\Core\Consistency::flexEntity('Hoa\Log\Log');
550 
551 }
getLogStack()
Definition: Log.php:457
const EMERGENCY
Definition: Log.php:79
__construct()
Definition: Log.php:222
const ALERT
Definition: Log.php:86
const TEST
Definition: Log.php:135
const STACK_MESSAGE
Definition: Log.php:149
static getChannel($id=null)
Definition: Log.php:235
acceptAll()
Definition: Log.php:282
$_stackInfos
Definition: Log.php:212
static $_currentId
Definition: Log.php:184
const STACK_PRIORITY
Definition: Log.php:156
__toString()
Definition: Log.php:516
setFilter($filter)
Definition: Log.php:297
addStackInformations(Array $stackInfos)
Definition: Log.php:349
const STACK_MEMORY
Definition: Log.php:163
const INFORMATIONAL
Definition: Log.php:121
const ERROR
Definition: Log.php:100
const NOTICE
Definition: Log.php:114
static $_instances
Definition: Log.php:177
const STACK_TIMESTAMP
Definition: Log.php:142
const STACK_MEMORY_PEAK
Definition: Log.php:170
getBacktrace()
Definition: Log.php:446
dropAll()
Definition: Log.php:326
accept($filter)
Definition: Log.php:265
const DEBUG
Definition: Log.php:128
addStackInformation($key, $value)
Definition: Log.php:365
getFilters()
Definition: Log.php:435
$_backtrace
Definition: Log.php:198
drop($filter)
Definition: Log.php:312
const WARNING
Definition: Log.php:107
log($message, $type=self::DEBUG, $extra=array())
Definition: Log.php:393
const CRITICAL
Definition: Log.php:93
getAddedStackInformations()
Definition: Log.php:378
typeAsString($type)
Definition: Log.php:469