setFilename($filename); } public function __destruct() { //Make sure no data is lost if($this->_fp) $this->end(); } public function setFilename($filename) { $this->_filename = $filename; } public function getFilename() { return $this->_filename; } public function setHaltOnError($value) { //If new state is same as old, don't do anything if($value === $this->_errorHandlersRegistered) return; if($value === true) { set_exception_handler(array($this,'exceptionHandler')); set_error_handler(array($this,'errorHandler')); $this->_errorHandlersRegistered = true; } else { restore_error_handler(); restore_exception_handler(); $this->_errorHandlersRegistered = false; } } public function start() { $this->_fp = @fopen($this->_filename,'w'); if(!$this->_fp) throw new Exception('Cannot open file '.$this->_filename.' for writing!'); ob_start(array($this,'outputHandler'),1024); } public function end() { $this->_stopBuffering(); $this->setHaltOnError(false); } private function _stopBuffering() { @ob_end_flush(); if($this->_fp) fclose($this->_fp); $this->_fp = null; } public function outputHandler($buffer) { fwrite($this->_fp,$buffer); } public function exceptionHandler($exception) { $this->_stopBuffering(); echo 'Fatal error: uncaught', $exception; } public function errorHandler($errno, $errstr, $errfile, $errline) { $this->_stopBuffering(); $errorNumber = E_USER_ERROR; switch($errno) { case E_ERROR: $errorNumber = E_USER_ERROR; break; case E_NOTICE: $errorNumber = E_USER_NOTICE; break; case E_WARNING: $errorNumber = E_USER_WARNING; break; } trigger_error("$errstr, File: $errfile line $errline",$errorNumber); } } ?>