Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve initialization crash message #27138

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 35 additions & 24 deletions CRM/Core/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -468,46 +468,57 @@ function_exists($config->fatalErrorHandler)
*
* @param string $name name of debug section
* @param mixed $variable reference to variables that we need a trace of
* @param bool $log should we log or return the output
* @param bool $html whether to generate a HTML-escaped output
* @param bool $log
* - TRUE: log to error_log and echo one of: everything / a code / nothing, depending on permissions.
* - FALSE: only return the output as a string. Nothing to error_log or echo.
* @param bool $html whether to HTML-escape output (typically use TRUE unless you know your $variable is safe)
* @param bool $checkPermission should we check permissions before displaying output
* useful when we die during initialization and permissioning
* subsystem is not initialized - CRM-13765
* useful when we die during initialization and permissioning
* subsystem is not initialized - CRM-13765
*
* @return string
* the generated output
*
* @deprecated prefer Civi::log().
* @see https://github.com/civicrm/civicrm-core/pull/27138
*/
public static function debug($name, $variable = NULL, $log = TRUE, $html = TRUE, $checkPermission = TRUE) {
$error = self::singleton();

if ($variable === NULL) {
$variable = $name;
$name = NULL;
}

$out = print_r($variable, TRUE);
$prefix = NULL;
if ($html) {
$out = htmlspecialchars($out);
if ($name) {
$prefix = "<p>$name</p>";
}
$out = "{$prefix}<p><pre>$out</pre></p><p></p>";
$outHTML = htmlspecialchars($out);

if ($name) {
$outHTML = "<p>" . htmlspecialchars($name) . "</p><p><pre>$outHTML</pre></p>";
$out = "$name:\n$out";
}
else {
if ($name) {
$prefix = "$name:\n";

if ($log) {
// Log the output to error_log with a unique reference.
$unique = substr(md5(random_bytes(32)), 0, 12);
error_log("errorID:$unique\n$out");

if (!$checkPermission) {
// Permission system inactive, only emit a reference to content in logfile
echo "Critical error. Please see server logs for errorID:$unique";
}
else {
if (CRM_Core_Permission::check('view debug output')) {
// We are outputting to the browser.
echo $html ? $outHTML : $out;
}
else {
// No permission; show nothing visible, but include the error in an HTML
// comment in case a dev wants to inspect.
echo $html ? "<!-- critical error reference $unique -->" : '';
}
}
$out = "{$prefix}$out\n";
}
if (
$log &&
(!$checkPermission || CRM_Core_Permission::check('view debug output'))
) {
echo $out;
}

return $out;
return $html ? $outHTML : $out;
}

/**
Expand Down