[795] | 1 | <?php |
---|
| 2 | /** |
---|
| 3 | * $Header: /repository/pear/Log/Log/daemon.php,v 1.3 2008/01/19 22:12:16 jon Exp $ |
---|
| 4 | * |
---|
| 5 | * @version $Revision: 1.3 $ |
---|
| 6 | * @package Log |
---|
| 7 | */ |
---|
| 8 | |
---|
| 9 | /** |
---|
| 10 | * The Log_daemon class is a concrete implementation of the Log:: |
---|
| 11 | * abstract class which sends messages to syslog daemon on UNIX-like machines. |
---|
| 12 | * This class uses the syslog protocol: http://www.ietf.org/rfc/rfc3164.txt |
---|
| 13 | * |
---|
| 14 | * @author Bart van der Schans <schans@dds.nl> |
---|
| 15 | * @version $Revision: 1.3 $ |
---|
| 16 | * @package Log |
---|
| 17 | */ |
---|
| 18 | class Log_daemon extends Log |
---|
| 19 | { |
---|
| 20 | /** |
---|
| 21 | * Integer holding the log facility to use. |
---|
| 22 | * @var string |
---|
| 23 | */ |
---|
| 24 | var $_name = LOG_DAEMON; |
---|
| 25 | |
---|
| 26 | /** |
---|
| 27 | * Var holding the resource pointer to the socket |
---|
| 28 | * @var resource |
---|
| 29 | */ |
---|
| 30 | var $_socket; |
---|
| 31 | |
---|
| 32 | /** |
---|
| 33 | * The ip address or servername |
---|
| 34 | * @see http://www.php.net/manual/en/transports.php |
---|
| 35 | * @var string |
---|
| 36 | */ |
---|
| 37 | var $_ip = '127.0.0.1'; |
---|
| 38 | |
---|
| 39 | /** |
---|
| 40 | * Protocol to use (tcp, udp, etc.) |
---|
| 41 | * @see http://www.php.net/manual/en/transports.php |
---|
| 42 | * @var string |
---|
| 43 | */ |
---|
| 44 | var $_proto = 'udp'; |
---|
| 45 | |
---|
| 46 | /** |
---|
| 47 | * Port to connect to |
---|
| 48 | * @var int |
---|
| 49 | */ |
---|
| 50 | var $_port = 514; |
---|
| 51 | |
---|
| 52 | /** |
---|
| 53 | * Maximum message length in bytes |
---|
| 54 | * @var int |
---|
| 55 | */ |
---|
| 56 | var $_maxsize = 4096; |
---|
| 57 | |
---|
| 58 | /** |
---|
| 59 | * Socket timeout in seconds |
---|
| 60 | * @var int |
---|
| 61 | */ |
---|
| 62 | var $_timeout = 1; |
---|
| 63 | |
---|
| 64 | |
---|
| 65 | /** |
---|
| 66 | * Constructs a new syslog object. |
---|
| 67 | * |
---|
| 68 | * @param string $name The syslog facility. |
---|
| 69 | * @param string $ident The identity string. |
---|
| 70 | * @param array $conf The configuration array. |
---|
| 71 | * @param int $maxLevel Maximum level at which to log. |
---|
| 72 | * @access public |
---|
| 73 | */ |
---|
| 74 | function Log_daemon($name, $ident = '', $conf = array(), |
---|
| 75 | $level = PEAR_LOG_DEBUG) |
---|
| 76 | { |
---|
| 77 | /* Ensure we have a valid integer value for $name. */ |
---|
| 78 | if (empty($name) || !is_int($name)) { |
---|
| 79 | $name = LOG_SYSLOG; |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | $this->_id = md5(microtime()); |
---|
| 83 | $this->_name = $name; |
---|
| 84 | $this->_ident = $ident; |
---|
| 85 | $this->_mask = Log::UPTO($level); |
---|
| 86 | |
---|
| 87 | if (isset($conf['ip'])) { |
---|
| 88 | $this->_ip = $conf['ip']; |
---|
| 89 | } |
---|
| 90 | if (isset($conf['proto'])) { |
---|
| 91 | $this->_proto = $conf['proto']; |
---|
| 92 | } |
---|
| 93 | if (isset($conf['port'])) { |
---|
| 94 | $this->_port = $conf['port']; |
---|
| 95 | } |
---|
| 96 | if (isset($conf['maxsize'])) { |
---|
| 97 | $this->_maxsize = $conf['maxsize']; |
---|
| 98 | } |
---|
| 99 | if (isset($conf['timeout'])) { |
---|
| 100 | $this->_timeout = $conf['timeout']; |
---|
| 101 | } |
---|
| 102 | $this->_proto = $this->_proto . '://'; |
---|
| 103 | |
---|
| 104 | register_shutdown_function(array(&$this, '_Log_daemon')); |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | /** |
---|
| 108 | * Destructor. |
---|
| 109 | * |
---|
| 110 | * @access private |
---|
| 111 | */ |
---|
| 112 | function _Log_daemon() |
---|
| 113 | { |
---|
| 114 | $this->close(); |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | /** |
---|
| 118 | * Opens a connection to the system logger, if it has not already |
---|
| 119 | * been opened. This is implicitly called by log(), if necessary. |
---|
| 120 | * @access public |
---|
| 121 | */ |
---|
| 122 | function open() |
---|
| 123 | { |
---|
| 124 | if (!$this->_opened) { |
---|
| 125 | $this->_opened = (bool)($this->_socket = @fsockopen( |
---|
| 126 | $this->_proto . $this->_ip, |
---|
| 127 | $this->_port, |
---|
| 128 | $errno, |
---|
| 129 | $errstr, |
---|
| 130 | $this->_timeout)); |
---|
| 131 | } |
---|
| 132 | return $this->_opened; |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | /** |
---|
| 136 | * Closes the connection to the system logger, if it is open. |
---|
| 137 | * @access public |
---|
| 138 | */ |
---|
| 139 | function close() |
---|
| 140 | { |
---|
| 141 | if ($this->_opened) { |
---|
| 142 | $this->_opened = false; |
---|
| 143 | return fclose($this->_socket); |
---|
| 144 | } |
---|
| 145 | return true; |
---|
| 146 | } |
---|
| 147 | |
---|
| 148 | /** |
---|
| 149 | * Sends $message to the currently open syslog connection. Calls |
---|
| 150 | * open() if necessary. Also passes the message along to any Log_observer |
---|
| 151 | * instances that are observing this Log. |
---|
| 152 | * |
---|
| 153 | * @param string $message The textual message to be logged. |
---|
| 154 | * @param int $priority (optional) The priority of the message. Valid |
---|
| 155 | * values are: LOG_EMERG, LOG_ALERT, LOG_CRIT, |
---|
| 156 | * LOG_ERR, LOG_WARNING, LOG_NOTICE, LOG_INFO, |
---|
| 157 | * and LOG_DEBUG. The default is LOG_INFO. |
---|
| 158 | * @access public |
---|
| 159 | */ |
---|
| 160 | function log($message, $priority = null) |
---|
| 161 | { |
---|
| 162 | /* If a priority hasn't been specified, use the default value. */ |
---|
| 163 | if ($priority === null) { |
---|
| 164 | $priority = $this->_priority; |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | /* Abort early if the priority is above the maximum logging level. */ |
---|
| 168 | if (!$this->_isMasked($priority)) { |
---|
| 169 | return false; |
---|
| 170 | } |
---|
| 171 | |
---|
| 172 | /* If the connection isn't open and can't be opened, return failure. */ |
---|
| 173 | if (!$this->_opened && !$this->open()) { |
---|
| 174 | return false; |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | /* Extract the string representation of the message. */ |
---|
| 178 | $message = $this->_extractMessage($message); |
---|
| 179 | |
---|
| 180 | /* Set the facility level. */ |
---|
| 181 | $facility_level = intval($this->_name) + |
---|
| 182 | intval($this->_toSyslog($priority)); |
---|
| 183 | |
---|
| 184 | /* Prepend ident info. */ |
---|
| 185 | if (!empty($this->_ident)) { |
---|
| 186 | $message = $this->_ident . ' ' . $message; |
---|
| 187 | } |
---|
| 188 | |
---|
| 189 | /* Check for message length. */ |
---|
| 190 | if (strlen($message) > $this->_maxsize) { |
---|
| 191 | $message = substr($message, 0, ($this->_maxsize) - 10) . ' [...]'; |
---|
| 192 | } |
---|
| 193 | |
---|
| 194 | /* Write to socket. */ |
---|
| 195 | fwrite($this->_socket, '<' . $facility_level . '>' . $message . "\n"); |
---|
| 196 | |
---|
| 197 | $this->_announce(array('priority' => $priority, 'message' => $message)); |
---|
| 198 | } |
---|
| 199 | |
---|
| 200 | /** |
---|
| 201 | * Converts a PEAR_LOG_* constant into a syslog LOG_* constant. |
---|
| 202 | * |
---|
| 203 | * This function exists because, under Windows, not all of the LOG_* |
---|
| 204 | * constants have unique values. Instead, the PEAR_LOG_* were introduced |
---|
| 205 | * for global use, with the conversion to the LOG_* constants kept local to |
---|
| 206 | * to the syslog driver. |
---|
| 207 | * |
---|
| 208 | * @param int $priority PEAR_LOG_* value to convert to LOG_* value. |
---|
| 209 | * |
---|
| 210 | * @return The LOG_* representation of $priority. |
---|
| 211 | * |
---|
| 212 | * @access private |
---|
| 213 | */ |
---|
| 214 | function _toSyslog($priority) |
---|
| 215 | { |
---|
| 216 | static $priorities = array( |
---|
| 217 | PEAR_LOG_EMERG => LOG_EMERG, |
---|
| 218 | PEAR_LOG_ALERT => LOG_ALERT, |
---|
| 219 | PEAR_LOG_CRIT => LOG_CRIT, |
---|
| 220 | PEAR_LOG_ERR => LOG_ERR, |
---|
| 221 | PEAR_LOG_WARNING => LOG_WARNING, |
---|
| 222 | PEAR_LOG_NOTICE => LOG_NOTICE, |
---|
| 223 | PEAR_LOG_INFO => LOG_INFO, |
---|
| 224 | PEAR_LOG_DEBUG => LOG_DEBUG |
---|
| 225 | ); |
---|
| 226 | |
---|
| 227 | /* If we're passed an unknown priority, default to LOG_INFO. */ |
---|
| 228 | if (!is_int($priority) || !in_array($priority, $priorities)) { |
---|
| 229 | return LOG_INFO; |
---|
| 230 | } |
---|
| 231 | |
---|
| 232 | return $priorities[$priority]; |
---|
| 233 | } |
---|
| 234 | |
---|
| 235 | } |
---|