PageRenderTime 31ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/php/Log/console.php

https://bitbucket.org/adarshj/convenient_website
PHP | 190 lines | 71 code | 23 blank | 96 comment | 11 complexity | 68dd4ca5107df00d9901c1eef1112803 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. /**
  3. * $Header: /repository/pear/Log/Log/console.php,v 1.19 2004/01/19 08:02:40 jon Exp $
  4. *
  5. * @version $Revision: 1.19 $
  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->flush();
  106. }
  107. /**
  108. * Flushes all pending ("buffered") data to the output stream.
  109. *
  110. * @access public
  111. * @since Log 1.8.2
  112. */
  113. function flush()
  114. {
  115. /*
  116. * If output buffering is enabled, dump the contents of the buffer to
  117. * the output stream.
  118. */
  119. if ($this->_buffering && (strlen($this->_buffer) > 0)) {
  120. fwrite($this->_stream, $this->_buffer);
  121. $this->_buffer = '';
  122. }
  123. return fflush($this->_stream);
  124. }
  125. /**
  126. * Writes $message to the text console. Also, passes the message
  127. * along to any Log_observer instances that are observing this Log.
  128. *
  129. * @param mixed $message String or object containing the message to log.
  130. * @param string $priority The priority of the message. Valid
  131. * values are: PEAR_LOG_EMERG, PEAR_LOG_ALERT,
  132. * PEAR_LOG_CRIT, PEAR_LOG_ERR, PEAR_LOG_WARNING,
  133. * PEAR_LOG_NOTICE, PEAR_LOG_INFO, and PEAR_LOG_DEBUG.
  134. * @return boolean True on success or false on failure.
  135. * @access public
  136. */
  137. function log($message, $priority = null)
  138. {
  139. /* If a priority hasn't been specified, use the default value. */
  140. if ($priority === null) {
  141. $priority = $this->_priority;
  142. }
  143. /* Abort early if the priority is above the maximum logging level. */
  144. if (!$this->_isMasked($priority)) {
  145. return false;
  146. }
  147. /* Extract the string representation of the message. */
  148. $message = $this->_extractMessage($message);
  149. /* Build the string containing the complete log line. */
  150. $line = sprintf($this->_lineFormat, strftime($this->_timeFormat),
  151. $this->_ident, $this->priorityToString($priority),
  152. $message) . "\n";
  153. /*
  154. * If buffering is enabled, append this line to the output buffer.
  155. * Otherwise, print the line to the output stream immediately.
  156. */
  157. if ($this->_buffering) {
  158. $this->_buffer .= $line;
  159. } else {
  160. fwrite($this->_stream, $line);
  161. }
  162. /* Notify observers about this log message. */
  163. $this->_announce(array('priority' => $priority, 'message' => $message));
  164. return true;
  165. }
  166. }
  167. ?>