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

