ILIAS  eassessment Revision 61809
 All Data Structures Namespaces Files Functions Variables Groups Pages
file.php
Go to the documentation of this file.
1 <?php
20 class Log_file extends Log
21 {
27  var $_filename = 'php.log';
28 
34  var $_fp = false;
35 
42  var $_append = true;
43 
49  var $_locking = false;
50 
56  var $_mode = 0644;
57 
64  var $_dirmode = 0755;
65 
71  var $_lineFormat = '%1$s %2$s [%3$s] %4$s';
72 
80  var $_timeFormat = '%b %d %H:%M:%S';
81 
87  var $_eol = "\n";
88 
98  function Log_file($name, $ident = '', $conf = array(),
99  $level = PEAR_LOG_DEBUG)
100  {
101  $this->_id = md5(microtime());
102  $this->_filename = $name;
103  $this->_ident = $ident;
104  $this->_mask = Log::UPTO($level);
105 
106  if (isset($conf['append'])) {
107  $this->_append = $conf['append'];
108  }
109 
110  if (isset($conf['locking'])) {
111  $this->_locking = $conf['locking'];
112  }
113 
114  if (!empty($conf['mode'])) {
115  if (is_string($conf['mode'])) {
116  $this->_mode = octdec($conf['mode']);
117  } else {
118  $this->_mode = $conf['mode'];
119  }
120  }
121 
122  if (!empty($conf['dirmode'])) {
123  if (is_string($conf['dirmode'])) {
124  $this->_dirmode = octdec($conf['dirmode']);
125  } else {
126  $this->_dirmode = $conf['dirmode'];
127  }
128  }
129 
130  if (!empty($conf['lineFormat'])) {
131  $this->_lineFormat = str_replace(array_keys($this->_formatMap),
132  array_values($this->_formatMap),
133  $conf['lineFormat']);
134  }
135 
136  if (!empty($conf['timeFormat'])) {
137  $this->_timeFormat = $conf['timeFormat'];
138  }
139 
140  if (!empty($conf['eol'])) {
141  $this->_eol = $conf['eol'];
142  } else {
143  $this->_eol = (strstr(PHP_OS, 'WIN')) ? "\r\n" : "\n";
144  }
145 
146  register_shutdown_function(array(&$this, '_Log_file'));
147  }
148 
152  function _Log_file()
153  {
154  if ($this->_opened) {
155  $this->close();
156  }
157  }
158 
174  function _mkpath($path, $mode = 0700)
175  {
176  /* Separate the last pathname component from the rest of the path. */
177  $head = dirname($path);
178  $tail = basename($path);
179 
180  /* Make sure we've split the path into two complete components. */
181  if (empty($tail)) {
182  $head = dirname($path);
183  $tail = basename($path);
184  }
185 
186  /* Recurse up the path if our current segment does not exist. */
187  if (!empty($head) && !empty($tail) && !is_dir($head)) {
188  $this->_mkpath($head, $mode);
189  }
190 
191  /* Create this segment of the path. */
192  return @mkdir($head, $mode);
193  }
194 
204  function open()
205  {
206  if (!$this->_opened) {
207  /* If the log file's directory doesn't exist, create it. */
208  if (!is_dir(dirname($this->_filename))) {
209  $this->_mkpath($this->_filename, $this->_dirmode);
210  }
211 
212  /* Determine whether the log file needs to be created. */
213  $creating = !file_exists($this->_filename);
214 
215  /* Obtain a handle to the log file. */
216  $this->_fp = fopen($this->_filename, ($this->_append) ? 'a' : 'w');
217 
218  /* We consider the file "opened" if we have a valid file pointer. */
219  $this->_opened = ($this->_fp !== false);
220 
221  /* Attempt to set the file's permissions if we just created it. */
222  if ($creating && $this->_opened) {
223  chmod($this->_filename, $this->_mode);
224  }
225  }
226 
227  return $this->_opened;
228  }
229 
235  function close()
236  {
237  /* If the log file is open, close it. */
238  if ($this->_opened && fclose($this->_fp)) {
239  $this->_opened = false;
240  }
241 
242  return ($this->_opened === false);
243  }
244 
251  function flush()
252  {
253  if (is_resource($this->_fp)) {
254  return fflush($this->_fp);
255  }
256 
257  return false;
258  }
259 
272  function log($message, $priority = null)
273  {
274  /* If a priority hasn't been specified, use the default value. */
275  if ($priority === null) {
276  $priority = $this->_priority;
277  }
278 
279  /* Abort early if the priority is above the maximum logging level. */
280  if (!$this->_isMasked($priority)) {
281  return false;
282  }
283 
284  /* If the log file isn't already open, open it now. */
285  if (!$this->_opened && !$this->open()) {
286  return false;
287  }
288 
289  /* Extract the string representation of the message. */
290  $message = $this->_extractMessage($message);
291 
292  /* Build the string containing the complete log line. */
293  $line = $this->_format($this->_lineFormat,
294  strftime($this->_timeFormat),
295  $priority, $message) . $this->_eol;
296 
297  /* If locking is enabled, acquire an exclusive lock on the file. */
298  if ($this->_locking) {
299  flock($this->_fp, LOCK_EX);
300  }
301 
302  /* Write the log line to the log file. */
303  $success = (fwrite($this->_fp, $line) !== false);
304 
305  /* Unlock the file now that we're finished writing to it. */
306  if ($this->_locking) {
307  flock($this->_fp, LOCK_UN);
308  }
309 
310  /* Notify observers about this log message. */
311  $this->_announce(array('priority' => $priority, 'message' => $message));
312 
313  return $success;
314  }
315 
316 }