ILIAS  Release_4_2_x_branch Revision 61807
 All Data Structures Namespaces Files Functions Variables Groups Pages
win.php
Go to the documentation of this file.
1 <?php
24 class Log_win extends Log
25 {
31  var $_name = 'LogWindow';
32 
38  var $_title = 'Log Output Window';
39 
45  var $_styles = array(
46  PEAR_LOG_EMERG => 'color: red;',
47  PEAR_LOG_ALERT => 'color: orange;',
48  PEAR_LOG_CRIT => 'color: yellow;',
49  PEAR_LOG_ERR => 'color: green;',
50  PEAR_LOG_WARNING => 'color: blue;',
51  PEAR_LOG_NOTICE => 'color: indigo;',
52  PEAR_LOG_INFO => 'color: violet;',
53  PEAR_LOG_DEBUG => 'color: black;'
54  );
55 
61  var $_buffer = array();
62 
72  function Log_win($name, $ident = '', $conf = array(),
73  $level = PEAR_LOG_DEBUG)
74  {
75  $this->_id = md5(microtime());
76  $this->_name = str_replace(' ', '_', $name);
77  $this->_ident = $ident;
78  $this->_mask = Log::UPTO($level);
79 
80  if (isset($conf['title'])) {
81  $this->_title = $conf['title'];
82  }
83  if (isset($conf['styles']) && is_array($conf['styles'])) {
84  $this->_styles = $conf['styles'];
85  }
86  if (isset($conf['colors']) && is_array($conf['colors'])) {
87  foreach ($conf['colors'] as $level => $color) {
88  $this->_styles[$level] .= "color: $color;";
89  }
90  }
91 
92  register_shutdown_function(array(&$this, '_Log_win'));
93  }
94 
98  function _Log_win()
99  {
100  if ($this->_opened || (count($this->_buffer) > 0)) {
101  $this->close();
102  }
103  }
104 
113  function open()
114  {
115  if (!$this->_opened) {
116  $win = $this->_name;
117  $styles = $this->_styles;
118 
119  if (!empty($this->_ident)) {
120  $identHeader = "$win.document.writeln('<th>Ident</th>')";
121  } else {
122  $identHeader = '';
123  }
124 
125  echo <<< EOT
126 <script language="JavaScript">
127 $win = window.open('', '{$this->_name}', 'toolbar=no,scrollbars,width=600,height=400');
128 $win.document.writeln('<html>');
129 $win.document.writeln('<head>');
130 $win.document.writeln('<title>{$this->_title}</title>');
131 $win.document.writeln('<style type="text/css">');
132 $win.document.writeln('body { font-family: monospace; font-size: 8pt; }');
133 $win.document.writeln('td,th { font-size: 8pt; }');
134 $win.document.writeln('td,th { border-bottom: #999999 solid 1px; }');
135 $win.document.writeln('td,th { border-right: #999999 solid 1px; }');
136 $win.document.writeln('tr { text-align: left; vertical-align: top; }');
137 $win.document.writeln('td.l0 { $styles[0] }');
138 $win.document.writeln('td.l1 { $styles[1] }');
139 $win.document.writeln('td.l2 { $styles[2] }');
140 $win.document.writeln('td.l3 { $styles[3] }');
141 $win.document.writeln('td.l4 { $styles[4] }');
142 $win.document.writeln('td.l5 { $styles[5] }');
143 $win.document.writeln('td.l6 { $styles[6] }');
144 $win.document.writeln('td.l7 { $styles[7] }');
145 $win.document.writeln('</style>');
146 $win.document.writeln('<script type="text/javascript">');
147 $win.document.writeln('function scroll() {');
148 $win.document.writeln(' body = document.getElementById("{$this->_name}");');
149 $win.document.writeln(' body.scrollTop = body.scrollHeight;');
150 $win.document.writeln('}');
151 $win.document.writeln('<\/script>');
152 $win.document.writeln('</head>');
153 $win.document.writeln('<body id="{$this->_name}" onclick="scroll()">');
154 $win.document.writeln('<table border="0" cellpadding="2" cellspacing="0">');
155 $win.document.writeln('<tr><th>Time</th>');
156 $identHeader
157 $win.document.writeln('<th>Priority</th><th width="100%">Message</th></tr>');
158 </script>
159 EOT;
160  $this->_opened = true;
161  }
162 
163  return $this->_opened;
164  }
165 
173  function close()
174  {
175  /*
176  * If there are still lines waiting to be written, open the output
177  * window so that we can drain the buffer.
178  */
179  if (!$this->_opened && (count($this->_buffer) > 0)) {
180  $this->open();
181  }
182 
183  if ($this->_opened) {
184  $this->_writeln('</table>');
185  $this->_writeln('</body></html>');
186  $this->_drainBuffer();
187  $this->_opened = false;
188  }
189 
190  return ($this->_opened === false);
191  }
192 
198  function _drainBuffer()
199  {
200  $win = $this->_name;
201  foreach ($this->_buffer as $line) {
202  echo "<script language='JavaScript'>\n";
203  echo "$win.document.writeln('" . addslashes($line) . "');\n";
204  echo "self.focus();\n";
205  echo "</script>\n";
206  }
207 
208  /* Now that the buffer has been drained, clear it. */
209  $this->_buffer = array();
210  }
211 
219  function _writeln($line)
220  {
221  /* Add this line to our output buffer. */
222  $this->_buffer[] = $line;
223 
224  /* Buffer the output until this page's headers have been sent. */
225  if (!headers_sent()) {
226  return;
227  }
228 
229  /* If we haven't already opened the output window, do so now. */
230  if (!$this->_opened && !$this->open()) {
231  return;
232  }
233 
234  /* Drain the buffer to the output window. */
235  $this->_drainBuffer();
236  }
237 
250  function log($message, $priority = null)
251  {
252  /* If a priority hasn't been specified, use the default value. */
253  if ($priority === null) {
254  $priority = $this->_priority;
255  }
256 
257  /* Abort early if the priority is above the maximum logging level. */
258  if (!$this->_isMasked($priority)) {
259  return false;
260  }
261 
262  /* Extract the string representation of the message. */
263  $message = $this->_extractMessage($message);
264  $message = preg_replace('/\r\n|\n|\r/', '<br />', $message);
265 
266  list($usec, $sec) = explode(' ', microtime());
267 
268  /* Build the output line that contains the log entry row. */
269  $line = '<tr>';
270  $line .= sprintf('<td>%s.%s</td>',
271  strftime('%H:%M:%S', $sec), substr($usec, 2, 2));
272  if (!empty($this->_ident)) {
273  $line .= '<td>' . $this->_ident . '</td>';
274  }
275  $line .= '<td>' . ucfirst($this->priorityToString($priority)) . '</td>';
276  $line .= sprintf('<td class="l%d">%s</td>', $priority, $message);
277  $line .= '</tr>';
278 
279  $this->_writeln($line);
280 
281  $this->_announce(array('priority' => $priority, 'message' => $message));
282 
283  return true;
284  }
285 
286 }