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.
 
 
 
 
 
 

203 lines
5.6 KiB

  1. <?php
  2. /**
  3. * $Header: /home/ppcvs/paypal_php_sdk/Log/console.php,v 1.1 2006/02/19 08:22:29 dennis Exp $
  4. *
  5. * @version $Revision: 1.1 $
  6. * @package Log
  7. */
  8. /**
  9. * The Log_console class is a concrete implementation of the Log::
  10. * abstract class which writes message to the text console.
  11. *
  12. * @author Jon Parise <jon@php.net>
  13. * @since Log 1.1
  14. * @package Log
  15. *
  16. * @example console.php Using the console handler.
  17. */
  18. class Log_console extends Log
  19. {
  20. /**
  21. * Handle to the current output stream.
  22. * @var resource
  23. * @access private
  24. */
  25. var $_stream = STDOUT;
  26. /**
  27. * Should the output be buffered or displayed immediately?
  28. * @var string
  29. * @access private
  30. */
  31. var $_buffering = false;
  32. /**
  33. * String holding the buffered output.
  34. * @var string
  35. * @access private
  36. */
  37. var $_buffer = '';
  38. /**
  39. * String containing the format of a log line.
  40. * @var string
  41. * @access private
  42. */
  43. var $_lineFormat = '%1$s %2$s [%3$s] %4$s';
  44. /**
  45. * String containing the timestamp format. It will be passed directly to
  46. * strftime(). Note that the timestamp string will generated using the
  47. * current locale.
  48. * @var string
  49. * @access private
  50. */
  51. var $_timeFormat = '%b %d %H:%M:%S';
  52. /**
  53. * Hash that maps canonical format keys to position arguments for the
  54. * "line format" string.
  55. * @var array
  56. * @access private
  57. */
  58. var $_formatMap = array('%{timestamp}' => '%1$s',
  59. '%{ident}' => '%2$s',
  60. '%{priority}' => '%3$s',
  61. '%{message}' => '%4$s',
  62. '%\{' => '%%{');
  63. /**
  64. * Constructs a new Log_console object.
  65. *
  66. * @param string $name Ignored.
  67. * @param string $ident The identity string.
  68. * @param array $conf The configuration array.
  69. * @param int $level Log messages up to and including this level.
  70. * @access public
  71. */
  72. function Log_console($name, $ident = '', $conf = array(),
  73. $level = PEAR_LOG_DEBUG)
  74. {
  75. $this->_id = md5(microtime());
  76. $this->_ident = $ident;
  77. $this->_mask = Log::UPTO($level);
  78. if (!empty($conf['stream'])) {
  79. $this->_stream = $conf['stream'];
  80. }
  81. if (isset($conf['buffering'])) {
  82. $this->_buffering = $conf['buffering'];
  83. }
  84. if (!empty($conf['lineFormat'])) {
  85. $this->_lineFormat = str_replace(array_keys($this->_formatMap),
  86. array_values($this->_formatMap),
  87. $conf['lineFormat']);
  88. }
  89. if (!empty($conf['timeFormat'])) {
  90. $this->_timeFormat = $conf['timeFormat'];
  91. }
  92. /*
  93. * If output buffering has been requested, we need to register a
  94. * shutdown function that will dump the buffer upon termination.
  95. */
  96. if ($this->_buffering) {
  97. register_shutdown_function(array(&$this, '_Log_console'));
  98. }
  99. }
  100. /**
  101. * Destructor
  102. */
  103. function _Log_console()
  104. {
  105. $this->close();
  106. }
  107. /**
  108. * Closes the output stream.
  109. *
  110. * This results in a call to flush().
  111. *
  112. * @access public
  113. * @since Log 1.9.0
  114. */
  115. function close()
  116. {
  117. $this->flush();
  118. }
  119. /**
  120. * Flushes all pending ("buffered") data to the output stream.
  121. *
  122. * @access public
  123. * @since Log 1.8.2
  124. */
  125. function flush()
  126. {
  127. /*
  128. * If output buffering is enabled, dump the contents of the buffer to
  129. * the output stream.
  130. */
  131. if ($this->_buffering && (strlen($this->_buffer) > 0)) {
  132. fwrite($this->_stream, $this->_buffer);
  133. $this->_buffer = '';
  134. }
  135. return fflush($this->_stream);
  136. }
  137. /**
  138. * Writes $message to the text console. Also, passes the message
  139. * along to any Log_observer instances that are observing this Log.
  140. *
  141. * @param mixed $message String or object containing the message to log.
  142. * @param string $priority The priority of the message. Valid
  143. * values are: PEAR_LOG_EMERG, PEAR_LOG_ALERT,
  144. * PEAR_LOG_CRIT, PEAR_LOG_ERR, PEAR_LOG_WARNING,
  145. * PEAR_LOG_NOTICE, PEAR_LOG_INFO, and PEAR_LOG_DEBUG.
  146. * @return boolean True on success or false on failure.
  147. * @access public
  148. */
  149. function log($message, $priority = null)
  150. {
  151. /* If a priority hasn't been specified, use the default value. */
  152. if ($priority === null) {
  153. $priority = $this->_priority;
  154. }
  155. /* Abort early if the priority is above the maximum logging level. */
  156. if (!$this->_isMasked($priority)) {
  157. return false;
  158. }
  159. /* Extract the string representation of the message. */
  160. $message = $this->_extractMessage($message);
  161. /* Build the string containing the complete log line. */
  162. $line = sprintf($this->_lineFormat, strftime($this->_timeFormat),
  163. $this->_ident, $this->priorityToString($priority),
  164. $message) . "\n";
  165. /*
  166. * If buffering is enabled, append this line to the output buffer.
  167. * Otherwise, print the line to the output stream immediately.
  168. */
  169. if ($this->_buffering) {
  170. $this->_buffer .= $line;
  171. } else {
  172. fwrite($this->_stream, $line);
  173. }
  174. /* Notify observers about this log message. */
  175. $this->_announce(array('priority' => $priority, 'message' => $message));
  176. return true;
  177. }
  178. }