PageRenderTime 42ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/src/lib/Log/daemon.php

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