diff --git a/lib/dml/moodle_database.php b/lib/dml/moodle_database.php index 091fdeff374..4621c731ed3 100644 --- a/lib/dml/moodle_database.php +++ b/lib/dml/moodle_database.php @@ -889,9 +889,6 @@ abstract class moodle_database { // convert table names $sql = $this->fix_table_names($sql); - // Optionally add debug trace to sql as a comment. - $sql = $this->add_sql_debugging($sql); - // cast booleans to 1/0 int and detect forbidden objects foreach ($params as $key => $value) { $this->detect_objects($value); @@ -903,6 +900,9 @@ abstract class moodle_database { $dollar_count = preg_match_all('/\$[1-9][0-9]*/', $sql, $dollar_matches); $q_count = substr_count($sql, '?'); + // Optionally add debug trace to sql as a comment. + $sql = $this->add_sql_debugging($sql); + $count = 0; if ($named_count) { diff --git a/lib/dml/tests/dml_test.php b/lib/dml/tests/dml_test.php index 95341605a15..3e950457c72 100644 --- a/lib/dml/tests/dml_test.php +++ b/lib/dml/tests/dml_test.php @@ -519,6 +519,30 @@ EOD; $CFG->debugsqltrace = 0; } + /** + * Test the database debugging as SQL comment in anon class + * + * @covers ::add_sql_debugging + */ + public function test_sql_debugging_anon_class() { + global $CFG; + $CFG->debugsqltrace = 100; + + // A anon class. + $another = new class { + /** + * Just a test log function + */ + public function log() { + global $DB; + $DB->get_records_sql('SELECT firstname FROM {user} WHERE firstname = :firstname', ['firstname' => 'JohnDoe']); + } + }; + $another->log(); + $CFG->debugsqltrace = 0; + // No assertions just it should not error. + } + public function test_strtok() { // Strtok was previously used by bound emulation, make sure it is not used any more. $DB = $this->tdb; diff --git a/lib/setuplib.php b/lib/setuplib.php index aacdb699948..d1065dc8150 100644 --- a/lib/setuplib.php +++ b/lib/setuplib.php @@ -714,18 +714,23 @@ function format_backtrace($callers, $plaintext = false) { if (!isset($caller['file'])) { $caller['file'] = 'unknownfile'; // probably call_user_func() } - $from .= $plaintext ? '* ' : '
  • '; - $from .= 'line ' . $caller['line'] . ' of ' . str_replace($dirroot, '', $caller['file']); + $line = $plaintext ? '* ' : '
  • '; + $line .= 'line ' . $caller['line'] . ' of ' . str_replace($dirroot, '', $caller['file']); if (isset($caller['function'])) { - $from .= ': call to '; + $line .= ': call to '; if (isset($caller['class'])) { - $from .= $caller['class'] . $caller['type']; + $line .= $caller['class'] . $caller['type']; } - $from .= $caller['function'] . '()'; + $line .= $caller['function'] . '()'; } else if (isset($caller['exception'])) { - $from .= ': '.$caller['exception'].' thrown'; + $line .= ': '.$caller['exception'].' thrown'; } - $from .= $plaintext ? "\n" : '
  • '; + + // Remove any non printable chars. + $line = preg_replace('/[[:^print:]]/', '', $line); + + $line .= $plaintext ? "\n" : ''; + $from .= $line; } $from .= $plaintext ? '' : '';