Skip to content

Commit 8da31c9

Browse files
authored
Support to log the state machine data (#1168)
Adds support for logging the lifecycle state machine data. - Introduces a new C++ binding (`print`) that calls `rcl_print_state_machine` - Exposes a `print()` method on the JavaScript `LifecycleNode` class - Adds TypeScript and JS tests to verify the new `print()` functionality Fix: #1167
1 parent 3c4492b commit 8da31c9

File tree

5 files changed

+35
-0
lines changed

5 files changed

+35
-0
lines changed

lib/lifecycle.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,15 @@ class LifecycleNode extends Node {
600600
return rclnodejs.isInitialized(this._stateMachineHandle);
601601
}
602602

603+
/**
604+
* Log the state machine data.
605+
*
606+
* @returns {undefined} void.
607+
*/
608+
print() {
609+
rclnodejs.print(this._stateMachineHandle);
610+
}
611+
603612
/**
604613
* The GetState service handler.
605614
* @param {Object} request - The GetState service request.

src/rcl_lifecycle_bindings.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,17 @@ Napi::Value IsInitialized(const Napi::CallbackInfo& info) {
372372
return Napi::Boolean::New(env, is_initialized);
373373
}
374374

375+
Napi::Value Print(const Napi::CallbackInfo& info) {
376+
Napi::Env env = info.Env();
377+
RclHandle* state_machine_handle =
378+
RclHandle::Unwrap(info[0].As<Napi::Object>());
379+
rcl_lifecycle_state_machine_t* state_machine =
380+
reinterpret_cast<rcl_lifecycle_state_machine_t*>(
381+
state_machine_handle->ptr());
382+
rcl_print_state_machine(state_machine);
383+
return env.Undefined();
384+
}
385+
375386
Napi::Object InitLifecycleBindings(Napi::Env env, Napi::Object exports) {
376387
exports.Set("createLifecycleStateMachine",
377388
Napi::Function::New(env, CreateLifecycleStateMachine));
@@ -396,6 +407,7 @@ Napi::Object InitLifecycleBindings(Napi::Env env, Napi::Object exports) {
396407
exports.Set("getLifecycleShutdownTransitionLabel",
397408
Napi::Function::New(env, GetLifecycleShutdownTransitionLabel));
398409
exports.Set("isInitialized", Napi::Function::New(env, IsInitialized));
410+
exports.Set("print", Napi::Function::New(env, Print));
399411
return exports;
400412
}
401413

test/test-lifecycle.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,4 +356,10 @@ describe('LifecycleNode test suite', function () {
356356
assert.equal(serviceMsgs.length, 0, 'Unexpected lifecycle services found');
357357
testNode.destroy();
358358
});
359+
360+
it('lifecycleNode initial state', function () {
361+
assert.doesNotThrow(() => {
362+
node.print();
363+
});
364+
});
359365
});

test/types/index.test-d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ expectType<rclnodejs.lifecycle.State>(lifecycleNode.deactivate());
136136
expectType<rclnodejs.lifecycle.State>(lifecycleNode.cleanup());
137137
expectType<rclnodejs.lifecycle.State>(lifecycleNode.shutdown());
138138
expectType<boolean>(lifecycleNode.isInitialized);
139+
expectType<void>(lifecycleNode.print());
139140

140141
// ---- Publisher ----
141142
const publisher = node.createPublisher(TYPE_CLASS, TOPIC);

types/lifecycle.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,13 @@ declare module 'rclnodejs' {
333333
* @returns {boolean} true if the state machine is initialized; otherwise false.
334334
*/
335335
get isInitialized(): boolean;
336+
337+
/**
338+
* Log the state machine data.
339+
*
340+
* @returns {undefined} void.
341+
*/
342+
print(): void;
336343
}
337344
} // lifecycle namespace
338345
} // rclnodejs namespace

0 commit comments

Comments
 (0)