ILIAS  Release_4_1_x_branch Revision 61804
 All Data Structures Namespaces Files Functions Variables Groups Pages
firebug.php
Go to the documentation of this file.
1 <?php
21 class Log_firebug extends Log
22 {
28  var $_buffering = false;
29 
35  var $_buffer = array();
36 
42  var $_lineFormat = '%2$s [%3$s] %4$s';
43 
54  var $_timeFormat = '%b %d %H:%M:%S';
55 
61  var $_methods = array(
62  PEAR_LOG_EMERG => 'error',
63  PEAR_LOG_ALERT => 'error',
64  PEAR_LOG_CRIT => 'error',
65  PEAR_LOG_ERR => 'error',
66  PEAR_LOG_WARNING => 'warn',
67  PEAR_LOG_NOTICE => 'info',
68  PEAR_LOG_INFO => 'info',
69  PEAR_LOG_DEBUG => 'debug'
70  );
71 
81  function Log_firebug($name = '', $ident = 'PHP', $conf = array(),
82  $level = PEAR_LOG_DEBUG)
83  {
84  $this->_id = md5(microtime());
85  $this->_ident = $ident;
86  $this->_mask = Log::UPTO($level);
87  if (isset($conf['buffering'])) {
88  $this->_buffering = $conf['buffering'];
89  }
90 
91  if ($this->_buffering) {
92  register_shutdown_function(array(&$this, '_Log_firebug'));
93  }
94 
95  if (!empty($conf['lineFormat'])) {
96  $this->_lineFormat = str_replace(array_keys($this->_formatMap),
97  array_values($this->_formatMap),
98  $conf['lineFormat']);
99  }
100 
101  if (!empty($conf['timeFormat'])) {
102  $this->_timeFormat = $conf['timeFormat'];
103  }
104  }
105 
111  function open()
112  {
113  $this->_opened = true;
114  return true;
115  }
116 
120  function _Log_firebug()
121  {
122  $this->close();
123  }
124 
130  function close()
131  {
132  $this->flush();
133  $this->_opened = false;
134  return true;
135  }
136 
142  function flush() {
143  if (count($this->_buffer)) {
144  print '<script type="text/javascript">';
145  print "\nif (('console' in window) && ('firebug' in console)) {\n";
146  foreach ($this->_buffer as $line) {
147  print " $line\n";
148  }
149  print "}\n";
150  print "</script>\n";
151  };
152  $this->_buffer = array();
153  }
154 
167  function log($message, $priority = null)
168  {
169  /* If a priority hasn't been specified, use the default value. */
170  if ($priority === null) {
171  $priority = $this->_priority;
172  }
173 
174  /* Abort early if the priority is above the maximum logging level. */
175  if (!$this->_isMasked($priority)) {
176  return false;
177  }
178 
179  /* Extract the string representation of the message. */
180  $message = $this->_extractMessage($message);
181  $method = $this->_methods[$priority];
182 
183  /* normalize line breaks */
184  $message = str_replace("\r\n", "\n", $message);
185 
186  /* escape line breaks */
187  $message = str_replace("\n", "\\n\\\n", $message);
188 
189  /* escape quotes */
190  $message = str_replace('"', '\\"', $message);
191 
192  /* Build the string containing the complete log line. */
193  $line = $this->_format($this->_lineFormat,
194  strftime($this->_timeFormat),
195  $priority,
196  $message);
197 
198  if ($this->_buffering) {
199  $this->_buffer[] = sprintf('console.%s("%s");', $method, $line);
200  } else {
201  print '<script type="text/javascript">';
202  print "\nif (('console' in window) && ('firebug' in console)) {\n";
203  /* Build and output the complete log line. */
204  printf(' console.%s("%s");', $method, $line);
205  print "\n}\n";
206  print "</script>\n";
207  }
208  /* Notify observers about this log message. */
209  $this->_announce(array('priority' => $priority, 'message' => $message));
210 
211  return true;
212  }
213 
214 }