Hi Jack,
here is a link that can be of help
http://bugs.php.net/bug.php?id=22839

also you can use custom error handler function , catch errors and write to stderr
============

function myErrorHandler($errno, $errstr, $errfile, $errline)
{
$ERROR='';
    switch ($errno) {
    case E_USER_ERROR:
        $ERROR = "<b>My ERROR</b> [$errno] $errstr<br />\n";
        $ERROR.= "  Fatal error on line $errline in file $errfile";
        $ERROR.= ", PHP " . PHP_VERSION . " (" . PHP_OS . ")<br />\n";
        $ERROR.= "Aborting...<br />\n";

        break;

    case E_USER_WARNING:
        $ERROR= "<b>My WARNING</b> [$errno] $errstr<br />\n";
        break;

    case E_USER_NOTICE:
        $ERROR= "<b>My NOTICE</b> [$errno] $errstr<br />\n";
        break;

    default:
        $ERROR  = echo "Unknown error type: [$errno] $errstr<br />\n";
        break;

    }


        if($ERROR){
                $stderr = fopen('php://stderr', 'w');
                fwrite($stderr, $ERROR );
                fclose($stderr);
        }

    /* Don't execute PHP internal error handler */
    return true;
}


set_error_handler("myErrorHandler");


//code .....
==========================

I guess you can achieve what you need by one of those 2 concepts
Cheers.

On Feb 23, 2008, at 1:04 AM, Jack Bates wrote:

How can I implement in PHP, a script which redirects stdout to stderr,
such that echo, etc. print to stderr instead of stdout?

I can redirect stdout to stderr when invoking PHP like so:

php script-name >&2

However I want to perform this redirection within the script itself.

The solution I currently use is output buffering:

ob_start();

// Call library code

fwrite(STDERR, ob_get_contents());
ob_end_clean();

However I wonder if there's a more efficient way, so that output appears
on stderr immediately, rather than waiting for fwrite(STDERR,
ob_get_contents());

My reason for wanting this is to create a Subversion pre-commit hook
using PHP_CodeSniffer: http://pear.php.net/package/PHP_CodeSniffer

I want:

1) Commits to our Subversion repository to be checked against our coding
standard with PHP_CodeSniffer
2) Commits to fail when PHP_CodeSniffer returns an error
3) PHP_CodeSniffer's report to be displayed to the Subversion user, so
they can fix any problems

I achieved 1) and 2), but PHP_CodeSniffer prints its report to stdout
and Subversion only displays stderr to the user, not stdout. So to make this pre-commit hook fool proof, I want it to redirect PHP_CodeSniffer's
report to stderr.

Anyone have better suggestions than output buffering?

Much thanks, Jack

Bojan Tesanovic
http://www.classicio.com/
http://www.carster.us/



Reply via email to