/includes/Log-1.12.2/Log/firebug.php

https://github.com/sseshachala/Open-Web-Analytics · PHP · 214 lines · 95 code · 23 blank · 96 comment · 11 complexity · 45330534133be7f69fb0b5dbe8d06766 MD5 · raw file

  1. <?php
  2. /**
  3. * $Header$
  4. *
  5. * @version $Revision: 250923 $
  6. * @package Log
  7. */
  8. /**
  9. * The Log_firebug class is a concrete implementation of the Log::
  10. * abstract class which writes message into Firebug console.
  11. *
  12. * http://www.getfirebug.com/
  13. *
  14. * @author Mika Tuupola <tuupola@appelsiini.net>
  15. * @since Log 1.9.11
  16. * @package Log
  17. *
  18. * @example firebug.php Using the firebug handler.
  19. */
  20. class Log_firebug extends Log
  21. {
  22. /**
  23. * Should the output be buffered or displayed immediately?
  24. * @var string
  25. * @access private
  26. */
  27. var $_buffering = false;
  28. /**
  29. * String holding the buffered output.
  30. * @var string
  31. * @access private
  32. */
  33. var $_buffer = array();
  34. /**
  35. * String containing the format of a log line.
  36. * @var string
  37. * @access private
  38. */
  39. var $_lineFormat = '%2$s [%3$s] %4$s';
  40. /**
  41. * String containing the timestamp format. It will be passed directly to
  42. * strftime(). Note that the timestamp string will generated using the
  43. * current locale.
  44. *
  45. * Note! Default lineFormat of this driver does not display time.
  46. *
  47. * @var string
  48. * @access private
  49. */
  50. var $_timeFormat = '%b %d %H:%M:%S';
  51. /**
  52. * Mapping of log priorities to Firebug methods.
  53. * @var array
  54. * @access private
  55. */
  56. var $_methods = array(
  57. PEAR_LOG_EMERG => 'error',
  58. PEAR_LOG_ALERT => 'error',
  59. PEAR_LOG_CRIT => 'error',
  60. PEAR_LOG_ERR => 'error',
  61. PEAR_LOG_WARNING => 'warn',
  62. PEAR_LOG_NOTICE => 'info',
  63. PEAR_LOG_INFO => 'info',
  64. PEAR_LOG_DEBUG => 'debug'
  65. );
  66. /**
  67. * Constructs a new Log_firebug object.
  68. *
  69. * @param string $name Ignored.
  70. * @param string $ident The identity string.
  71. * @param array $conf The configuration array.
  72. * @param int $level Log messages up to and including this level.
  73. * @access public
  74. */
  75. function Log_firebug($name = '', $ident = 'PHP', $conf = array(),
  76. $level = PEAR_LOG_DEBUG)
  77. {
  78. $this->_id = md5(microtime());
  79. $this->_ident = $ident;
  80. $this->_mask = Log::UPTO($level);
  81. if (isset($conf['buffering'])) {
  82. $this->_buffering = $conf['buffering'];
  83. }
  84. if ($this->_buffering) {
  85. register_shutdown_function(array(&$this, '_Log_firebug'));
  86. }
  87. if (!empty($conf['lineFormat'])) {
  88. $this->_lineFormat = str_replace(array_keys($this->_formatMap),
  89. array_values($this->_formatMap),
  90. $conf['lineFormat']);
  91. }
  92. if (!empty($conf['timeFormat'])) {
  93. $this->_timeFormat = $conf['timeFormat'];
  94. }
  95. }
  96. /**
  97. * Opens the firebug handler.
  98. *
  99. * @access public
  100. */
  101. function open()
  102. {
  103. $this->_opened = true;
  104. return true;
  105. }
  106. /**
  107. * Destructor
  108. */
  109. function _Log_firebug()
  110. {
  111. $this->close();
  112. }
  113. /**
  114. * Closes the firebug handler.
  115. *
  116. * @access public
  117. */
  118. function close()
  119. {
  120. $this->flush();
  121. $this->_opened = false;
  122. return true;
  123. }
  124. /**
  125. * Flushes all pending ("buffered") data.
  126. *
  127. * @access public
  128. */
  129. function flush() {
  130. if (count($this->_buffer)) {
  131. print '<script type="text/javascript">';
  132. print "\nif (('console' in window) && ('firebug' in console)) {\n";
  133. foreach ($this->_buffer as $line) {
  134. print " $line\n";
  135. }
  136. print "}\n";
  137. print "</script>\n";
  138. };
  139. $this->_buffer = array();
  140. }
  141. /**
  142. * Writes $message to Firebug console. Also, passes the message
  143. * along to any Log_observer instances that are observing this Log.
  144. *
  145. * @param mixed $message String or object containing the message to log.
  146. * @param string $priority The priority of the message. Valid
  147. * values are: PEAR_LOG_EMERG, PEAR_LOG_ALERT,
  148. * PEAR_LOG_CRIT, PEAR_LOG_ERR, PEAR_LOG_WARNING,
  149. * PEAR_LOG_NOTICE, PEAR_LOG_INFO, and PEAR_LOG_DEBUG.
  150. * @return boolean True on success or false on failure.
  151. * @access public
  152. */
  153. function log($message, $priority = null)
  154. {
  155. /* If a priority hasn't been specified, use the default value. */
  156. if ($priority === null) {
  157. $priority = $this->_priority;
  158. }
  159. /* Abort early if the priority is above the maximum logging level. */
  160. if (!$this->_isMasked($priority)) {
  161. return false;
  162. }
  163. /* Extract the string representation of the message. */
  164. $message = $this->_extractMessage($message);
  165. $method = $this->_methods[$priority];
  166. /* normalize line breaks */
  167. $message = str_replace("\r\n", "\n", $message);
  168. /* escape line breaks */
  169. $message = str_replace("\n", "\\n\\\n", $message);
  170. /* escape quotes */
  171. $message = str_replace('"', '\\"', $message);
  172. /* Build the string containing the complete log line. */
  173. $line = $this->_format($this->_lineFormat,
  174. strftime($this->_timeFormat),
  175. $priority,
  176. $message);
  177. if ($this->_buffering) {
  178. $this->_buffer[] = sprintf('console.%s("%s");', $method, $line);
  179. } else {
  180. print '<script type="text/javascript">';
  181. print "\nif (('console' in window) && ('firebug' in console)) {\n";
  182. /* Build and output the complete log line. */
  183. printf(' console.%s("%s");', $method, $line);
  184. print "\n}\n";
  185. print "</script>\n";
  186. }
  187. /* Notify observers about this log message. */
  188. $this->_announce(array('priority' => $priority, 'message' => $message));
  189. return true;
  190. }
  191. }