3 * @copyright Copyright (C) 2020, Friendica
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\Util;
25 * Get Introspection information about the current call
29 private $skipStackFramesCount;
31 private $skipClassesPartials;
33 private $skipFunctions = [
35 'call_user_func_array',
39 * @param array $skipClassesPartials An array of classes to skip during logging
40 * @param int $skipStackFramesCount If the logger should use information from other hierarchy levels of the call
42 public function __construct($skipClassesPartials = array(), $skipStackFramesCount = 0)
44 $this->skipClassesPartials = $skipClassesPartials;
45 $this->skipStackFramesCount = $skipStackFramesCount;
49 * Adds new classes to get skipped
50 * @param array $classNames
52 public function addClasses(array $classNames)
54 $this->skipClassesPartials = array_merge($this->skipClassesPartials, $classNames);
58 * Returns the introspection record of the current call
62 public function getRecord()
64 $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
68 while ($this->isTraceClassOrSkippedFunction($trace, $i)) {
72 $i += $this->skipStackFramesCount;
75 'file' => isset($trace[$i - 1]['file']) ? basename($trace[$i - 1]['file']) : null,
76 'line' => isset($trace[$i - 1]['line']) ? $trace[$i - 1]['line'] : null,
77 'function' => isset($trace[$i]['function']) ? $trace[$i]['function'] : null,
82 * Checks if the current trace class or function has to be skipped
84 * @param array $trace The current trace array
85 * @param int $index The index of the current hierarchy level
87 * @return bool True if the class or function should get skipped, otherwise false
89 private function isTraceClassOrSkippedFunction(array $trace, $index)
91 if (!isset($trace[$index])) {
95 if (isset($trace[$index]['class'])) {
96 foreach ($this->skipClassesPartials as $part) {
97 if (strpos($trace[$index]['class'], $part) !== false) {
101 } elseif (in_array($trace[$index]['function'], $this->skipFunctions)) {