Skip to content

Commit

Permalink
Add getLogger to Node object (#591)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
mattrichard authored Mar 1, 2020
1 parent 6857dd2 commit d3d5c2f
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const Client = require('./client.js');
const Clock = require('./clock.js');
const Context = require('./context.js');
const GuardCondition = require('./guard_condition.js');
const Logging = require('./logging.js');
const NodeOptions = require('./node_options.js');
const {
ParameterType,
Expand Down Expand Up @@ -57,6 +58,7 @@ class Node {
this._parameters = new Map();
this._parameterService = null;
this._parameterEventPublisher = null;
this._logger = new Logging(rclnodejs.getNodeLoggerName(this.handle));
this.spinning = false;

this._parameterEventPublisher = this.createPublisher(
Expand Down Expand Up @@ -550,6 +552,14 @@ class Node {
return rclnodejs.getNamespace(this.handle);
}

/**
* Get the nodes logger.
* @returns {Logger} - The logger for the node.
*/
getLogger() {
return this._logger;
}

/**
* Get the list of published topics discovered by the provided node for the remote node name.
* @param {string} nodeName - The name of the node.
Expand Down
16 changes: 16 additions & 0 deletions src/rcl_bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1345,6 +1345,21 @@ NAN_METHOD(GetLoggerEffectiveLevel) {
info.GetReturnValue().Set(Nan::New(logger_level));
}

NAN_METHOD(GetNodeLoggerName) {
RclHandle* node_handle = RclHandle::Unwrap<RclHandle>(
Nan::To<v8::Object>(info[0]).ToLocalChecked());
rcl_node_t* node = reinterpret_cast<rcl_node_t*>(node_handle->ptr());

const char* node_logger_name = rcl_node_get_logger_name(node);
if (!node_logger_name) {
info.GetReturnValue().Set(Nan::Undefined());
return;
}

info.GetReturnValue().Set(
Nan::New<v8::String>(node_logger_name).ToLocalChecked());
}

NAN_METHOD(Log) {
v8::Local<v8::Context> currentContent = Nan::GetCurrentContext();
std::string name(
Expand Down Expand Up @@ -1715,6 +1730,7 @@ BindingMethod binding_methods[] = {
{"createArrayBufferCleaner", CreateArrayBufferCleaner},
{"setLoggerLevel", setLoggerLevel},
{"getLoggerEffectiveLevel", GetLoggerEffectiveLevel},
{"getNodeLoggerName", GetNodeLoggerName},
{"log", Log},
{"isEnableFor", IsEnableFor},
{"createContext", CreateContext},
Expand Down
10 changes: 10 additions & 0 deletions test/test-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,16 @@ describe('rcl node methods testing', function() {
});
});

it('node.getLogger', function() {
var logger = node.getLogger();
assert.ok(logger);
assert.equal(logger.debug('message debug'), false);
assert.equal(logger.info('message info'), true);
assert.equal(logger.warn('message warn'), true);
assert.equal(logger.error('message error'), true);
assert.equal(logger.fatal('message fatal'), true);
});

it('node.getNodeNames', function() {
var nodeNames = node.getNodeNames();

Expand Down
3 changes: 3 additions & 0 deletions test/types/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ node.name();
// $ExpectType string
node.namespace();

// $ExpectType Logging
node.getLogger();

// $ExpectType void
rclnodejs.spin(node);

Expand Down
8 changes: 8 additions & 0 deletions types/node.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// eslint camelcase: ["error", {ignoreImports: true}]

import { Logging } from 'rclnodejs';
import { Parameters } from 'rclnodejs';
import { rcl_interfaces as rclinterfaces } from 'rclnodejs';
import { QoS } from 'rclnodejs';
Expand Down Expand Up @@ -157,6 +158,13 @@ declare module 'rclnodejs' {
*/
namespace(): string;

/**
* Get the nodes logger.
*
* @returns The logger for the node.
*/
getLogger(): Logging;

/**
* Get the nodeOptions provided through the constructor.
*/
Expand Down

0 comments on commit d3d5c2f

Please sign in to comment.