Skip to content

Core\Debug::recordIfArmed

Core\Debug::recordIfArmed(string $point): boolCore\Debug::recordIfArmed(
    string $point,
): bool

Names a place in your code where a developer can start recording requests from the debug viewer.

Core\Debug::recordIfArmed takes a point name, such as checkout. Until a developer starts a capture for that name in the viewer, the call does nothing and returns false. While the capture runs, the call returns true and the server records the rest of this request. The capture stops on its own after the time the developer chose.

The call never starts recording by itself. [debug] store, the setting that records every request, does not change the result either.

Good to know: the point name must be written in your code. A value from the request is tainted: it came from outside the program. A tainted name does not compile. So a request cannot choose which point it reaches.

The examples below show a point at the top of a checkout, a log line written only when a request is recorded, and a point name built from a constant.

Parameters

  • $pointstringA name for this place in your code, such as 'checkout'. A developer types this name in the viewer to start a capture for it.

Return value

Returns bool. true when the server records this request after this call. Otherwise false, and the call changes nothing.

Changelog

VersionChange
0.0.6Core\Debug::recordIfArmed records the request only while a developer has started a capture for its point in the debug viewer. A capture records requests for a short time, and then ends on its own.

Examples

A point at the top of a checkout

<?nvs
// Shows a point named `checkout` at the start of a checkout.
array<int> $cart = [1999, 450, 450];
// A capture records requests for a time a developer chooses in the debug viewer.
// Nobody started a capture for `checkout` here, so this returns `false`.
Core\Debug::recordIfArmed('checkout');
int $total = 0;
foreach ($cart as int $price) {
$total = $total + $price;
}
// This prints: total 2899 cents.
echo 'total ', $total, " cents\n";
Output
total 2899 cents

A log line only when recorded

<?nvs
// Shows how to use the result of `Core\Debug::recordIfArmed`. The program
// writes a log line only when a capture records this request.
bool $recorded = Core\Debug::recordIfArmed('search');
// While a capture for `search` runs, this writes one log record.
// Nobody started a capture here, so the result is `false` and nothing is written.
if ($recorded) {
Core\Log::write(Core\Log::Level::Info, 'this search is recorded');
}
// This prints: recorded: no.
echo 'recorded: ', $recorded ? 'yes' : 'no', "\n";
Output
recorded: no

A point name from a constant

<?nvs
// Shows a point name built from a class constant. The name is written in
// the code, so it compiles. A name read from the request is tainted, and it does not compile.
class Points
{
public const string PREFIX = 'report.';
}
// The point name is `report.monthly`.
string $point = Points::PREFIX . 'monthly';
// Nobody started a capture for this name, so this prints: report.monthly: false.
echo $point, ': ', Core\Debug::recordIfArmed($point) ? 'true' : 'false', "\n";
Output
report.monthly: false