/modules/civicrm/vendor/pear/log/Log/display.php

https://github.com/nysenate/Bluebird-CRM · PHP · 181 lines · 77 code · 20 blank · 84 comment · 14 complexity · 9e23b5e9f53c30d71ed52b4fef5176b4 MD5 · raw file

  1. <?php
  2. /**
  3. * $Header$
  4. *
  5. * @version $Revision$
  6. * @package Log
  7. */
  8. /**
  9. * The Log_display class is a concrete implementation of the Log::
  10. * abstract class which writes message into browser in usual PHP maner.
  11. * This may be useful because when you use PEAR::setErrorHandling in
  12. * PEAR_ERROR_CALLBACK mode error messages are not displayed by
  13. * PHP error handler.
  14. *
  15. * @author Paul Yanchenko <pusher@inaco.ru>
  16. * @since Log 1.8.0
  17. * @package Log
  18. *
  19. * @example display.php Using the display handler.
  20. */
  21. class Log_display extends Log
  22. {
  23. /**
  24. * String containing the format of a log line.
  25. * @var string
  26. * @access private
  27. */
  28. var $_lineFormat = '<b>%3$s</b>: %4$s';
  29. /**
  30. * String containing the timestamp format. It will be passed directly to
  31. * strftime(). Note that the timestamp string will generated using the
  32. * current locale.
  33. * @var string
  34. * @access private
  35. */
  36. var $_timeFormat = '%b %d %H:%M:%S';
  37. /**
  38. * Flag indicating whether raw message text should be passed directly to
  39. * the log system. Otherwise, the text will be converted to an HTML-safe
  40. * representation.
  41. * @var boolean
  42. * @access private
  43. */
  44. var $_rawText = false;
  45. /**
  46. * Constructs a new Log_display object.
  47. *
  48. * @param string $name Ignored.
  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. public function __construct($name = '', $ident = '', $conf = array(),
  55. $level = PEAR_LOG_DEBUG)
  56. {
  57. $this->_id = md5(microtime().rand());
  58. $this->_ident = $ident;
  59. $this->_mask = Log::UPTO($level);
  60. /* Start by configuring the line format. */
  61. if (!empty($conf['lineFormat'])) {
  62. $this->_lineFormat = str_replace(array_keys($this->_formatMap),
  63. array_values($this->_formatMap),
  64. $conf['lineFormat']);
  65. }
  66. /* We may need to prepend a string to our line format. */
  67. $prepend = null;
  68. if (isset($conf['error_prepend'])) {
  69. $prepend = $conf['error_prepend'];
  70. } else {
  71. $prepend = ini_get('error_prepend_string');
  72. }
  73. if (!empty($prepend)) {
  74. $this->_lineFormat = $prepend . $this->_lineFormat;
  75. }
  76. /* We may also need to append a string to our line format. */
  77. $append = null;
  78. if (isset($conf['error_append'])) {
  79. $append = $conf['error_append'];
  80. } else {
  81. $append = ini_get('error_append_string');
  82. }
  83. if (!empty($append)) {
  84. $this->_lineFormat .= $append;
  85. }
  86. /* Lastly, the line ending sequence is also configurable. */
  87. if (isset($conf['linebreak'])) {
  88. $this->_lineFormat .= $conf['linebreak'];
  89. } else {
  90. $this->_lineFormat .= "<br />\n";
  91. }
  92. /* The user can also change the time format. */
  93. if (!empty($conf['timeFormat'])) {
  94. $this->_timeFormat = $conf['timeFormat'];
  95. }
  96. /* Message text conversion can be disabled. */
  97. if (isset($conf['rawText'])) {
  98. $this->_rawText = $conf['rawText'];
  99. }
  100. }
  101. /**
  102. * Opens the display handler.
  103. *
  104. * @access public
  105. * @since Log 1.9.6
  106. */
  107. function open()
  108. {
  109. $this->_opened = true;
  110. return true;
  111. }
  112. /**
  113. * Closes the display handler.
  114. *
  115. * @access public
  116. * @since Log 1.9.6
  117. */
  118. function close()
  119. {
  120. $this->_opened = false;
  121. return true;
  122. }
  123. /**
  124. * Writes $message to the text browser. Also, passes the message
  125. * along to any Log_observer instances that are observing this Log.
  126. *
  127. * @param mixed $message String or object containing the message to log.
  128. * @param string $priority The priority of the message. Valid
  129. * values are: PEAR_LOG_EMERG, PEAR_LOG_ALERT,
  130. * PEAR_LOG_CRIT, PEAR_LOG_ERR, PEAR_LOG_WARNING,
  131. * PEAR_LOG_NOTICE, PEAR_LOG_INFO, and PEAR_LOG_DEBUG.
  132. * @return boolean True on success or false on failure.
  133. * @access public
  134. */
  135. function log($message, $priority = null)
  136. {
  137. /* If a priority hasn't been specified, use the default value. */
  138. if ($priority === null) {
  139. $priority = $this->_priority;
  140. }
  141. /* Abort early if the priority is above the maximum logging level. */
  142. if (!$this->_isMasked($priority)) {
  143. return false;
  144. }
  145. /* Extract the string representation of the message. */
  146. $message = $this->_extractMessage($message);
  147. /* Convert the message to an HTML-friendly represention unless raw
  148. * text has been requested. */
  149. if ($this->_rawText === false) {
  150. $message = nl2br(htmlspecialchars($message));
  151. }
  152. /* Build and output the complete log line. */
  153. echo $this->_format($this->_lineFormat,
  154. strftime($this->_timeFormat),
  155. $priority,
  156. $message);
  157. /* Notify observers about this log message. */
  158. $this->_announce(array('priority' => $priority, 'message' => $message));
  159. return true;
  160. }
  161. }