Automatically exported from code.google.com/p/planningalerts
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

231 lines
6.5 KiB

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