PHP Classes

File: examples/utils.inc.php

Recommend this page to a friend!
  Packages of Colin McKinnon   Stackable PHP Session Handler   examples/utils.inc.php   Download  
File: examples/utils.inc.php
Role: Example script
Content type: text/plain
Description: Utilitiy functions for the example files
Class: Stackable PHP Session Handler
Store session data with multiple session handlers
Author: By
Last change:
Date: 11 years ago
Size: 3,137 bytes
 

 

Contents

Class file image Download
<?php
$utils_start_time=microtime(true);
register_shutdown_function('utils_finished');
set_error_handler('error_handler');

$e_user_error_levels=array(
0=>'E_USER_DEBUG',     E_ERROR=>'E_ERROR',
E_WARNING=>'E_WARNING',        E_PARSE=>'E_PARSE',
E_NOTICE=>'E_NOTICE',        E_CORE_ERROR=>'E_CORE_ERROR',
E_CORE_WARNING=>'E_CORE_WARNING',    E_COMPILE_ERROR=>'E_COMPILE_ERROR',
E_COMPILE_WARNING=>'E_COMPILE_WARNING',    E_USER_ERROR=>'E_USER_ERROR',
E_USER_WARNING=>'E_USER_WARNING',    E_USER_NOTICE=>'E_USER_NOTICE',
E_STRICT=>'E_STRICT',    E_RECOVERABLE_ERROR=>'E_RECOVERABLE_ERROR',
E_DEPRECATED=>'E_DEPRECATED',    E_USER_DEPRECATED=>'E_USER_DEPRECATED'
);

function utils_finished()
{
    global $utils_start_time;
    $duration=1000*(microtime(true)-$utils_start_time);
    $duration=round($duration, 3);
    $mem=(integer)(memory_get_usage()/1024);
    print "<hr />Duration: $duration msec  Memory: $mem kb\n";
    print "<button name='refresh' onclick='window.location.reload()'>refresh</button>\n";
    print "&nbsp;&nbsp;<a href=\"../\">Return to docs</a><br /><br />\n</html>";
}

function logger($msg, $level=0)
{
    global $statuslog,$e_user_error_levels;
    $statuslog.=(isset($e_user_error_levels[$level]) ?
        $e_user_error_levels[$level] : 'ERROR' )
        . ':' . $msg . "\n";
}

function error_handler($errno, $errstr, $errfile, $errline)
{
        $msg="Error $errno: $errstr : at line $errline of $errfile";
    return niceDebugBacktrace($msg);
}
function niceDebugBacktrace($msg)
{
    global $statuslog;
    // from http://makandracards.com/magento/8123-pretty-backtrace-stack-trace
    $d = debug_backtrace();
    array_shift($d);
    array_shift($d);
    $out = $msg . "\n";
    $c1width = strlen(count($d) + 1);
    $c2width = 0;
    $basepath=dirname(dirname(__FILE__));
    foreach ($d as &$f) {
        if (!isset($f['file'])) $f['file'] = '';
        if (!isset($f['line'])) $f['line'] = '';
        if (!isset($f['class'])) $f['class'] = '';
        if (!isset($f['type'])) $f['type'] = '';
        $f['file_rel'] = str_replace($basepath, '', $f['file']);
        $thisLen = strlen($f['file_rel'] . ':' . $f['line']);
        if ($c2width < $thisLen) $c2width = $thisLen;
    }
    foreach ($d as $i => $f) {
        $args = '';
        if (isset($f['args'])) {
            $args = array();
            foreach ($f['args'] as $arg) {
                if (is_object($arg)) {
                    $str = get_class($arg);
                } elseif (is_array($arg)) {
                    $str = 'Array';
                } elseif (is_numeric($arg)) {
                    $str = $arg;
                } else {
                    $str = "'$arg'";
                }
                $args[] = $str;
            }
            $args = implode(', ', $args);
        }
        $out .= sprintf(
            "[%{$c1width}s] %-{$c2width}s %s%s%s(%s)\n",
            $i,
            $f['file_rel'] . ':' . $f['line'],
            $f['class'],
            $f['type'],
            $f['function'],
            $args
        );
    }
    $stderr = fopen('php://stderr', 'w');
    fwrite($stderr,$out.$statuslog);
    print "<pre>$out</pre><hr /><pre>$statuslog</pre>";
    return false;
}