/**
 * Due to the fact that DataGrid's will perform php eval's on anything that is back-ticked within each column/row's
 * DataGridColumn::HTML, we need to set up this special DataGridEvalHandleError error handler to correctly report
 * errors that happen.
 *
 * @global string $__exc_dtg_errstr
 * @param mixed $__exc_errno
 * @param string $__exc_errstr
 * @param string $__exc_errfile
 * @param string $__exc_errline
 */
function DataGridEvalHandleError($__exc_errno, $__exc_errstr, $__exc_errfile, $__exc_errline)
{
    $__exc_objBacktrace = debug_backtrace();
    for ($__exc_intIndex = 0; $__exc_intIndex < count($__exc_objBacktrace); $__exc_intIndex++) {
        $__exc_objItem = $__exc_objBacktrace[$__exc_intIndex];
        if (strpos($__exc_errfile, "DataGrid.inc") !== false && strpos($__exc_objItem["file"], "DataGrid.inc") === false) {
            $__exc_errfile = $__exc_objItem["file"];
            $__exc_errline = $__exc_objItem["line"];
        } else {
            if (strpos($__exc_errfile, "Form.inc") !== false && strpos($__exc_objItem["file"], "Form.inc") === false) {
                $__exc_errfile = $__exc_objItem["file"];
                $__exc_errline = $__exc_objItem["line"];
            }
        }
    }
    global $__exc_dtg_errstr;
    if (isset($__exc_dtg_errstr) && $__exc_dtg_errstr) {
        $__exc_errstr = sprintf("%s\n%s", $__exc_dtg_errstr, $__exc_errstr);
    }
    QcubedHandleError($__exc_errno, $__exc_errstr, $__exc_errfile, $__exc_errline, null);
}
/**
 * Some errors are not caught by a php custom error handler, which can cause the system to silently fail.
 * This shutdown function will catch those errors.
 */
function QCubedShutdown()
{
    if (defined('__TIMER_OUT_FILE__')) {
        $strTimerOutput = QTimer::VarDump(false);
        if ($strTimerOutput) {
            file_put_contents(__TIMER_OUT_FILE__, $strTimerOutput . "\n", FILE_APPEND);
        }
    }
    $error = error_get_last();
    if ($error && is_array($error) && (!defined('QCodeGen::DebugMode') || QCodeGen::DebugMode)) {
        // if we are codegenning, only error if we are in debug mode. Prevents chmod error.
        QcubedHandleError($error['type'], $error['message'], $error['file'], $error['line'], '');
    }
    //flush();	// required for hhvm
    //error_log("Flushed");
}