From e0f8bad2d97bb7f3a5332528f799bae2942354c8 Mon Sep 17 00:00:00 2001 From: Clemens Schwaighofer Date: Fri, 16 Jun 2023 13:24:34 +0900 Subject: [PATCH] DB\IO Call trace in context array for any debug calls --- src/DB/Extended/ArrayIO.php | 2 +- src/DB/IO.php | 32 +++++++++++++++++++++++++++++--- src/Logging/Logging.php | 2 +- 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/src/DB/Extended/ArrayIO.php b/src/DB/Extended/ArrayIO.php index 36a9fd3..9b6b734 100644 --- a/src/DB/Extended/ArrayIO.php +++ b/src/DB/Extended/ArrayIO.php @@ -588,7 +588,7 @@ class ArrayIO extends \CoreLibs\DB\IO // get it at the end, cause now we can be more sure of no double IDs, etc reset($this->table_array); // create select part & addition FK part - foreach ($this->table_array as $column => $data_array) { + foreach (array_keys($this->table_array) as $column) { // check FK ... if ( isset($this->table_array[$column]['fk']) && diff --git a/src/DB/IO.php b/src/DB/IO.php index 9c6e4ae..622c4f7 100644 --- a/src/DB/IO.php +++ b/src/DB/IO.php @@ -792,19 +792,45 @@ class IO . \CoreLibs\Debug\Support::prAr($error_data) . ']'; } + // we need to trace back where the first non class call was done + // add this stack trace to the context + $call_stack = []; + foreach (array_reverse(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS)) as $call_trace) { + if ( + !empty($call_trace['file']) && + str_ends_with($call_trace['file'], '/DB/IO.php') + ) { + break; + } + $call_stack[] = + ($call_trace['file'] ?? 'n/f') . ':' + . ($call_trace['line'] ?? '-') . ':' + . (!empty($call_trace['class']) ? $call_trace['class'] . '->' : '') + . $call_trace['function']; + } + $context = [ + 'call_trace' => array_reverse($call_stack) + ]; switch ($id) { case 'DB_ERROR': - $this->log->error($debug_id . ' :' . $prefix . $error_string); + $this->log->error( + $debug_id . ' :' . $prefix . $error_string, + $context + ); break; case 'DB_WARNING': - $this->log->warning($debug_id . ' :' . $prefix . $error_string); + $this->log->warning( + $debug_id . ' :' . $prefix . $error_string, + $context + ); break; default: // used named arguments so we can easy change the order of debug $this->log->debug( group_id: $debug_id, message: $error_string, - prefix: $prefix + prefix: $prefix, + context: $context ); break; } diff --git a/src/Logging/Logging.php b/src/Logging/Logging.php index 443a4be..6c74845 100644 --- a/src/Logging/Logging.php +++ b/src/Logging/Logging.php @@ -567,7 +567,7 @@ class Logging $context_str = ''; if ($context != []) { // TODO this here has to be changed to something better - $context_str = ' ' . print_r($context, true); + $context_str = ' :' . print_r($context, true); } // build log string return '[' . $timestamp . '] '