win.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php
  2. /**
  3. * $Header: /repository/pear/Log/Log/win.php,v 1.15 2004/01/19 08:02:40 jon Exp $
  4. *
  5. * @version $Revision: 1.15 $
  6. * @package Log
  7. */
  8. /**
  9. * The Log_win class is a concrete implementation of the Log abstract
  10. * class that logs messages to a separate browser window.
  11. *
  12. * The concept for this log handler is based on part by Craig Davis' article
  13. * entitled "JavaScript Power PHP Debugging:
  14. *
  15. * http://www.zend.com/zend/tut/tutorial-DebugLib.php
  16. *
  17. * @author Jon Parise <jon@php.net>
  18. * @since Log 1.7.0
  19. * @package Log
  20. *
  21. * @example win.php Using the window handler.
  22. */
  23. class Log_win extends Log
  24. {
  25. /**
  26. * The name of the output window.
  27. * @var string
  28. * @access private
  29. */
  30. var $_name = 'LogWindow';
  31. /**
  32. * The title of the output window.
  33. * @var string
  34. * @access private
  35. */
  36. var $_title = 'Log Output Window';
  37. /**
  38. * Mapping of log priorities to colors.
  39. * @var array
  40. * @access private
  41. */
  42. var $_colors = array(
  43. PEAR_LOG_EMERG => 'red',
  44. PEAR_LOG_ALERT => 'orange',
  45. PEAR_LOG_CRIT => 'yellow',
  46. PEAR_LOG_ERR => 'green',
  47. PEAR_LOG_WARNING => 'blue',
  48. PEAR_LOG_NOTICE => 'indigo',
  49. PEAR_LOG_INFO => 'violet',
  50. PEAR_LOG_DEBUG => 'black'
  51. );
  52. /**
  53. * String buffer that holds line that are pending output.
  54. * @var array
  55. * @access private
  56. */
  57. var $_buffer = array();
  58. /**
  59. * Constructs a new Log_win object.
  60. *
  61. * @param string $name Ignored.
  62. * @param string $ident The identity string.
  63. * @param array $conf The configuration array.
  64. * @param int $level Log messages up to and including this level.
  65. * @access public
  66. */
  67. function Log_win($name, $ident = '', $conf = array(),
  68. $level = PEAR_LOG_DEBUG)
  69. {
  70. $this->_id = md5(microtime());
  71. $this->_name = $name;
  72. $this->_ident = $ident;
  73. $this->_mask = Log::UPTO($level);
  74. if (isset($conf['title'])) {
  75. $this->_title = $conf['title'];
  76. }
  77. if (isset($conf['colors']) && is_array($conf['colors'])) {
  78. $this->_colors = $conf['colors'];
  79. }
  80. register_shutdown_function(array(&$this, '_Log_win'));
  81. }
  82. /**
  83. * Destructor
  84. */
  85. function _Log_win()
  86. {
  87. if ($this->_opened || (count($this->_buffer) > 0)) {
  88. $this->close();
  89. }
  90. }
  91. /**
  92. * The first time open() is called, it will open a new browser window and
  93. * prepare it for output.
  94. *
  95. * This is implicitly called by log(), if necessary.
  96. *
  97. * @access public
  98. */
  99. function open()
  100. {
  101. if (!$this->_opened) {
  102. ?>
  103. <script language="JavaScript">
  104. win = window.open('', '<?php echo $this->_name; ?>', 'toolbar=no,scrollbars,width=600,height=400');
  105. win.document.writeln('<html>');
  106. win.document.writeln('<head>');
  107. win.document.writeln('<title><?php echo $this->_title; ?></title>');
  108. win.document.writeln('<style type="text/css">');
  109. win.document.writeln('body { font-family: monospace; font-size: 8pt; }');
  110. win.document.writeln('td,th { font-size: 8pt; }');
  111. win.document.writeln('td,th { border-bottom: #999999 solid 1px; }');
  112. win.document.writeln('td,th { border-right: #999999 solid 1px; }');
  113. win.document.writeln('</style>');
  114. win.document.writeln('</head>');
  115. win.document.writeln('<body>');
  116. win.document.writeln('<table border="0" cellpadding="2" cellspacing="0">');
  117. win.document.writeln('<tr><th>Time</th>');
  118. <?php if (!empty($this->_ident)): ?>
  119. win.document.writeln('<th>Ident</th>');
  120. <?php endif; ?>
  121. win.document.writeln('<th>Priority</th><th width="100%">Message</th></tr>');
  122. </script>
  123. <?php
  124. $this->_opened = true;
  125. }
  126. return $this->_opened;
  127. }
  128. /**
  129. * Closes the output stream if it is open. If there are still pending
  130. * lines in the output buffer, the output window will be opened so that
  131. * the buffer can be drained.
  132. *
  133. * @access public
  134. */
  135. function close()
  136. {
  137. /*
  138. * If there are still lines waiting to be written, open the output
  139. * window so that we can drain the buffer.
  140. */
  141. if (!$this->_opened && (count($this->_buffer) > 0)) {
  142. $this->open();
  143. }
  144. if ($this->_opened) {
  145. $this->_writeln('</table>');
  146. $this->_writeln('</body></html>');
  147. $this->_opened = false;
  148. }
  149. return ($this->_opened === false);
  150. }
  151. /**
  152. * Writes a single line of text to the output window.
  153. *
  154. * @param string $line The line of text to write.
  155. *
  156. * @access private
  157. */
  158. function _writeln($line)
  159. {
  160. /* Add this line to our output buffer. */
  161. $this->_buffer[] = $line;
  162. /* Buffer the output until this page's headers have been sent. */
  163. if (!headers_sent()) {
  164. return;
  165. }
  166. /* If we haven't already opened the output window, do so now. */
  167. if (!$this->_opened && !$this->open()) {
  168. return false;
  169. }
  170. /* Drain the buffer to the output window. */
  171. foreach ($this->_buffer as $line) {
  172. echo "<script language='JavaScript'>\n";
  173. echo "win.document.writeln('" . addslashes($line) . "');\n";
  174. echo "self.focus();\n";
  175. echo "</script>\n";
  176. }
  177. /* Now that the buffer has been drained, clear it. */
  178. $this->_buffer = array();
  179. }
  180. /**
  181. * Logs $message to the output window. The message is also passed along
  182. * to any Log_observer instances that are observing this Log.
  183. *
  184. * @param mixed $message String or object containing the message to log.
  185. * @param string $priority The priority of the message. Valid
  186. * values are: PEAR_LOG_EMERG, PEAR_LOG_ALERT,
  187. * PEAR_LOG_CRIT, PEAR_LOG_ERR, PEAR_LOG_WARNING,
  188. * PEAR_LOG_NOTICE, PEAR_LOG_INFO, and PEAR_LOG_DEBUG.
  189. * @return boolean True on success or false on failure.
  190. * @access public
  191. */
  192. function log($message, $priority = null)
  193. {
  194. /* If a priority hasn't been specified, use the default value. */
  195. if ($priority === null) {
  196. $priority = $this->_priority;
  197. }
  198. /* Abort early if the priority is above the maximum logging level. */
  199. if (!$this->_isMasked($priority)) {
  200. return false;
  201. }
  202. /* Extract the string representation of the message. */
  203. $message = $this->_extractMessage($message);
  204. list($usec, $sec) = explode(' ', microtime());
  205. /* Build the output line that contains the log entry row. */
  206. $line = '<tr align="left" valign="top">';
  207. $line .= sprintf('<td>%s.%s</td>',
  208. strftime('%T', $sec), substr($usec, 2, 2));
  209. if (!empty($this->_ident)) {
  210. $line .= '<td>' . $this->_ident . '</td>';
  211. }
  212. $line .= '<td>' . ucfirst($this->priorityToString($priority)) . '</td>';
  213. $line .= sprintf('<td style="color: %s">%s</td>',
  214. $this->_colors[$priority],
  215. preg_replace('/\r\n|\n|\r/', '<br />', $message));
  216. $line .= '</tr>';
  217. $this->_writeln($line);
  218. $this->_announce(array('priority' => $priority, 'message' => $message));
  219. return true;
  220. }
  221. }
  222. ?>