PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

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

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