PageRenderTime 36ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/script/lib/Log/display.php

https://bitbucket.org/chamilo/chamilo-dev/
PHP | 170 lines | 79 code | 17 blank | 74 comment | 9 complexity | 95c4d6bde64c92cc11ca31805be06255 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.1, LGPL-3.0, GPL-3.0, MIT
  1. <?php
  2. /**
  3. * $Header$
  4. *
  5. * @version $Revision: 255603 $
  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. * Constructs a new Log_display object.
  39. *
  40. * @param string $name Ignored.
  41. * @param string $ident The identity string.
  42. * @param array $conf The configuration array.
  43. * @param int $level Log messages up to and including this level.
  44. * @access public
  45. */
  46. function Log_display($name = '', $ident = '', $conf = array(), $level = PEAR_LOG_DEBUG)
  47. {
  48. $this->_id = md5(microtime());
  49. $this->_ident = $ident;
  50. $this->_mask = Log :: UPTO($level);
  51. /* Start by configuring the line format. */
  52. if (! empty($conf['lineFormat']))
  53. {
  54. $this->_lineFormat = str_replace(array_keys($this->_formatMap), array_values($this->_formatMap), $conf['lineFormat']);
  55. }
  56. /* We may need to prepend a string to our line format. */
  57. $prepend = null;
  58. if (isset($conf['error_prepend']))
  59. {
  60. $prepend = $conf['error_prepend'];
  61. }
  62. else
  63. {
  64. $prepend = ini_get('error_prepend_string');
  65. }
  66. if (! empty($prepend))
  67. {
  68. $this->_lineFormat = $prepend . $this->_lineFormat;
  69. }
  70. /* We may also need to append a string to our line format. */
  71. $append = null;
  72. if (isset($conf['error_append']))
  73. {
  74. $append = $conf['error_append'];
  75. }
  76. else
  77. {
  78. $append = ini_get('error_append_string');
  79. }
  80. if (! empty($append))
  81. {
  82. $this->_lineFormat .= $append;
  83. }
  84. /* Lastly, the line ending sequence is also configurable. */
  85. if (isset($conf['linebreak']))
  86. {
  87. $this->_lineFormat .= $conf['linebreak'];
  88. }
  89. else
  90. {
  91. $this->_lineFormat .= "<br />\n";
  92. }
  93. /* The user can also change the time format. */
  94. if (! empty($conf['timeFormat']))
  95. {
  96. $this->_timeFormat = $conf['timeFormat'];
  97. }
  98. }
  99. /**
  100. * Opens the display handler.
  101. *
  102. * @access public
  103. * @since Log 1.9.6
  104. */
  105. function open()
  106. {
  107. $this->_opened = true;
  108. return true;
  109. }
  110. /**
  111. * Closes the display handler.
  112. *
  113. * @access public
  114. * @since Log 1.9.6
  115. */
  116. function close()
  117. {
  118. $this->_opened = false;
  119. return true;
  120. }
  121. /**
  122. * Writes $message to the text browser. Also, passes the message
  123. * along to any Log_observer instances that are observing this Log.
  124. *
  125. * @param mixed $message String or object containing the message to log.
  126. * @param string $priority The priority of the message. Valid
  127. * values are: PEAR_LOG_EMERG, PEAR_LOG_ALERT,
  128. * PEAR_LOG_CRIT, PEAR_LOG_ERR, PEAR_LOG_WARNING,
  129. * PEAR_LOG_NOTICE, PEAR_LOG_INFO, and PEAR_LOG_DEBUG.
  130. * @return boolean True on success or false on failure.
  131. * @access public
  132. */
  133. function log($message, $priority = null)
  134. {
  135. /* If a priority hasn't been specified, use the default value. */
  136. if ($priority === null)
  137. {
  138. $priority = $this->_priority;
  139. }
  140. /* Abort early if the priority is above the maximum logging level. */
  141. if (! $this->_isMasked($priority))
  142. {
  143. return false;
  144. }
  145. /* Extract the string representation of the message. */
  146. $message = $this->_extractMessage($message);
  147. /* Build and output the complete log line. */
  148. echo $this->_format($this->_lineFormat, strftime($this->_timeFormat), $priority, nl2br(htmlspecialchars($message)));
  149. /* Notify observers about this log message. */
  150. $this->_announce(array('priority' => $priority, 'message' => $message));
  151. return true;
  152. }
  153. }