PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/php/Log/daemon.php

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