Automatically exported from code.google.com/p/planningalerts
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

win.php 7.6 KiB

17 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php
  2. /**
  3. * $Header: /home/ppcvs/paypal_php_sdk/Log/win.php,v 1.1 2006/02/19 08:22:29 dennis Exp $
  4. *
  5. * @version $Revision: 1.1 $
  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. $win = $this->_name;
  103. if (!empty($this->_ident)) {
  104. $identHeader = "$win.document.writeln('<th>Ident</th>')";
  105. } else {
  106. $identHeader = '';
  107. }
  108. echo <<< END_OF_SCRIPT
  109. <script language="JavaScript">
  110. $win = window.open('', '{$this->_name}', 'toolbar=no,scrollbars,width=600,height=400');
  111. $win.document.writeln('<html>');
  112. $win.document.writeln('<head>');
  113. $win.document.writeln('<title>{$this->_title}</title>');
  114. $win.document.writeln('<style type="text/css">');
  115. $win.document.writeln('body { font-family: monospace; font-size: 8pt; }');
  116. $win.document.writeln('td,th { font-size: 8pt; }');
  117. $win.document.writeln('td,th { border-bottom: #999999 solid 1px; }');
  118. $win.document.writeln('td,th { border-right: #999999 solid 1px; }');
  119. $win.document.writeln('</style>');
  120. $win.document.writeln('</head>');
  121. $win.document.writeln('<body>');
  122. $win.document.writeln('<table border="0" cellpadding="2" cellspacing="0">');
  123. $win.document.writeln('<tr><th>Time</th>');
  124. $identHeader
  125. $win.document.writeln('<th>Priority</th><th width="100%">Message</th></tr>');
  126. </script>
  127. END_OF_SCRIPT;
  128. $this->_opened = true;
  129. }
  130. return $this->_opened;
  131. }
  132. /**
  133. * Closes the output stream if it is open. If there are still pending
  134. * lines in the output buffer, the output window will be opened so that
  135. * the buffer can be drained.
  136. *
  137. * @access public
  138. */
  139. function close()
  140. {
  141. /*
  142. * If there are still lines waiting to be written, open the output
  143. * window so that we can drain the buffer.
  144. */
  145. if (!$this->_opened && (count($this->_buffer) > 0)) {
  146. $this->open();
  147. }
  148. if ($this->_opened) {
  149. $this->_writeln('</table>');
  150. $this->_writeln('</body></html>');
  151. $this->_opened = false;
  152. }
  153. return ($this->_opened === false);
  154. }
  155. /**
  156. * Writes a single line of text to the output window.
  157. *
  158. * @param string $line The line of text to write.
  159. *
  160. * @access private
  161. */
  162. function _writeln($line)
  163. {
  164. /* Add this line to our output buffer. */
  165. $this->_buffer[] = $line;
  166. /* Buffer the output until this page's headers have been sent. */
  167. if (!headers_sent()) {
  168. return;
  169. }
  170. /* If we haven't already opened the output window, do so now. */
  171. if (!$this->_opened && !$this->open()) {
  172. return false;
  173. }
  174. /* Drain the buffer to the output window. */
  175. $win = $this->_name;
  176. foreach ($this->_buffer as $line) {
  177. echo "<script language='JavaScript'>\n";
  178. echo "$win.document.writeln('" . addslashes($line) . "');\n";
  179. echo "self.focus();\n";
  180. echo "</script>\n";
  181. }
  182. /* Now that the buffer has been drained, clear it. */
  183. $this->_buffer = array();
  184. }
  185. /**
  186. * Logs $message to the output window. The message is also passed along
  187. * to any Log_observer instances that are observing this Log.
  188. *
  189. * @param mixed $message String or object containing the message to log.
  190. * @param string $priority The priority of the message. Valid
  191. * values are: PEAR_LOG_EMERG, PEAR_LOG_ALERT,
  192. * PEAR_LOG_CRIT, PEAR_LOG_ERR, PEAR_LOG_WARNING,
  193. * PEAR_LOG_NOTICE, PEAR_LOG_INFO, and PEAR_LOG_DEBUG.
  194. * @return boolean True on success or false on failure.
  195. * @access public
  196. */
  197. function log($message, $priority = null)
  198. {
  199. /* If a priority hasn't been specified, use the default value. */
  200. if ($priority === null) {
  201. $priority = $this->_priority;
  202. }
  203. /* Abort early if the priority is above the maximum logging level. */
  204. if (!$this->_isMasked($priority)) {
  205. return false;
  206. }
  207. /* Extract the string representation of the message. */
  208. $message = $this->_extractMessage($message);
  209. list($usec, $sec) = explode(' ', microtime());
  210. /* Build the output line that contains the log entry row. */
  211. $line = '<tr align="left" valign="top">';
  212. $line .= sprintf('<td>%s.%s</td>',
  213. strftime('%T', $sec), substr($usec, 2, 2));
  214. if (!empty($this->_ident)) {
  215. $line .= '<td>' . $this->_ident . '</td>';
  216. }
  217. $line .= '<td>' . ucfirst($this->priorityToString($priority)) . '</td>';
  218. $line .= sprintf('<td style="color: %s">%s</td>',
  219. $this->_colors[$priority],
  220. preg_replace('/\r\n|\n|\r/', '<br />', $message));
  221. $line .= '</tr>';
  222. $this->_writeln($line);
  223. $this->_announce(array('priority' => $priority, 'message' => $message));
  224. return true;
  225. }
  226. }