ILIAS  Release_5_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
console.php
Go to the documentation of this file.
1 <?php
19 class Log_console extends Log
20 {
26  var $_stream = STDOUT;
27 
33  var $_buffering = false;
34 
40  var $_buffer = '';
41 
47  var $_lineFormat = '%1$s %2$s [%3$s] %4$s';
48 
56  var $_timeFormat = '%b %d %H:%M:%S';
57 
67  function Log_console($name, $ident = '', $conf = array(),
68  $level = PEAR_LOG_DEBUG)
69  {
70  $this->_id = md5(microtime());
71  $this->_ident = $ident;
72  $this->_mask = Log::UPTO($level);
73 
74  if (!empty($conf['stream'])) {
75  $this->_stream = $conf['stream'];
76  }
77 
78  if (isset($conf['buffering'])) {
79  $this->_buffering = $conf['buffering'];
80  }
81 
82  if (!empty($conf['lineFormat'])) {
83  $this->_lineFormat = str_replace(array_keys($this->_formatMap),
84  array_values($this->_formatMap),
85  $conf['lineFormat']);
86  }
87 
88  if (!empty($conf['timeFormat'])) {
89  $this->_timeFormat = $conf['timeFormat'];
90  }
91 
92  /*
93  * If output buffering has been requested, we need to register a
94  * shutdown function that will dump the buffer upon termination.
95  */
96  if ($this->_buffering) {
97  register_shutdown_function(array(&$this, '_Log_console'));
98  }
99  }
100 
104  function _Log_console()
105  {
106  $this->close();
107  }
108 
115  function open()
116  {
117  $this->_opened = true;
118  return true;
119  }
120 
129  function close()
130  {
131  $this->flush();
132  $this->_opened = false;
133  return true;
134  }
135 
142  function flush()
143  {
144  /*
145  * If output buffering is enabled, dump the contents of the buffer to
146  * the output stream.
147  */
148  if ($this->_buffering && (strlen($this->_buffer) > 0)) {
149  fwrite($this->_stream, $this->_buffer);
150  $this->_buffer = '';
151  }
152 
153  if (is_resource($this->_stream)) {
154  return fflush($this->_stream);
155  }
156 
157  return false;
158  }
159 
172  function log($message, $priority = null)
173  {
174  /* If a priority hasn't been specified, use the default value. */
175  if ($priority === null) {
176  $priority = $this->_priority;
177  }
178 
179  /* Abort early if the priority is above the maximum logging level. */
180  if (!$this->_isMasked($priority)) {
181  return false;
182  }
183 
184  /* Extract the string representation of the message. */
185  $message = $this->_extractMessage($message);
186 
187  /* Build the string containing the complete log line. */
188  $line = $this->_format($this->_lineFormat,
189  strftime($this->_timeFormat),
190  $priority, $message) . "\n";
191 
192  /*
193  * If buffering is enabled, append this line to the output buffer.
194  * Otherwise, print the line to the output stream immediately.
195  */
196  if ($this->_buffering) {
197  $this->_buffer .= $line;
198  } else {
199  fwrite($this->_stream, $line);
200  }
201 
202  /* Notify observers about this log message. */
203  $this->_announce(array('priority' => $priority, 'message' => $message));
204 
205  return true;
206  }
207 
208 }