composite.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. /**
  3. * $Header: /repository/pear/Log/Log/composite.php,v 1.22 2004/01/19 08:02:40 jon Exp $
  4. * $Horde: horde/lib/Log/composite.php,v 1.2 2000/06/28 21:36:13 jon Exp $
  5. *
  6. * @version $Revision: 1.22 $
  7. * @package Log
  8. */
  9. /**
  10. * The Log_composite:: class implements a Composite pattern which
  11. * allows multiple Log implementations to receive the same events.
  12. *
  13. * @author Chuck Hagenbuch <chuck@horde.org>
  14. * @author Jon Parise <jon@php.net>
  15. *
  16. * @since Horde 1.3
  17. * @since Log 1.0
  18. * @package Log
  19. *
  20. * @example composite.php Using the composite handler.
  21. */
  22. class Log_composite extends Log
  23. {
  24. /**
  25. * Array holding all of the Log instances to which log events should be
  26. * sent.
  27. *
  28. * @var array
  29. * @access private
  30. */
  31. var $_children = array();
  32. /**
  33. * Constructs a new composite Log object.
  34. *
  35. * @param boolean $name This parameter is ignored.
  36. * @param boolean $ident This parameter is ignored.
  37. * @param boolean $conf This parameter is ignored.
  38. * @param boolean $level This parameter is ignored.
  39. *
  40. * @access public
  41. */
  42. function Log_composite($name = false, $ident = false, $conf = false,
  43. $level = PEAR_LOG_DEBUG)
  44. {
  45. }
  46. /**
  47. * Opens the child connections.
  48. *
  49. * @access public
  50. */
  51. function open()
  52. {
  53. if (!$this->_opened) {
  54. foreach ($this->_children as $id => $child) {
  55. $this->_children[$id]->open();
  56. }
  57. }
  58. }
  59. /**
  60. * Closes any child instances.
  61. *
  62. * @access public
  63. */
  64. function close()
  65. {
  66. if ($this->_opened) {
  67. foreach ($this->_children as $id => $child) {
  68. $this->_children[$id]->close();
  69. }
  70. }
  71. }
  72. /**
  73. * Flushes all open child instances.
  74. *
  75. * @access public
  76. * @since Log 1.8.2
  77. */
  78. function flush()
  79. {
  80. if ($this->_opened) {
  81. foreach ($this->_children as $id => $child) {
  82. $this->_children[$id]->flush();
  83. }
  84. }
  85. }
  86. /**
  87. * Sends $message and $priority to each child of this composite.
  88. *
  89. * @param mixed $message String or object containing the message
  90. * to log.
  91. * @param string $priority (optional) The priority of the message.
  92. * Valid values are: PEAR_LOG_EMERG,
  93. * PEAR_LOG_ALERT, PEAR_LOG_CRIT,
  94. * PEAR_LOG_ERR, PEAR_LOG_WARNING,
  95. * PEAR_LOG_NOTICE, PEAR_LOG_INFO, and
  96. * PEAR_LOG_DEBUG.
  97. *
  98. * @return boolean True if the entry is successfully logged.
  99. *
  100. * @access public
  101. */
  102. function log($message, $priority = null)
  103. {
  104. /* If a priority hasn't been specified, use the default value. */
  105. if ($priority === null) {
  106. $priority = $this->_priority;
  107. }
  108. foreach ($this->_children as $id => $child) {
  109. $this->_children[$id]->log($message, $priority);
  110. }
  111. $this->_announce(array('priority' => $priority, 'message' => $message));
  112. return true;
  113. }
  114. /**
  115. * Returns true if this is a composite.
  116. *
  117. * @return boolean True if this is a composite class.
  118. *
  119. * @access public
  120. */
  121. function isComposite()
  122. {
  123. return true;
  124. }
  125. /**
  126. * Sets this identification string for all of this composite's children.
  127. *
  128. * @param string $ident The new identification string.
  129. *
  130. * @access public
  131. * @since Log 1.6.7
  132. */
  133. function setIdent($ident)
  134. {
  135. foreach ($this->_children as $id => $child) {
  136. $this->_children[$id]->setIdent($ident);
  137. }
  138. }
  139. /**
  140. * Adds a Log instance to the list of children.
  141. *
  142. * @param object $child The Log instance to add.
  143. *
  144. * @return boolean True if the Log instance was successfully added.
  145. *
  146. * @access public
  147. */
  148. function addChild(&$child)
  149. {
  150. /* Make sure this is a Log instance. */
  151. if (!is_a($child, 'Log')) {
  152. return false;
  153. }
  154. $this->_children[$child->_id] = &$child;
  155. return true;
  156. }
  157. /**
  158. * Removes a Log instance from the list of children.
  159. *
  160. * @param object $child The Log instance to remove.
  161. *
  162. * @return boolean True if the Log instance was successfully removed.
  163. *
  164. * @access public
  165. */
  166. function removeChild($child)
  167. {
  168. if (!is_a($child, 'Log') || !isset($this->_children[$child->_id])) {
  169. return false;
  170. }
  171. unset($this->_children[$child->_id]);
  172. return true;
  173. }
  174. }
  175. ?>