2017-11-12 09:28:04 -05:00
|
|
|
<?php
|
2020-06-17 09:17:59 -04:00
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2017-11-12 09:28:04 -05:00
|
|
|
/**
|
2024-05-23 03:26:56 -04:00
|
|
|
* SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
2017-11-12 09:28:04 -05:00
|
|
|
*/
|
2025-06-17 09:00:01 -04:00
|
|
|
|
2017-11-12 09:28:04 -05:00
|
|
|
namespace OC\Support\CrashReport;
|
|
|
|
|
|
|
|
|
|
use Exception;
|
2020-06-17 09:17:59 -04:00
|
|
|
use OCP\AppFramework\QueryException;
|
2025-06-17 09:00:01 -04:00
|
|
|
use OCP\Server;
|
2018-09-03 02:37:50 -04:00
|
|
|
use OCP\Support\CrashReport\ICollectBreadcrumbs;
|
2019-05-09 08:06:44 -04:00
|
|
|
use OCP\Support\CrashReport\IMessageReporter;
|
2017-11-12 09:28:04 -05:00
|
|
|
use OCP\Support\CrashReport\IRegistry;
|
|
|
|
|
use OCP\Support\CrashReport\IReporter;
|
2022-03-30 04:55:41 -04:00
|
|
|
use Psr\Log\LoggerInterface;
|
2017-11-12 09:28:04 -05:00
|
|
|
use Throwable;
|
2021-06-24 09:31:49 -04:00
|
|
|
use function array_shift;
|
2017-11-12 09:28:04 -05:00
|
|
|
|
|
|
|
|
class Registry implements IRegistry {
|
2020-06-17 09:17:59 -04:00
|
|
|
/** @var string[] */
|
|
|
|
|
private $lazyReporters = [];
|
|
|
|
|
|
2017-12-03 06:02:29 -05:00
|
|
|
/** @var IReporter[] */
|
2017-11-12 09:28:04 -05:00
|
|
|
private $reporters = [];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Register a reporter instance
|
|
|
|
|
*/
|
2019-05-09 08:06:44 -04:00
|
|
|
public function register(IReporter $reporter): void {
|
2017-11-12 09:28:04 -05:00
|
|
|
$this->reporters[] = $reporter;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-17 09:17:59 -04:00
|
|
|
public function registerLazy(string $class): void {
|
|
|
|
|
$this->lazyReporters[] = $class;
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-03 02:37:50 -04:00
|
|
|
/**
|
|
|
|
|
* Delegate breadcrumb collection to all registered reporters
|
|
|
|
|
*
|
2018-09-03 02:49:14 -04:00
|
|
|
* @since 15.0.0
|
2018-09-03 02:37:50 -04:00
|
|
|
*/
|
2019-05-09 08:06:44 -04:00
|
|
|
public function delegateBreadcrumb(string $message, string $category, array $context = []): void {
|
2020-06-17 09:17:59 -04:00
|
|
|
$this->loadLazyProviders();
|
|
|
|
|
|
2018-09-03 02:37:50 -04:00
|
|
|
foreach ($this->reporters as $reporter) {
|
|
|
|
|
if ($reporter instanceof ICollectBreadcrumbs) {
|
|
|
|
|
$reporter->collect($message, $category, $context);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-12 09:28:04 -05:00
|
|
|
/**
|
|
|
|
|
* Delegate crash reporting to all registered reporters
|
|
|
|
|
*
|
|
|
|
|
* @param Exception|Throwable $exception
|
|
|
|
|
*/
|
2019-05-09 08:06:44 -04:00
|
|
|
public function delegateReport($exception, array $context = []): void {
|
2020-06-17 09:17:59 -04:00
|
|
|
$this->loadLazyProviders();
|
|
|
|
|
|
2017-11-12 09:28:04 -05:00
|
|
|
foreach ($this->reporters as $reporter) {
|
2017-11-14 08:28:04 -05:00
|
|
|
$reporter->report($exception, $context);
|
2017-11-12 09:28:04 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-09 08:06:44 -04:00
|
|
|
/**
|
|
|
|
|
* Delegate a message to all reporters that implement IMessageReporter
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function delegateMessage(string $message, array $context = []): void {
|
2020-06-17 09:17:59 -04:00
|
|
|
$this->loadLazyProviders();
|
|
|
|
|
|
2019-05-09 08:06:44 -04:00
|
|
|
foreach ($this->reporters as $reporter) {
|
|
|
|
|
if ($reporter instanceof IMessageReporter) {
|
|
|
|
|
$reporter->reportMessage($message, $context);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-06-17 09:17:59 -04:00
|
|
|
|
|
|
|
|
private function loadLazyProviders(): void {
|
2021-06-24 09:31:49 -04:00
|
|
|
while (($class = array_shift($this->lazyReporters)) !== null) {
|
2020-06-17 09:17:59 -04:00
|
|
|
try {
|
|
|
|
|
/** @var IReporter $reporter */
|
2025-06-17 09:00:01 -04:00
|
|
|
$reporter = Server::get($class);
|
2020-06-17 09:17:59 -04:00
|
|
|
} catch (QueryException $e) {
|
|
|
|
|
/*
|
|
|
|
|
* There is a circular dependency between the logger and the registry, so
|
|
|
|
|
* we can not inject it. Thus the static call.
|
|
|
|
|
*/
|
2025-06-17 09:00:01 -04:00
|
|
|
Server::get(LoggerInterface::class)->critical('Could not load lazy crash reporter: ' . $e->getMessage(), [
|
2022-03-30 04:55:41 -04:00
|
|
|
'exception' => $e,
|
2020-06-17 09:17:59 -04:00
|
|
|
]);
|
2025-05-13 05:42:09 -04:00
|
|
|
return;
|
2020-06-17 09:17:59 -04:00
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* Try to register the loaded reporter. Theoretically it could be of a wrong
|
|
|
|
|
* type, so we might get a TypeError here that we should catch.
|
|
|
|
|
*/
|
|
|
|
|
try {
|
|
|
|
|
$this->register($reporter);
|
|
|
|
|
} catch (Throwable $e) {
|
|
|
|
|
/*
|
|
|
|
|
* There is a circular dependency between the logger and the registry, so
|
|
|
|
|
* we can not inject it. Thus the static call.
|
|
|
|
|
*/
|
2025-06-17 09:00:01 -04:00
|
|
|
Server::get(LoggerInterface::class)->critical('Could not register lazy crash reporter: ' . $e->getMessage(), [
|
2022-03-30 04:55:41 -04:00
|
|
|
'exception' => $e,
|
2020-06-17 09:17:59 -04:00
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-01-03 13:03:57 -05:00
|
|
|
|
|
|
|
|
public function hasReporters(): bool {
|
|
|
|
|
return !empty($this->lazyReporters) || !empty($this->reporters);
|
|
|
|
|
}
|
2017-11-12 09:28:04 -05:00
|
|
|
}
|