/includes/Log-1.12.2/Log/console.php

https://github.com/sseshachala/Open-Web-Analytics · PHP · 208 lines · 79 code · 25 blank · 104 comment · 12 complexity · 932d7b9d76e8a3323689e6ef1a70ca6c MD5 · raw file

  1. <?php
  2. /**
  3. * $Header$
  4. *
  5. * @version $Revision: 224513 $
  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. * Constructs a new Log_console object.
  54. *
  55. * @param string $name Ignored.
  56. * @param string $ident The identity string.
  57. * @param array $conf The configuration array.
  58. * @param int $level Log messages up to and including this level.
  59. * @access public
  60. */
  61. function Log_console($name, $ident = '', $conf = array(),
  62. $level = PEAR_LOG_DEBUG)
  63. {
  64. $this->_id = md5(microtime());
  65. $this->_ident = $ident;
  66. $this->_mask = Log::UPTO($level);
  67. if (!empty($conf['stream'])) {
  68. $this->_stream = $conf['stream'];
  69. }
  70. if (isset($conf['buffering'])) {
  71. $this->_buffering = $conf['buffering'];
  72. }
  73. if (!empty($conf['lineFormat'])) {
  74. $this->_lineFormat = str_replace(array_keys($this->_formatMap),
  75. array_values($this->_formatMap),
  76. $conf['lineFormat']);
  77. }
  78. if (!empty($conf['timeFormat'])) {
  79. $this->_timeFormat = $conf['timeFormat'];
  80. }
  81. /*
  82. * If output buffering has been requested, we need to register a
  83. * shutdown function that will dump the buffer upon termination.
  84. */
  85. if ($this->_buffering) {
  86. register_shutdown_function(array(&$this, '_Log_console'));
  87. }
  88. }
  89. /**
  90. * Destructor
  91. */
  92. function _Log_console()
  93. {
  94. $this->close();
  95. }
  96. /**
  97. * Open the output stream.
  98. *
  99. * @access public
  100. * @since Log 1.9.7
  101. */
  102. function open()
  103. {
  104. $this->_opened = true;
  105. return true;
  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. $this->_opened = false;
  119. return true;
  120. }
  121. /**
  122. * Flushes all pending ("buffered") data to the output stream.
  123. *
  124. * @access public
  125. * @since Log 1.8.2
  126. */
  127. function flush()
  128. {
  129. /*
  130. * If output buffering is enabled, dump the contents of the buffer to
  131. * the output stream.
  132. */
  133. if ($this->_buffering && (strlen($this->_buffer) > 0)) {
  134. fwrite($this->_stream, $this->_buffer);
  135. $this->_buffer = '';
  136. }
  137. if (is_resource($this->_stream)) {
  138. return fflush($this->_stream);
  139. }
  140. return false;
  141. }
  142. /**
  143. * Writes $message to the text console. Also, passes the message
  144. * along to any Log_observer instances that are observing this Log.
  145. *
  146. * @param mixed $message String or object containing the message to log.
  147. * @param string $priority The priority of the message. Valid
  148. * values are: PEAR_LOG_EMERG, PEAR_LOG_ALERT,
  149. * PEAR_LOG_CRIT, PEAR_LOG_ERR, PEAR_LOG_WARNING,
  150. * PEAR_LOG_NOTICE, PEAR_LOG_INFO, and PEAR_LOG_DEBUG.
  151. * @return boolean True on success or false on failure.
  152. * @access public
  153. */
  154. function log($message, $priority = null)
  155. {
  156. /* If a priority hasn't been specified, use the default value. */
  157. if ($priority === null) {
  158. $priority = $this->_priority;
  159. }
  160. /* Abort early if the priority is above the maximum logging level. */
  161. if (!$this->_isMasked($priority)) {
  162. return false;
  163. }
  164. /* Extract the string representation of the message. */
  165. $message = $this->_extractMessage($message);
  166. /* Build the string containing the complete log line. */
  167. $line = $this->_format($this->_lineFormat,
  168. strftime($this->_timeFormat),
  169. $priority, $message) . "\n";
  170. /*
  171. * If buffering is enabled, append this line to the output buffer.
  172. * Otherwise, print the line to the output stream immediately.
  173. */
  174. if ($this->_buffering) {
  175. $this->_buffer .= $line;
  176. } else {
  177. fwrite($this->_stream, $line);
  178. }
  179. /* Notify observers about this log message. */
  180. $this->_announce(array('priority' => $priority, 'message' => $message));
  181. return true;
  182. }
  183. }