Core\Debug::stopTrace
Core\Debug::stopTrace(): voidStops the trace that Core\Debug::startTrace started, and closes its file.
After Core\Debug::stopTrace, the trace file is complete, and your program can read it. A call that returns after the stop adds no line. A call that is still running when the trace stops also has no line, even when it returns later.
When no trace is running, stopTrace does nothing. So you can call it in a finally block, and the trace stops even when a step throws an error. stopTrace throws an error only when Novis could not write every call to the file, for example because the disk is full.
The examples below trace one step of a job, stop a trace in a finally block, and show which calls a trace has when it stops inside a method.
Return value
Returns void. Nothing. It does nothing when no trace is running.
Errors
RuntimeErrorNot every call could be written to the file. The trace is stopped anyway.
Examples
The examples below run with this configuration file.
# The examples write a trace of their calls to a file in this folder.# `mode` allows a program to start a trace.[debug]mode = ["trace"]
# The program may write a trace file in this folder, and nowhere else.# A path in `[capabilities]` is relative to this file.[capabilities.debug]trace = ["."]Trace only one step
<?nvs// Traces one step of a nightly job, and stops the trace before the next step.
class Nightly{ public static function importOrders(): int { return 12; }
public static function sendReport(int $orders): string { return 'report: ' . $orders . ' orders'; }}
string $file = Core\Path::thisDirectory('import.jsonl');
// Only the import is traced. `stopTrace` closes the file before the report starts.Core\Debug::startTrace($file);int $orders = Nightly::importOrders();Core\Debug::stopTrace();
echo Nightly::sendReport($orders), "\n";
// The file has no line for `sendReport`, because it ran after the stop.foreach (Core\IO::lines($file) as string $line) { var $call = Core\Json::decode($line) as array<mixed>; echo 'traced: ', $call['callee'] as string, "\n";}
// This deletes the trace file, so the next run starts with an empty folder.Core\IO::remove($file);report: 12 orderstraced: Core\Debug::startTracetraced: Nightly::importOrdersStop even when a step fails
<?nvs// Stops a trace in a `finally` block, so the trace ends even when a step throws an error.
class Payments{ public static function charge(int $cents): string { if ($cents <= 0) { throw new RuntimeError('the amount must be more than zero'); } return 'charged ' . $cents; }}
string $file = Core\Path::thisDirectory('payments.jsonl');
Core\Debug::startTrace($file);try { echo Payments::charge(1999), "\n"; echo Payments::charge(0), "\n";} catch (RuntimeError $error) { echo 'failed: ', $error->message, "\n";} finally { Core\Debug::stopTrace();}
// `status` is `thrown` for the call that threw the error.// `Throwable::constructor` is the line for creating the error.foreach (Core\IO::lines($file) as string $line) { var $call = Core\Json::decode($line) as array<mixed>; echo $call['callee'] as string, ': ', $call['status'] as string, "\n";}
// This deletes the trace file, so the next run starts with an empty folder.Core\IO::remove($file);charged 1999failed: the amount must be more than zeroCore\Debug::startTrace: okPayments::charge: okThrowable::constructor: okPayments::charge: thrownA call that has not returned
<?nvs// Shows which calls a trace has when it stops inside a method.
class Signup{ public static function checkEmail(string $email): bool { return Core\Str::contains($email, '@'); }
// The trace stops here, before `register` returns. public static function register(string $email): string { bool $valid = Signup::checkEmail($email); Core\Debug::stopTrace(); return $valid ? 'welcome' : 'invalid email'; }}
string $file = Core\Path::thisDirectory('signup.jsonl');
Core\Debug::startTrace($file);echo Signup::register('ada@example.com'), "\n";
// No trace is running now, so this second stop does nothing.Core\Debug::stopTrace();
// `checkEmail` returned before the stop, so it has a line. `register` has none.foreach (Core\IO::lines($file) as string $line) { var $call = Core\Json::decode($line) as array<mixed>; echo 'traced: ', $call['callee'] as string, "\n";}
// This deletes the trace file, so the next run starts with an empty folder.Core\IO::remove($file);welcometraced: Core\Debug::startTracetraced: Core\Str::containstraced: Signup::checkEmail