tags:

views:

70

answers:

2

Greetings,

I am writing some code inside a framework for PHP 5.3, and I am trying to catch all errors in a way that will allow me to gracefully crash on client side and add some log entry at the same time. To be sure to also catch parse errors, I am using register_shutdown_function to specifically catch parse errors.

Here is the function that I register

static function shutdown()
{  
    if(is_null($e = error_get_last()) === FALSE)
      if($e["type"] == E_PARSE)
        self::error($e["type"], $e["message"], $e["file"], $e["line"], array(self::$url));
} 

The error method does two things :

  1. It adds an error entry to a log file using fopen in append.
  2. It execute an error display: it explicitely sets the HTTP code to 500, and display a custom format 500 error page. Some include (which I do within a wapper class, but is only an include for now) are required from there

For some reason, I can fopen my log file and append, but cannot do a simple include; it just silently dies from there.

Here is what the log outputs if I add a Log entry for each includes

static public function file($file)
{
  if(class_exists("Logs"))
    Logs::append("errors.log", $file . ":" . ((include $file) ? 1 : 0));
  else
    include $file;
}

// Inside the Logs class...
static public function append($file, $message)
{
  if(!is_scalar($message)) 
   $message = print_r($message, true);

  $fh = fopen(Config::getPath("LOGS") . "/" . $file, 'a');
 fwrite($fh, $message."\r\n");
 fclose($fh);
}

Here what the log file gives me:

/Users/mt/Documents/workspace/core/language.php:1
...
/Users/mt/Documents/workspace/index.php:1
/Users/mt/Documents/workspace/core/view.php:1
[2010-01-31 08:16:31] Parsing Error (Code 4) ~ syntax error, unexpected T_VARIABLE {/Users/mt/Documents/workspace/controllers/controller1.php:13}

After the parse error is hitted, it does run the registered function, but as soon as it hits a new include file, it dies of a silent death... is there a way to circumvent this? Why would I be able to open a file for read/write, but not for inclusion?

A: 

It would seem that it is related with either something in the config, either with some build specifics.

I ran the code initially on MacOSX, which would not run and fail as described, but it runs on a compiled version of PHP under Ubuntu.

Which is kinda fine for me, but pretty much makes me wonder why it still fails under OSX (XAMPP to be more precise).

Marc Trudel
+2  A: 

Try to use Lagger

SeniorDev