Example #1
0
/**
 * Lightwork shutdown function
 *
 * This function gets executed everytime a script finishes in order to clean stuff up and log some final debug messages.
 *
 * @param float $start The start microtime of this request
 */
function shutdown($start)
{
    $error = error_get_last();
    // Get the last error that occured...
    if (!empty($error)) {
        Lightwork::Log(sprintf('Shutdown with error: [%s] %s in %s at %s (php.log may contain more info)', $error['type'], $error['message'], $error['file'], $error['line']), Lightwork::LOG_ERROR);
    }
    // ...and log it - it might proof useful
    if (METHOD != 'CLI') {
        Lightwork::Log('Request ended with status ' . http_response_code(), Lightwork::LOG_DEBUG);
    }
    // Log the status the request ended with if this is not a CLI request
    if (LOG_DATABASE) {
        Lightwork::Log(Database::Queries() . ' queries were executed for this request.', Lightwork::LOG_DEBUG);
    }
    // Log how many queries were executed
    if (LOG_INCLUDES) {
        $history = 'Used files:';
        $files = get_included_files();
        // Get all files that were included...
        foreach ($files as $file) {
            $history .= ' > ' . basename($file, '.php');
            // ...and log them - this might be useful for debugging and performance optimization
        }
        Lightwork::Log('A total of ' . count($files) . ' files were used for this request.', Lightwork::LOG_DEBUG);
        // Log the amount of files included...
        Lightwork::Log($history, Lightwork::LOG_DEBUG);
        // ...and the actual history of files
    }
    if (LOG_MEMORYUSAGE) {
        // Measure memory usage and memory limit
        Lightwork::Log('Memory usage: ' . number_format(memory_get_usage() / 1024 / 1024, 2) . 'MB/' . ini_get('memory_limit') . 'B (peak: ' . number_format(memory_get_peak_usage() / 1024 / 1024, 2) . ')', Lightwork::LOG_DEBUG);
    }
    if (LOG_PERFORMANCE) {
        // Measure the time that passed since initialization and script shutdown
        Lightwork::Log('Execution time: ' . number_format(microtime(true) - $start, 3) . 's', Lightwork::LOG_DEBUG);
    }
    // If wanted, log execution time
    Lightwork::Flush();
    // Flush log file to disk
}