Showing help when calling script/setup without parameters.
[akelos.git] / vendor / pear / Log.php
bloba2c8dca76131fab7e9442029a12d32435e65c593
1 <?php
2 /**
3 * $Header: /repository/pear/Log/Log.php,v 1.51 2005/09/06 05:54:36 jon Exp $
4 * $Horde: horde/lib/Log.php,v 1.15 2000/06/29 23:39:45 jon Exp $
6 * @version $Revision: 1.51 $
7 * @package Log
8 */
10 define('PEAR_LOG_EMERG', 0); /** System is unusable */
11 define('PEAR_LOG_ALERT', 1); /** Immediate action required */
12 define('PEAR_LOG_CRIT', 2); /** Critical conditions */
13 define('PEAR_LOG_ERR', 3); /** Error conditions */
14 define('PEAR_LOG_WARNING', 4); /** Warning conditions */
15 define('PEAR_LOG_NOTICE', 5); /** Normal but significant */
16 define('PEAR_LOG_INFO', 6); /** Informational */
17 define('PEAR_LOG_DEBUG', 7); /** Debug-level messages */
19 define('PEAR_LOG_ALL', bindec('11111111')); /** All messages */
20 define('PEAR_LOG_NONE', bindec('00000000')); /** No message */
22 /* Log types for PHP's native error_log() function. */
23 define('PEAR_LOG_TYPE_SYSTEM', 0); /** Use PHP's system logger */
24 define('PEAR_LOG_TYPE_MAIL', 1); /** Use PHP's mail() function */
25 define('PEAR_LOG_TYPE_DEBUG', 2); /** Use PHP's debugging connection */
26 define('PEAR_LOG_TYPE_FILE', 3); /** Append to a file */
28 /**
29 * The Log:: class implements both an abstraction for various logging
30 * mechanisms and the Subject end of a Subject-Observer pattern.
32 * @author Chuck Hagenbuch <chuck@horde.org>
33 * @author Jon Parise <jon@php.net>
34 * @since Horde 1.3
35 * @package Log
37 class Log
39 /**
40 * Indicates whether or not the log can been opened / connected.
42 * @var boolean
43 * @access private
45 var $_opened = false;
47 /**
48 * Instance-specific unique identification number.
50 * @var integer
51 * @access private
53 var $_id = 0;
55 /**
56 * The label that uniquely identifies this set of log messages.
58 * @var string
59 * @access private
61 var $_ident = '';
63 /**
64 * The default priority to use when logging an event.
66 * @var integer
67 * @access private
69 var $_priority = PEAR_LOG_INFO;
71 /**
72 * The bitmask of allowed log levels.
73 * @var integer
74 * @access private
76 var $_mask = PEAR_LOG_ALL;
78 /**
79 * Holds all Log_observer objects that wish to be notified of new messages.
81 * @var array
82 * @access private
84 var $_listeners = array();
87 /**
88 * Attempts to return a concrete Log instance of type $handler.
90 * @param string $handler The type of concrete Log subclass to return.
91 * Attempt to dynamically include the code for
92 * this subclass. Currently, valid values are
93 * 'console', 'syslog', 'sql', 'file', and 'mcal'.
95 * @param string $name The name of the actually log file, table, or
96 * other specific store to use. Defaults to an
97 * empty string, with which the subclass will
98 * attempt to do something intelligent.
100 * @param string $ident The identity reported to the log system.
102 * @param array $conf A hash containing any additional configuration
103 * information that a subclass might need.
105 * @param int $level Log messages up to and including this level.
107 * @return object Log The newly created concrete Log instance, or an
108 * false on an error.
109 * @access public
110 * @since Log 1.0
112 function &factory($handler, $name = '', $ident = '', $conf = array(),
113 $level = PEAR_LOG_DEBUG)
115 $handler = strtolower($handler);
116 $class = 'Log_' . $handler;
117 $classfile = 'Log/' . $handler . '.php';
120 * Attempt to include our version of the named class, but don't treat
121 * a failure as fatal. The caller may have already included their own
122 * version of the named class.
124 if (!class_exists($class)) {
125 @include_once $classfile;
128 /* If the class exists, return a new instance of it. */
129 if (class_exists($class)) {
130 $obj = new $class($name, $ident, $conf, $level);
131 return $obj;
134 return false;
138 * Attempts to return a reference to a concrete Log instance of type
139 * $handler, only creating a new instance if no log instance with the same
140 * parameters currently exists.
142 * You should use this if there are multiple places you might create a
143 * logger, you don't want to create multiple loggers, and you don't want to
144 * check for the existence of one each time. The singleton pattern does all
145 * the checking work for you.
147 * <b>You MUST call this method with the $var = &Log::singleton() syntax.
148 * Without the ampersand (&) in front of the method name, you will not get
149 * a reference, you will get a copy.</b>
151 * @param string $handler The type of concrete Log subclass to return.
152 * Attempt to dynamically include the code for
153 * this subclass. Currently, valid values are
154 * 'console', 'syslog', 'sql', 'file', and 'mcal'.
156 * @param string $name The name of the actually log file, table, or
157 * other specific store to use. Defaults to an
158 * empty string, with which the subclass will
159 * attempt to do something intelligent.
161 * @param string $ident The identity reported to the log system.
163 * @param array $conf A hash containing any additional configuration
164 * information that a subclass might need.
166 * @param int $level Log messages up to and including this level.
168 * @return object Log The newly created concrete Log instance, or an
169 * false on an error.
170 * @access public
171 * @since Log 1.0
173 function &singleton($handler, $name = '', $ident = '', $conf = array(),
174 $level = PEAR_LOG_DEBUG)
176 static $instances;
177 if (!isset($instances)) $instances = array();
179 $signature = serialize(array($handler, $name, $ident, $conf, $level));
180 if (!isset($instances[$signature])) {
181 $instances[$signature] = &Log::factory($handler, $name, $ident,
182 $conf, $level);
185 return $instances[$signature];
189 * Abstract implementation of the open() method.
190 * @since Log 1.0
192 function open()
194 return false;
198 * Abstract implementation of the close() method.
199 * @since Log 1.0
201 function close()
203 return false;
207 * Abstract implementation of the flush() method.
208 * @since Log 1.8.2
210 function flush()
212 return false;
216 * Abstract implementation of the log() method.
217 * @since Log 1.0
219 function log($message, $priority = null)
221 return false;
225 * A convenience function for logging a emergency event. It will log a
226 * message at the PEAR_LOG_EMERG log level.
228 * @param mixed $message String or object containing the message
229 * to log.
231 * @return boolean True if the message was successfully logged.
233 * @access public
234 * @since Log 1.7.0
236 function emerg($message)
238 return $this->log($message, PEAR_LOG_EMERG);
242 * A convenience function for logging an alert event. It will log a
243 * message at the PEAR_LOG_ALERT log level.
245 * @param mixed $message String or object containing the message
246 * to log.
248 * @return boolean True if the message was successfully logged.
250 * @access public
251 * @since Log 1.7.0
253 function alert($message)
255 return $this->log($message, PEAR_LOG_ALERT);
259 * A convenience function for logging a critical event. It will log a
260 * message at the PEAR_LOG_CRIT log level.
262 * @param mixed $message String or object containing the message
263 * to log.
265 * @return boolean True if the message was successfully logged.
267 * @access public
268 * @since Log 1.7.0
270 function crit($message)
272 return $this->log($message, PEAR_LOG_CRIT);
276 * A convenience function for logging a error event. It will log a
277 * message at the PEAR_LOG_ERR log level.
279 * @param mixed $message String or object containing the message
280 * to log.
282 * @return boolean True if the message was successfully logged.
284 * @access public
285 * @since Log 1.7.0
287 function err($message)
289 return $this->log($message, PEAR_LOG_ERR);
293 * A convenience function for logging a warning event. It will log a
294 * message at the PEAR_LOG_WARNING log level.
296 * @param mixed $message String or object containing the message
297 * to log.
299 * @return boolean True if the message was successfully logged.
301 * @access public
302 * @since Log 1.7.0
304 function warning($message)
306 return $this->log($message, PEAR_LOG_WARNING);
310 * A convenience function for logging a notice event. It will log a
311 * message at the PEAR_LOG_NOTICE log level.
313 * @param mixed $message String or object containing the message
314 * to log.
316 * @return boolean True if the message was successfully logged.
318 * @access public
319 * @since Log 1.7.0
321 function notice($message)
323 return $this->log($message, PEAR_LOG_NOTICE);
327 * A convenience function for logging a information event. It will log a
328 * message at the PEAR_LOG_INFO log level.
330 * @param mixed $message String or object containing the message
331 * to log.
333 * @return boolean True if the message was successfully logged.
335 * @access public
336 * @since Log 1.7.0
338 function info($message)
340 return $this->log($message, PEAR_LOG_INFO);
344 * A convenience function for logging a debug event. It will log a
345 * message at the PEAR_LOG_DEBUG log level.
347 * @param mixed $message String or object containing the message
348 * to log.
350 * @return boolean True if the message was successfully logged.
352 * @access public
353 * @since Log 1.7.0
355 function debug($message)
357 return $this->log($message, PEAR_LOG_DEBUG);
361 * Returns the string representation of the message data.
363 * If $message is an object, _extractMessage() will attempt to extract
364 * the message text using a known method (such as a PEAR_Error object's
365 * getMessage() method). If a known method, cannot be found, the
366 * serialized representation of the object will be returned.
368 * If the message data is already a string, it will be returned unchanged.
370 * @param mixed $message The original message data. This may be a
371 * string or any object.
373 * @return string The string representation of the message.
375 * @access private
377 function _extractMessage($message)
380 * If we've been given an object, attempt to extract the message using
381 * a known method. If we can't find such a method, default to the
382 * "human-readable" version of the object.
384 * We also use the human-readable format for arrays.
386 if (is_object($message)) {
387 if (method_exists($message, 'getmessage')) {
388 $message = $message->getMessage();
389 } else if (method_exists($message, 'tostring')) {
390 $message = $message->toString();
391 } else if (method_exists($message, '__tostring')) {
392 if (version_compare(PHP_VERSION, '5.0.0', 'ge')) {
393 $message = (string)$message;
394 } else {
395 $message = $message->__toString();
397 } else {
398 $message = print_r($message, true);
400 } else if (is_array($message)) {
401 if (isset($message['message'])) {
402 $message = $message['message'];
403 } else {
404 $message = print_r($message, true);
408 /* Otherwise, we assume the message is a string. */
409 return $message;
413 * Returns the string representation of a PEAR_LOG_* integer constant.
415 * @param int $priority A PEAR_LOG_* integer constant.
417 * @return string The string representation of $level.
419 * @since Log 1.0
421 function priorityToString($priority)
423 $levels = array(
424 PEAR_LOG_EMERG => 'emergency',
425 PEAR_LOG_ALERT => 'alert',
426 PEAR_LOG_CRIT => 'critical',
427 PEAR_LOG_ERR => 'error',
428 PEAR_LOG_WARNING => 'warning',
429 PEAR_LOG_NOTICE => 'notice',
430 PEAR_LOG_INFO => 'info',
431 PEAR_LOG_DEBUG => 'debug'
434 return $levels[$priority];
438 * Returns the the PEAR_LOG_* integer constant for the given string
439 * representation of a priority name. This function performs a
440 * case-insensitive search.
442 * @param string $name String containing a priority name.
444 * @return string The PEAR_LOG_* integer contstant corresponding
445 * the the specified priority name.
447 * @since Log 1.9.0
449 function stringToPriority($name)
451 $levels = array(
452 'emergency' => PEAR_LOG_EMERG,
453 'alert' => PEAR_LOG_ALERT,
454 'critical' => PEAR_LOG_CRIT,
455 'error' => PEAR_LOG_ERR,
456 'warning' => PEAR_LOG_WARNING,
457 'notice' => PEAR_LOG_NOTICE,
458 'info' => PEAR_LOG_INFO,
459 'debug' => PEAR_LOG_DEBUG
462 return $levels[strtolower($name)];
466 * Calculate the log mask for the given priority.
468 * @param integer $priority The priority whose mask will be calculated.
470 * @return integer The calculated log mask.
472 * @access public
473 * @since Log 1.7.0
475 function MASK($priority)
477 return (1 << $priority);
481 * Calculate the log mask for all priorities up to the given priority.
483 * @param integer $priority The maximum priority covered by this mask.
485 * @return integer The calculated log mask.
487 * @access public
488 * @since Log 1.7.0
490 function UPTO($priority)
492 return ((1 << ($priority + 1)) - 1);
496 * Set and return the level mask for the current Log instance.
498 * @param integer $mask A bitwise mask of log levels.
500 * @return integer The current level mask.
502 * @access public
503 * @since Log 1.7.0
505 function setMask($mask)
507 $this->_mask = $mask;
509 return $this->_mask;
513 * Returns the current level mask.
515 * @return interger The current level mask.
517 * @access public
518 * @since Log 1.7.0
520 function getMask()
522 return $this->_mask;
526 * Check if the given priority is included in the current level mask.
528 * @param integer $priority The priority to check.
530 * @return boolean True if the given priority is included in the current
531 * log mask.
533 * @access private
534 * @since Log 1.7.0
536 function _isMasked($priority)
538 return (Log::MASK($priority) & $this->_mask);
542 * Returns the current default priority.
544 * @return integer The current default priority.
546 * @access public
547 * @since Log 1.8.4
549 function getPriority()
551 return $this->_priority;
555 * Sets the default priority to the specified value.
557 * @param integer $priority The new default priority.
559 * @access public
560 * @since Log 1.8.4
562 function setPriority($priority)
564 $this->_priority = $priority;
568 * Adds a Log_observer instance to the list of observers that are listening
569 * for messages emitted by this Log instance.
571 * @param object $observer The Log_observer instance to attach as a
572 * listener.
574 * @param boolean True if the observer is successfully attached.
576 * @access public
577 * @since Log 1.0
579 function attach(&$observer)
581 if (!is_a($observer, 'Log_observer')) {
582 return false;
585 $this->_listeners[$observer->_id] = &$observer;
587 return true;
591 * Removes a Log_observer instance from the list of observers.
593 * @param object $observer The Log_observer instance to detach from
594 * the list of listeners.
596 * @param boolean True if the observer is successfully detached.
598 * @access public
599 * @since Log 1.0
601 function detach($observer)
603 if (!is_a($observer, 'Log_observer') ||
604 !isset($this->_listeners[$observer->_id])) {
605 return false;
608 unset($this->_listeners[$observer->_id]);
610 return true;
614 * Informs each registered observer instance that a new message has been
615 * logged.
617 * @param array $event A hash describing the log event.
619 * @access private
621 function _announce($event)
623 foreach ($this->_listeners as $id => $listener) {
624 if ($event['priority'] <= $this->_listeners[$id]->_priority) {
625 $this->_listeners[$id]->notify($event);
631 * Indicates whether this is a composite class.
633 * @return boolean True if this is a composite class.
635 * @access public
636 * @since Log 1.0
638 function isComposite()
640 return false;
644 * Sets this Log instance's identification string.
646 * @param string $ident The new identification string.
648 * @access public
649 * @since Log 1.6.3
651 function setIdent($ident)
653 $this->_ident = $ident;
657 * Returns the current identification string.
659 * @return string The current Log instance's identification string.
661 * @access public
662 * @since Log 1.6.3
664 function getIdent()
666 return $this->_ident;