|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* @copyright 2020 Christoph Wurst <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* @author 2020 Christoph Wurst <[email protected]> |
|
9
|
|
|
* |
|
10
|
|
|
* @license GNU AGPL version 3 or any later version |
|
11
|
|
|
* |
|
12
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
13
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
14
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
15
|
|
|
* License, or (at your option) any later version. |
|
16
|
|
|
* |
|
17
|
|
|
* This program is distributed in the hope that it will be useful, |
|
18
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
19
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
20
|
|
|
* GNU Affero General Public License for more details. |
|
21
|
|
|
* |
|
22
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
23
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
24
|
|
|
*/ |
|
25
|
|
|
|
|
26
|
|
|
namespace OCA\Sentry\Reporter; |
|
27
|
|
|
|
|
28
|
|
|
use OCP\Support\CrashReport\ICollectBreadcrumbs; |
|
29
|
|
|
use OCP\Support\CrashReport\IMessageReporter; |
|
30
|
|
|
use OCP\Support\CrashReport\IReporter; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Decorator that detects and stops recursive calls to reporter methods |
|
34
|
|
|
*/ |
|
35
|
|
|
class RecursionAwareReporter implements IMessageReporter, ICollectBreadcrumbs { |
|
36
|
|
|
|
|
37
|
|
|
/** @var IReporter */ |
|
38
|
|
|
private $reporter; |
|
39
|
|
|
|
|
40
|
|
|
private $reporting = false; |
|
41
|
|
|
|
|
42
|
|
|
public function __construct(IReporter $reporter) { |
|
43
|
|
|
$this->reporter = $reporter; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
private function guard(callable $run): void { |
|
47
|
|
|
try { |
|
48
|
|
|
if ($this->reporting) { |
|
49
|
|
|
// Break the recursion |
|
50
|
|
|
return; |
|
51
|
|
|
} |
|
52
|
|
|
$this->reporting = true; |
|
53
|
|
|
$run(); |
|
54
|
|
|
} finally { |
|
55
|
|
|
$this->reporting = false; |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function collect(string $message, string $category, array $context = []): void { |
|
60
|
|
|
if ($this->reporter instanceof ICollectBreadcrumbs) { |
|
61
|
|
|
$this->guard(function() use ($context, $category, $message) { |
|
62
|
|
|
$this->reporter->collect($message, $category, $context); |
|
|
|
|
|
|
63
|
|
|
}); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
public function reportMessage(string $message, array $context = []): void { |
|
68
|
|
|
if ($this->reporter instanceof IMessageReporter) { |
|
69
|
|
|
$this->guard(function() use ($context, $message) { |
|
70
|
|
|
$this->reporter->reportMessage($message, $context); |
|
|
|
|
|
|
71
|
|
|
}); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function report($exception, array $context = []): void { |
|
76
|
|
|
$this->guard(function() use ($context, $exception) { |
|
77
|
|
|
$this->reporter->report($exception, $context); |
|
78
|
|
|
}); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
} |
|
82
|
|
|
|