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;
25 * Get Introspection information about the current call
30 private $skipStackFramesCount;
33 private $skipClassesPartials;
35 private $skipFunctions = [
37 'call_user_func_array',
41 * @param string[] $skipClassesPartials An array of classes to skip during logging
42 * @param int $skipStackFramesCount If the logger should use information from other hierarchy levels of the call
44 public function __construct(array $skipClassesPartials = [], int $skipStackFramesCount = 0)
46 $this->skipClassesPartials = $skipClassesPartials;
47 $this->skipStackFramesCount = $skipStackFramesCount;
51 * Adds new classes to get skipped
53 * @param array $classNames
55 public function addClasses(array $classNames)
57 $this->skipClassesPartials = array_merge($this->skipClassesPartials, $classNames);
61 * Returns the introspection record of the current call
65 public function getRecord(): array
67 $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
71 while ($this->isTraceClassOrSkippedFunction($trace, $i)) {
75 $i += $this->skipStackFramesCount;
78 'file' => isset($trace[$i - 1]['file']) ? basename($trace[$i - 1]['file']) : null,
79 'line' => $trace[$i - 1]['line'] ?? null,
80 'function' => $trace[$i]['function'] ?? null,
85 * Checks if the current trace class or function has to be skipped
87 * @param array $trace The current trace array
88 * @param int $index The index of the current hierarchy level
90 * @return bool True if the class or function should get skipped, otherwise false
92 private function isTraceClassOrSkippedFunction(array $trace, int $index): bool
94 if (!isset($trace[$index])) {
98 if (isset($trace[$index]['class'])) {
99 foreach ($this->skipClassesPartials as $part) {
100 if (strpos($trace[$index]['class'], $part) !== false) {
104 } elseif (in_array($trace[$index]['function'], $this->skipFunctions)) {