Skip to content

Commit 0c7b9af

Browse files
authored
Support getting logger name for subscription, publisher, service and client (#1224)
This pull request adds support for getting the logger name for ROS 2 entities (subscription, publisher, service, and client) by exposing a `loggerName` getter property on each entity class. - Adds `loggerName` getter property to Publisher, Subscription, Service, and Client classes - Stores node references in Publisher and Subscription constructors to enable logger name access - Adds comprehensive test coverage for the new `loggerName` property across all entity types Fix: #1219
1 parent a5f8aec commit 0c7b9af

File tree

12 files changed

+90
-0
lines changed

12 files changed

+90
-0
lines changed

lib/client.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,14 @@ class Client extends Entity {
157157
introspectionState
158158
);
159159
}
160+
161+
/**
162+
* Get the logger name for this client.
163+
* @returns {string} The logger name for this client.
164+
*/
165+
get loggerName() {
166+
return rclnodejs.getNodeLoggerName(this._nodeHandle);
167+
}
160168
}
161169

162170
module.exports = Client;

lib/publisher.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const Entity = require('./entity.js');
2626
class Publisher extends Entity {
2727
constructor(handle, typeClass, topic, options, node, eventCallbacks) {
2828
super(handle, typeClass, options);
29+
this._node = node;
2930
if (node && eventCallbacks) {
3031
this._events = eventCallbacks.createEventHandlers(this.handle);
3132
node._events.push(...this._events);
@@ -126,6 +127,14 @@ class Publisher extends Entity {
126127
set events(events) {
127128
this._events = events;
128129
}
130+
131+
/**
132+
* Get the logger name for this publisher.
133+
* @returns {string} The logger name for this publisher.
134+
*/
135+
get loggerName() {
136+
return rclnodejs.getNodeLoggerName(this._node.handle);
137+
}
129138
}
130139

131140
module.exports = Publisher;

lib/service.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,14 @@ class Service extends Entity {
149149
getOptions() {
150150
return rclnodejs.getOptions(this._handle);
151151
}
152+
153+
/**
154+
* Get the logger name for this service.
155+
* @returns {string} The logger name for this service.
156+
*/
157+
get loggerName() {
158+
return rclnodejs.getNodeLoggerName(this._nodeHandle);
159+
}
152160
}
153161

154162
module.exports = Service;

lib/subscription.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class Subscription extends Entity {
4242
this._topic = topic;
4343
this._callback = callback;
4444
this._isRaw = options.isRaw || false;
45+
this._node = node;
4546

4647
if (node && eventCallbacks) {
4748
this._events = eventCallbacks.createEventHandlers(this.handle);
@@ -168,6 +169,14 @@ class Subscription extends Entity {
168169
set events(events) {
169170
this._events = events;
170171
}
172+
173+
/**
174+
* Get the logger name for this subscription.
175+
* @returns {string} The logger name for this subscription.
176+
*/
177+
get loggerName() {
178+
return rclnodejs.getNodeLoggerName(this._node.handle);
179+
}
171180
}
172181

173182
module.exports = Subscription;

test/test-publisher.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@ describe('rclnodejs publisher test suite', function () {
5252
assert.strictEqual(publisher.subscriptionCount, 1);
5353
});
5454

55+
it('Test loggerName', function () {
56+
const node = rclnodejs.createNode('publisher_node');
57+
const String = 'std_msgs/msg/String';
58+
const publisher = node.createPublisher(String, 'topic');
59+
assert.strictEqual(typeof publisher.loggerName, 'string');
60+
});
61+
5562
it('Wait for all acked', function () {
5663
const node = rclnodejs.createNode('publisher_node');
5764
const String = 'std_msgs/msg/String';

test/test-service.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,22 @@ describe('Test service class', function () {
9393
);
9494
node.destroy();
9595
});
96+
97+
it('Test loggerName', function () {
98+
const node = rclnodejs.createNode('test_node');
99+
const AddTwoInts = 'example_interfaces/srv/AddTwoInts';
100+
const service = node.createService(
101+
AddTwoInts,
102+
'add_two_ints',
103+
{ qos: rclnodejs.QoS.profileSystemDefault },
104+
(request, response) => {
105+
let result = response.template;
106+
result.sum = request.a + request.b;
107+
}
108+
);
109+
const client = node.createClient(AddTwoInts, 'single_ps_channel2');
110+
111+
assert.strictEqual(typeof service.loggerName, 'string');
112+
assert.strictEqual(typeof client.loggerName, 'string');
113+
});
96114
});

test/test-subscription.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,11 @@ describe('rclnodejs subscription test suite', function () {
3535
const subscription = node.createSubscription(String, 'topic', (msg) => {});
3636
assert.strictEqual(subscription.publisherCount, 1);
3737
});
38+
39+
it('Test loggerName', function () {
40+
const node = rclnodejs.createNode('publisher_node');
41+
const String = 'std_msgs/msg/String';
42+
const subscription = node.createSubscription(String, 'topic', (msg) => {});
43+
assert.strictEqual(typeof subscription.loggerName, 'string');
44+
});
3845
});

test/types/index.test-d.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ expectType<boolean>(publisher.waitForAllAcked(BigInt(1000)));
163163
node.createPublisher(TYPE_CLASS, TOPIC, publisher.options, (event: object) => {
164164
const receivedEvent = event;
165165
});
166+
expectType<string>(publisher.loggerName);
166167

167168
// ---- LifecyclePublisher ----
168169
const lifecyclePublisher = lifecycleNode.createLifecyclePublisher(
@@ -221,6 +222,7 @@ expectType<boolean>(subscription.isDestroyed());
221222
expectType<boolean>(subscription.setContentFilter(contentFilter));
222223
expectType<boolean>(subscription.clearContentFilter());
223224
expectType<boolean>(subscription.hasContentFilter());
225+
expectType<string>(subscription.loggerName);
224226

225227
// ---- Service ----
226228
const service = node.createService(
@@ -241,6 +243,7 @@ expectType<void>(
241243
);
242244
expectType<boolean>(service.isDestroyed());
243245
expectType<object>(service.getOptions());
246+
expectType<string>(service.loggerName);
244247

245248
// ---- Client ----
246249
const client = node.createClient(
@@ -259,6 +262,7 @@ expectType<void>(
259262
)
260263
);
261264
expectType<boolean>(client.isDestroyed());
265+
expectType<string>(client.loggerName);
262266

263267
// ---- Timer ----
264268
const timerCallback = () => {};

types/client.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ declare module 'rclnodejs' {
5252
serviceEventPubQOS: QoS,
5353
introspectionState: ServiceIntrospectionStates
5454
): void;
55+
56+
/**
57+
* Get the logger name for this client.
58+
*/
59+
readonly loggerName: string;
5560
}
5661

5762
namespace Client {

types/publisher.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,10 @@ declare module 'rclnodejs' {
3737
* @return {boolean} `true` if all published message data is acknowledged before the timeout, otherwise `false`.
3838
*/
3939
waitForAllAcked(timeout: bigint): boolean;
40+
41+
/**
42+
* Get the logger name for this publisher.
43+
*/
44+
readonly loggerName: string;
4045
}
4146
}

0 commit comments

Comments
 (0)