[795] | 1 | <?php |
---|
| 2 | /** |
---|
| 3 | * $Header: /repository/pear/Log/Log/composite.php,v 1.28 2006/06/29 07:12:34 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.28 $ |
---|
| 7 | * @package Log |
---|
| 8 | */ |
---|
| 9 | |
---|
| 10 | /** |
---|
| 11 | * The Log_composite:: class implements a Composite pattern which |
---|
| 12 | * allows multiple Log implementations to receive the same events. |
---|
| 13 | * |
---|
| 14 | * @author Chuck Hagenbuch <chuck@horde.org> |
---|
| 15 | * @author Jon Parise <jon@php.net> |
---|
| 16 | * |
---|
| 17 | * @since Horde 1.3 |
---|
| 18 | * @since Log 1.0 |
---|
| 19 | * @package Log |
---|
| 20 | * |
---|
| 21 | * @example composite.php Using the composite handler. |
---|
| 22 | */ |
---|
| 23 | class Log_composite extends Log |
---|
| 24 | { |
---|
| 25 | /** |
---|
| 26 | * Array holding all of the Log instances to which log events should be |
---|
| 27 | * sent. |
---|
| 28 | * |
---|
| 29 | * @var array |
---|
| 30 | * @access private |
---|
| 31 | */ |
---|
| 32 | var $_children = array(); |
---|
| 33 | |
---|
| 34 | |
---|
| 35 | /** |
---|
| 36 | * Constructs a new composite Log object. |
---|
| 37 | * |
---|
| 38 | * @param boolean $name This parameter is ignored. |
---|
| 39 | * @param boolean $ident This parameter is ignored. |
---|
| 40 | * @param boolean $conf This parameter is ignored. |
---|
| 41 | * @param boolean $level This parameter is ignored. |
---|
| 42 | * |
---|
| 43 | * @access public |
---|
| 44 | */ |
---|
| 45 | function Log_composite($name, $ident = '', $conf = array(), |
---|
| 46 | $level = PEAR_LOG_DEBUG) |
---|
| 47 | { |
---|
| 48 | $this->_ident = $ident; |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | /** |
---|
| 52 | * Opens all of the child instances. |
---|
| 53 | * |
---|
| 54 | * @return True if all of the child instances were successfully opened. |
---|
| 55 | * |
---|
| 56 | * @access public |
---|
| 57 | */ |
---|
| 58 | function open() |
---|
| 59 | { |
---|
| 60 | /* Attempt to open each of our children. */ |
---|
| 61 | $this->_opened = true; |
---|
| 62 | foreach ($this->_children as $id => $child) { |
---|
| 63 | $this->_opened &= $this->_children[$id]->open(); |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | /* If all children were opened, return success. */ |
---|
| 67 | return $this->_opened; |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | /** |
---|
| 71 | * Closes all of the child instances. |
---|
| 72 | * |
---|
| 73 | * @return True if all of the child instances were successfully closed. |
---|
| 74 | * |
---|
| 75 | * @access public |
---|
| 76 | */ |
---|
| 77 | function close() |
---|
| 78 | { |
---|
| 79 | /* Attempt to close each of our children. */ |
---|
| 80 | $closed = true; |
---|
| 81 | foreach ($this->_children as $id => $child) { |
---|
| 82 | $closed &= $this->_children[$id]->close(); |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | /* Track the _opened state for consistency. */ |
---|
| 86 | $this->_opened = false; |
---|
| 87 | |
---|
| 88 | /* If all children were closed, return success. */ |
---|
| 89 | return $closed; |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | /** |
---|
| 93 | * Flushes all child instances. It is assumed that all of the children |
---|
| 94 | * have been successfully opened. |
---|
| 95 | * |
---|
| 96 | * @return True if all of the child instances were successfully flushed. |
---|
| 97 | * |
---|
| 98 | * @access public |
---|
| 99 | * @since Log 1.8.2 |
---|
| 100 | */ |
---|
| 101 | function flush() |
---|
| 102 | { |
---|
| 103 | /* Attempt to flush each of our children. */ |
---|
| 104 | $flushed = true; |
---|
| 105 | foreach ($this->_children as $id => $child) { |
---|
| 106 | $flushed &= $this->_children[$id]->flush(); |
---|
| 107 | } |
---|
| 108 | |
---|
| 109 | /* If all children were flushed, return success. */ |
---|
| 110 | return $flushed; |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | /** |
---|
| 114 | * Sends $message and $priority to each child of this composite. If the |
---|
| 115 | * children aren't already open, they will be opened here. |
---|
| 116 | * |
---|
| 117 | * @param mixed $message String or object containing the message |
---|
| 118 | * to log. |
---|
| 119 | * @param string $priority (optional) The priority of the message. |
---|
| 120 | * Valid values are: PEAR_LOG_EMERG, |
---|
| 121 | * PEAR_LOG_ALERT, PEAR_LOG_CRIT, |
---|
| 122 | * PEAR_LOG_ERR, PEAR_LOG_WARNING, |
---|
| 123 | * PEAR_LOG_NOTICE, PEAR_LOG_INFO, and |
---|
| 124 | * PEAR_LOG_DEBUG. |
---|
| 125 | * |
---|
| 126 | * @return boolean True if the entry is successfully logged. |
---|
| 127 | * |
---|
| 128 | * @access public |
---|
| 129 | */ |
---|
| 130 | function log($message, $priority = null) |
---|
| 131 | { |
---|
| 132 | /* If a priority hasn't been specified, use the default value. */ |
---|
| 133 | if ($priority === null) { |
---|
| 134 | $priority = $this->_priority; |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | /* |
---|
| 138 | * If the handlers haven't been opened, attempt to open them now. |
---|
| 139 | * However, we don't treat failure to open all of the handlers as a |
---|
| 140 | * fatal error. We defer that consideration to the success of calling |
---|
| 141 | * each handler's log() method below. |
---|
| 142 | */ |
---|
| 143 | if (!$this->_opened) { |
---|
| 144 | $this->open(); |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | /* Attempt to log the event using each of the children. */ |
---|
| 148 | $success = true; |
---|
| 149 | foreach ($this->_children as $id => $child) { |
---|
| 150 | $success &= $this->_children[$id]->log($message, $priority); |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | $this->_announce(array('priority' => $priority, 'message' => $message)); |
---|
| 154 | |
---|
| 155 | /* Return success if all of the children logged the event. */ |
---|
| 156 | return $success; |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | /** |
---|
| 160 | * Returns true if this is a composite. |
---|
| 161 | * |
---|
| 162 | * @return boolean True if this is a composite class. |
---|
| 163 | * |
---|
| 164 | * @access public |
---|
| 165 | */ |
---|
| 166 | function isComposite() |
---|
| 167 | { |
---|
| 168 | return true; |
---|
| 169 | } |
---|
| 170 | |
---|
| 171 | /** |
---|
| 172 | * Sets this identification string for all of this composite's children. |
---|
| 173 | * |
---|
| 174 | * @param string $ident The new identification string. |
---|
| 175 | * |
---|
| 176 | * @access public |
---|
| 177 | * @since Log 1.6.7 |
---|
| 178 | */ |
---|
| 179 | function setIdent($ident) |
---|
| 180 | { |
---|
| 181 | /* Call our base class's setIdent() method. */ |
---|
| 182 | parent::setIdent($ident); |
---|
| 183 | |
---|
| 184 | /* ... and then call setIdent() on all of our children. */ |
---|
| 185 | foreach ($this->_children as $id => $child) { |
---|
| 186 | $this->_children[$id]->setIdent($ident); |
---|
| 187 | } |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | /** |
---|
| 191 | * Adds a Log instance to the list of children. |
---|
| 192 | * |
---|
| 193 | * @param object $child The Log instance to add. |
---|
| 194 | * |
---|
| 195 | * @return boolean True if the Log instance was successfully added. |
---|
| 196 | * |
---|
| 197 | * @access public |
---|
| 198 | */ |
---|
| 199 | function addChild(&$child) |
---|
| 200 | { |
---|
| 201 | /* Make sure this is a Log instance. */ |
---|
| 202 | if (!is_a($child, 'Log')) { |
---|
| 203 | return false; |
---|
| 204 | } |
---|
| 205 | |
---|
| 206 | $this->_children[$child->_id] = &$child; |
---|
| 207 | |
---|
| 208 | return true; |
---|
| 209 | } |
---|
| 210 | |
---|
| 211 | /** |
---|
| 212 | * Removes a Log instance from the list of children. |
---|
| 213 | * |
---|
| 214 | * @param object $child The Log instance to remove. |
---|
| 215 | * |
---|
| 216 | * @return boolean True if the Log instance was successfully removed. |
---|
| 217 | * |
---|
| 218 | * @access public |
---|
| 219 | */ |
---|
| 220 | function removeChild($child) |
---|
| 221 | { |
---|
| 222 | if (!is_a($child, 'Log') || !isset($this->_children[$child->_id])) { |
---|
| 223 | return false; |
---|
| 224 | } |
---|
| 225 | |
---|
| 226 | unset($this->_children[$child->_id]); |
---|
| 227 | |
---|
| 228 | return true; |
---|
| 229 | } |
---|
| 230 | |
---|
| 231 | } |
---|