3 * @copyright Copyright (C) 2010-2022, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Core\Logger\Util;
24 use Friendica\Core\Logger\Capabilities\IHaveCallIntrospections;
27 * Get Introspection information about the current call
29 class Introspection implements IHaveCallIntrospections
32 private $skipStackFramesCount;
35 private $skipClassesPartials;
37 private $skipFunctions = [
39 'call_user_func_array',
43 * @param string[] $skipClassesPartials An array of classes to skip during logging
44 * @param int $skipStackFramesCount If the logger should use information from other hierarchy levels of the call
46 public function __construct(array $skipClassesPartials = [], int $skipStackFramesCount = 0)
48 $this->skipClassesPartials = $skipClassesPartials;
49 $this->skipStackFramesCount = $skipStackFramesCount;
53 * Adds new classes to get skipped
55 * @param array $classNames
57 public function addClasses(array $classNames): void
59 $this->skipClassesPartials = array_merge($this->skipClassesPartials, $classNames);
63 * Returns the introspection record of the current call
67 public function getRecord(): array
69 $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
73 while ($this->isTraceClassOrSkippedFunction($trace, $i)) {
77 $i += $this->skipStackFramesCount;
80 'file' => isset($trace[$i - 1]['file']) ? basename($trace[$i - 1]['file']) : null,
81 'line' => $trace[$i - 1]['line'] ?? null,
82 'function' => $trace[$i]['function'] ?? null,
87 * Checks if the current trace class or function has to be skipped
89 * @param array $trace The current trace array
90 * @param int $index The index of the current hierarchy level
92 * @return bool True if the class or function should get skipped, otherwise false
94 private function isTraceClassOrSkippedFunction(array $trace, int $index): bool
96 if (!isset($trace[$index])) {
100 if (isset($trace[$index]['class'])) {
101 foreach ($this->skipClassesPartials as $part) {
102 if (strpos($trace[$index]['class'], $part) !== false) {
106 } elseif (in_array($trace[$index]['function'], $this->skipFunctions)) {