Skip to content

Commit 0f52003

Browse files
authored
Add getLoggingDirectory() (#1337)
This PR adds a new `getLoggingDirectory()` static method to the Logging class that retrieves the directory path where ROS 2 logging files are stored. - Adds C++ binding for `rcl_logging_get_logging_directory` from the rcl_logging_interface library - Exposes the functionality through JavaScript and TypeScript APIs - Includes test coverage for both runtime behavior and type checking Fix: #1330
1 parent 2221b4a commit 0f52003

File tree

7 files changed

+47
-0
lines changed

7 files changed

+47
-0
lines changed

binding.gyp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
'-lrcl_action',
6868
'-lrcl_lifecycle',
6969
'-lrcutils',
70+
'-lrcl_logging_interface',
7071
'-lrcl_yaml_param_parser',
7172
'-lrcpputils',
7273
'-lrmw',

lib/logging.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,15 @@ class Logging {
217217
}
218218
return new Logging(name);
219219
}
220+
221+
/**
222+
* Get the logging directory.
223+
* @function
224+
* @return {string} - The logging directory.
225+
*/
226+
static getLoggingDirectory() {
227+
return rclnodejs.getLoggingDirectory();
228+
}
220229
}
221230

222231
module.exports = Logging;

scripts/config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ const dependencies = [
3333
'rosidl_runtime_c',
3434
'rosidl_dynamic_typesupport',
3535
'type_description_interfaces',
36+
'rcl_logging_interface',
3637
];
3738

3839
const command = os.type() === 'Windows_NT' ? 'where ros2' : 'which ros2';

src/rcl_logging_bindings.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include <rcl/error_handling.h>
1818
#include <rcl/rcl.h>
19+
#include <rcl_logging_interface/rcl_logging_interface.h>
1920

2021
#include <string>
2122

@@ -84,12 +85,33 @@ Napi::Value IsEnableFor(const Napi::CallbackInfo& info) {
8485
return Napi::Boolean::New(env, enabled);
8586
}
8687

88+
Napi::Value GetLoggingDirectory(const Napi::CallbackInfo& info) {
89+
Napi::Env env = info.Env();
90+
rcutils_allocator_t allocator = rcutils_get_default_allocator();
91+
char* directory_path = nullptr;
92+
rcl_logging_ret_t ret =
93+
rcl_logging_get_logging_directory(allocator, &directory_path);
94+
95+
if (ret != RCL_LOGGING_RET_OK) {
96+
Napi::Error::New(env, rcutils_get_error_string().str)
97+
.ThrowAsJavaScriptException();
98+
rcutils_reset_error();
99+
return env.Undefined();
100+
}
101+
102+
Napi::String result = Napi::String::New(env, directory_path);
103+
allocator.deallocate(directory_path, allocator.state);
104+
return result;
105+
}
106+
87107
Napi::Object InitLoggingBindings(Napi::Env env, Napi::Object exports) {
88108
exports.Set("setLoggerLevel", Napi::Function::New(env, setLoggerLevel));
89109
exports.Set("getLoggerEffectiveLevel",
90110
Napi::Function::New(env, GetLoggerEffectiveLevel));
91111
exports.Set("log", Napi::Function::New(env, Log));
92112
exports.Set("isEnableFor", Napi::Function::New(env, IsEnableFor));
113+
exports.Set("getLoggingDirectory",
114+
Napi::Function::New(env, GetLoggingDirectory));
93115
return exports;
94116
}
95117

test/test-logging.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,4 +83,10 @@ describe('Test logging util', function () {
8383
);
8484
rclnodejs.shutdown();
8585
});
86+
87+
it('Test getLoggingDirectory', function () {
88+
const logDir = rclnodejs.logging.getLoggingDirectory();
89+
assert.strictEqual(typeof logDir, 'string');
90+
assert.ok(logDir.length > 0);
91+
});
8692
});

test/types/index.test-d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,7 @@ expectType<boolean>(logger.debug('test msg'));
353353
expectType<boolean>(logger.warn('test msg'));
354354
expectType<boolean>(logger.error('test msg'));
355355
expectType<boolean>(logger.fatal('test msg'));
356+
expectType<string>(rclnodejs.Logging.getLoggingDirectory());
356357

357358
// ---- ActionClient -----
358359
const Fibonacci = rclnodejs.require('example_interfaces/action/Fibonacci');

types/logging.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,13 @@ declare module 'rclnodejs' {
8484
* @returns New logger.
8585
*/
8686
static getLogger(name: string): Logging;
87+
88+
/**
89+
* Get the logging directory.
90+
*
91+
* @returns The logging directory.
92+
*/
93+
static getLoggingDirectory(): string;
8794
}
8895

8996
namespace Logging {

0 commit comments

Comments
 (0)