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

/administrator/components/com_virtuemart/classes/Log/syslog.php

https://bitbucket.org/dgough/annamaria-daneswood-25102012
PHP | 177 lines | 68 code | 19 blank | 90 comment | 13 complexity | 8989c54f894392da7f1af2d305bc5cf0 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. if( !defined( '_VALID_MOS' ) && !defined( '_JEXEC' ) ) die( 'Direct Access to '.basename(__FILE__).' is not allowed.' );
  3. /**
  4. *
  5. * @version $Id: syslog.php 1336 2008-03-31 17:06:23Z soeren_nb $
  6. * @package VirtueMart
  7. * @subpackage Log
  8. * @copyright Copyright (C) 2004-2008 soeren - All rights reserved.
  9. * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
  10. * VirtueMart is free software. 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_virtuemart/COPYRIGHT.php for copyright notices and details.
  15. *
  16. * http://virtuemart.net
  17. */
  18. /**
  19. * $Header$
  20. * $Horde: horde/lib/Log/syslog.php,v 1.6 2000/06/28 21:36:13 jon Exp $
  21. *
  22. * @version $ Revision: 1.23 $
  23. * @package Log
  24. */
  25. /**
  26. * The vmLog_syslog class is a concrete implementation of the Log::
  27. * abstract class which sends messages to syslog on UNIX-like machines
  28. * (PHP emulates this with the Event Log on Windows machines).
  29. *
  30. * @author Chuck Hagenbuch <chuck@horde.org>
  31. * @since Horde 1.3
  32. * @since Log 1.0
  33. * @package Log
  34. *
  35. * @example syslog.php Using the syslog handler.
  36. */
  37. class vmLog_syslog extends vmLog
  38. {
  39. /**
  40. * Integer holding the log facility to use.
  41. * @var string
  42. * @access private
  43. */
  44. var $_name = vmLog_syslog;
  45. /**
  46. * Constructs a new syslog object.
  47. *
  48. * @param string $name The syslog facility.
  49. * @param string $ident The identity string.
  50. * @param array $conf The configuration array.
  51. * @param int $level Log messages up to and including this level.
  52. * @access public
  53. */
  54. function vmLog_syslog($name, $ident = '', $conf = array(),
  55. $level = PEAR_LOG_DEBUG)
  56. {
  57. /* Ensure we have a valid integer value for $name. */
  58. if (empty($name) || !is_int($name)) {
  59. $name = vmLog_syslog;
  60. }
  61. $this->_id = md5(microtime());
  62. $this->_name = $name;
  63. $this->_ident = $ident;
  64. $this->_mask = vmLog::UPTO($level);
  65. }
  66. /**
  67. * Opens a connection to the system logger, if it has not already
  68. * been opened. This is implicitly called by log(), if necessary.
  69. * @access public
  70. */
  71. function open()
  72. {
  73. if (!$this->_opened) {
  74. openlog($this->_ident, LOG_PID, $this->_name);
  75. $this->_opened = true;
  76. }
  77. return $this->_opened;
  78. }
  79. /**
  80. * Closes the connection to the system logger, if it is open.
  81. * @access public
  82. */
  83. function close()
  84. {
  85. if ($this->_opened) {
  86. closelog();
  87. $this->_opened = false;
  88. }
  89. return ($this->_opened === false);
  90. }
  91. /**
  92. * Sends $message to the currently open syslog connection. Calls
  93. * open() if necessary. Also passes the message along to any Log_observer
  94. * instances that are observing this Log.
  95. *
  96. * @param mixed $message String or object containing the message to log.
  97. * @param int $priority (optional) The priority of the message. Valid
  98. * values are: PEAR_LOG_EMERG, PEAR_LOG_ALERT,
  99. * PEAR_LOG_CRIT, PEAR_LOG_ERR, PEAR_LOG_WARNING,
  100. * PEAR_LOG_NOTICE, PEAR_LOG_INFO, and PEAR_LOG_DEBUG.
  101. * @return boolean True on success or false on failure.
  102. * @access public
  103. */
  104. function log($message, $priority = null)
  105. {
  106. /* If a priority hasn't been specified, use the default value. */
  107. if ($priority === null) {
  108. $priority = $this->_priority;
  109. }
  110. /* Abort early if the priority is above the maximum logging level. */
  111. if (!$this->_isMasked($priority)) {
  112. return false;
  113. }
  114. /* If the connection isn't open and can't be opened, return failure. */
  115. if (!$this->_opened && !$this->open()) {
  116. return false;
  117. }
  118. /* Extract the string representation of the message. */
  119. $message = $this->_extractMessage($message);
  120. if (!syslog($this->_toSyslog($priority), $message)) {
  121. return false;
  122. }
  123. $this->_announce(array('priority' => $priority, 'message' => $message));
  124. return true;
  125. }
  126. /**
  127. * Converts a PEAR_LOG_* constant into a syslog LOG_* constant.
  128. *
  129. * This function exists because, under Windows, not all of the LOG_*
  130. * constants have unique values. Instead, the PEAR_LOG_* were introduced
  131. * for global use, with the conversion to the LOG_* constants kept local to
  132. * to the syslog driver.
  133. *
  134. * @param int $priority PEAR_LOG_* value to convert to LOG_* value.
  135. *
  136. * @return The LOG_* representation of $priority.
  137. *
  138. * @access private
  139. */
  140. function _toSyslog($priority)
  141. {
  142. static $priorities = array(
  143. PEAR_LOG_EMERG => LOG_EMERG,
  144. PEAR_LOG_ALERT => LOG_ALERT,
  145. PEAR_LOG_CRIT => LOG_CRIT,
  146. PEAR_LOG_ERR => LOG_ERR,
  147. PEAR_LOG_WARNING => LOG_WARNING,
  148. PEAR_LOG_NOTICE => LOG_NOTICE,
  149. PEAR_LOG_INFO => LOG_INFO,
  150. PEAR_LOG_DEBUG => LOG_DEBUG
  151. );
  152. /* If we're passed an unknown priority, default to LOG_INFO. */
  153. if (!is_int($priority) || !in_array($priority, $priorities)) {
  154. return LOG_INFO;
  155. }
  156. return $priorities[$priority];
  157. }
  158. }