Skip to content

Commit d3d5c2f

Browse files
authored
Add getLogger to Node object (#591)
Adds getLogger function to the Node class. Both rclcpp and rclpy provide a get_logger function on the Node class, but it was missing in rclnodejs. Actions make use of this, but it's a really a separate feature. Fix #None
1 parent 6857dd2 commit d3d5c2f

File tree

5 files changed

+47
-0
lines changed

5 files changed

+47
-0
lines changed

lib/node.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const Client = require('./client.js');
2020
const Clock = require('./clock.js');
2121
const Context = require('./context.js');
2222
const GuardCondition = require('./guard_condition.js');
23+
const Logging = require('./logging.js');
2324
const NodeOptions = require('./node_options.js');
2425
const {
2526
ParameterType,
@@ -57,6 +58,7 @@ class Node {
5758
this._parameters = new Map();
5859
this._parameterService = null;
5960
this._parameterEventPublisher = null;
61+
this._logger = new Logging(rclnodejs.getNodeLoggerName(this.handle));
6062
this.spinning = false;
6163

6264
this._parameterEventPublisher = this.createPublisher(
@@ -550,6 +552,14 @@ class Node {
550552
return rclnodejs.getNamespace(this.handle);
551553
}
552554

555+
/**
556+
* Get the nodes logger.
557+
* @returns {Logger} - The logger for the node.
558+
*/
559+
getLogger() {
560+
return this._logger;
561+
}
562+
553563
/**
554564
* Get the list of published topics discovered by the provided node for the remote node name.
555565
* @param {string} nodeName - The name of the node.

src/rcl_bindings.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1345,6 +1345,21 @@ NAN_METHOD(GetLoggerEffectiveLevel) {
13451345
info.GetReturnValue().Set(Nan::New(logger_level));
13461346
}
13471347

1348+
NAN_METHOD(GetNodeLoggerName) {
1349+
RclHandle* node_handle = RclHandle::Unwrap<RclHandle>(
1350+
Nan::To<v8::Object>(info[0]).ToLocalChecked());
1351+
rcl_node_t* node = reinterpret_cast<rcl_node_t*>(node_handle->ptr());
1352+
1353+
const char* node_logger_name = rcl_node_get_logger_name(node);
1354+
if (!node_logger_name) {
1355+
info.GetReturnValue().Set(Nan::Undefined());
1356+
return;
1357+
}
1358+
1359+
info.GetReturnValue().Set(
1360+
Nan::New<v8::String>(node_logger_name).ToLocalChecked());
1361+
}
1362+
13481363
NAN_METHOD(Log) {
13491364
v8::Local<v8::Context> currentContent = Nan::GetCurrentContext();
13501365
std::string name(
@@ -1715,6 +1730,7 @@ BindingMethod binding_methods[] = {
17151730
{"createArrayBufferCleaner", CreateArrayBufferCleaner},
17161731
{"setLoggerLevel", setLoggerLevel},
17171732
{"getLoggerEffectiveLevel", GetLoggerEffectiveLevel},
1733+
{"getNodeLoggerName", GetNodeLoggerName},
17181734
{"log", Log},
17191735
{"isEnableFor", IsEnableFor},
17201736
{"createContext", CreateContext},

test/test-node.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,16 @@ describe('rcl node methods testing', function() {
349349
});
350350
});
351351

352+
it('node.getLogger', function() {
353+
var logger = node.getLogger();
354+
assert.ok(logger);
355+
assert.equal(logger.debug('message debug'), false);
356+
assert.equal(logger.info('message info'), true);
357+
assert.equal(logger.warn('message warn'), true);
358+
assert.equal(logger.error('message error'), true);
359+
assert.equal(logger.fatal('message fatal'), true);
360+
});
361+
352362
it('node.getNodeNames', function() {
353363
var nodeNames = node.getNodeNames();
354364

test/types/main.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ node.name();
4545
// $ExpectType string
4646
node.namespace();
4747

48+
// $ExpectType Logging
49+
node.getLogger();
50+
4851
// $ExpectType void
4952
rclnodejs.spin(node);
5053

types/node.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// eslint camelcase: ["error", {ignoreImports: true}]
22

3+
import { Logging } from 'rclnodejs';
34
import { Parameters } from 'rclnodejs';
45
import { rcl_interfaces as rclinterfaces } from 'rclnodejs';
56
import { QoS } from 'rclnodejs';
@@ -157,6 +158,13 @@ declare module 'rclnodejs' {
157158
*/
158159
namespace(): string;
159160

161+
/**
162+
* Get the nodes logger.
163+
*
164+
* @returns The logger for the node.
165+
*/
166+
getLogger(): Logging;
167+
160168
/**
161169
* Get the nodeOptions provided through the constructor.
162170
*/

0 commit comments

Comments
 (0)