Skip to content

Commit 817a2f1

Browse files
authored
feat(agnocastlib): enable file logging for agnocast::Node (#1204)
* enable file logging for agnocast::Node Signed-off-by: Koichi Imai <koichi.imai.2@tier4.jp> * fix to use parsed_arguments Signed-off-by: Koichi Imai <koichi.imai.2@tier4.jp> * fix to use rcl_get_default_allocator Signed-off-by: Koichi Imai <koichi.imai.2@tier4.jp> * update agnocast_node_interface_comparison.md Signed-off-by: Koichi Imai <koichi.imai.2@tier4.jp> * add shutdown and move initialized_=true Signed-off-by: Koichi Imai <koichi.imai.2@tier4.jp> * fixed comments Signed-off-by: Koichi Imai <koichi.imai.2@tier4.jp> * check initialized when shutdown Signed-off-by: Koichi Imai <koichi.imai.2@tier4.jp> --------- Signed-off-by: Koichi Imai <koichi.imai.2@tier4.jp>
1 parent fe20f77 commit 817a2f1

9 files changed

Lines changed: 50 additions & 11 deletions

File tree

docs/agnocast_node_interface_comparison.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ This provides the same argument parsing functionality as rcl.
201201
| `-r node:old:=new` || **Full Support** | - | Node-specific remapping |
202202
| `--log-level` || **Unsupported** | TBD | Set log level |
203203
| `--enable-rosout-logs` || **Unsupported** | TBD | Enable logging to rosout |
204-
| `--disable-external-lib-logs` | | **Unsupported** | TBD | Disable external library logs |
204+
| `--disable-external-lib-logs` | | **Full Support** | - | Disable external library logs (file logging via rcl_logging_spdlog) |
205205
| `--disable-stdout-logs` || **Unsupported** | TBD | Disable stdout logging |
206206
| `-e` (enclave) || **Unsupported** | TBD | Specify security enclave |
207207

@@ -394,6 +394,8 @@ agnocast::Node uses the following rcl/rclcpp functions, data structures, and cla
394394
- `rcl_arguments_get_param_overrides()` - Get parameter overrides
395395
- `rcl_arguments_fini()` - Argument cleanup
396396
- `rcl_get_default_allocator()` - Get default allocator
397+
- `rcl_logging_configure_with_output_handler()` - Configure rcl logging with file output via rcl_logging_spdlog
398+
- `rcl_logging_multiple_output_handler()` - Default output handler that dispatches to stdout and external lib (spdlog)
397399
- `rcl_enable_ros_time_override()` - Enable ROS time override for simulated time
398400
- `rcl_disable_ros_time_override()` - Disable ROS time override
399401
- `rcl_set_ros_time_override()` - Set ROS time override value

src/agnocast_components/src/node_main_agnocast_only.cpp.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,6 @@ int main(int argc, char * argv[])
7272
return 1;
7373
}
7474

75-
// TODO(Koichi98): Add agnocast::shutdown() here
75+
agnocast::shutdown();
7676
return 0;
7777
}

src/agnocast_sample_application/src/no_rclcpp_client.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,6 @@ int main(int argc, char * argv[])
4343
RCLCPP_INFO(node->get_logger(), "Result2: %ld", future.get()->sum);
4444

4545
spin_thread.join();
46-
// TODO(Koichi98): Add agnocast::shutdown() here
46+
agnocast::shutdown();
4747
return 0;
4848
}

src/agnocast_sample_application/src/no_rclcpp_server.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@ int main(int argc, char * argv[])
3232
auto node = std::make_shared<NoRclcppServer>();
3333
executor.add_node(node);
3434
executor.spin();
35-
// TODO(Koichi98): Add agnocast::shutdown() here
35+
agnocast::shutdown();
3636
return 0;
3737
}

src/agnocastlib/include/agnocast/node/agnocast_context.hpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,16 @@ class Context
3535
extern Context g_context;
3636
extern std::mutex g_context_mtx;
3737

38-
/// @brief Initialize Agnocast for Stage 2 (agnocast::Node). Must be called once before creating
39-
/// any agnocast::Node. This is the Stage 2 equivalent of rclcpp::init().
38+
/// @brief Initialize Agnocast. Must be called once before creating any agnocast::Node.
39+
/// This is the counterpart of rclcpp::init() for agnocast::Node.
4040
/// @param argc Number of command-line arguments.
4141
/// @param argv Command-line argument array.
4242
AGNOCAST_PUBLIC
4343
void init(int argc, char const * const * argv);
4444

45+
/// @brief Shut down Agnocast. Should be called before process exit in agnocast::Node processes.
46+
/// This is the counterpart of rclcpp::shutdown() for agnocast::Node.
47+
AGNOCAST_PUBLIC
48+
void shutdown();
49+
4550
} // namespace agnocast

src/agnocastlib/src/agnocast_client.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ bool wait_for_service_nanoseconds(
6161
nanoseconds time_to_wait =
6262
timeout > nanoseconds(0) ? timeout - (steady_clock::now() - start) : nanoseconds::max();
6363
do {
64-
// TODO(Koichi98): agnocast::ok and agnocast::shutdown are planned to be implemented.
64+
// TODO(Koichi98): agnocast::ok is planned to be implemented.
6565
// For standalone agnocast nodes (without rclcpp::init()), context->is_valid() returns false,
6666
// so we skip the rclcpp::ok() check in that case.
6767
if ((context && context->is_valid()) ? !rclcpp::ok(context) : false) {

src/agnocastlib/src/node/agnocast_context.cpp

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
#include "agnocast/agnocast_tracepoint_wrapper.h"
44

5+
#include <rcl/error_handling.h>
6+
#include <rcl/logging.h>
7+
#include <rcutils/logging_macros.h>
8+
59
namespace agnocast
610
{
711

@@ -22,8 +26,19 @@ void Context::init(int argc, char const * const * argv)
2226
}
2327

2428
parsed_arguments_ = parse_arguments(args);
25-
initialized_ = true;
2629

30+
// Initialize rcl logging so that RCLCPP_INFO/WARN/etc. are written to
31+
// ~/.ros/log/ files via rcl_logging_spdlog, matching rclcpp::init() behavior.
32+
rcl_allocator_t allocator = rcl_get_default_allocator();
33+
rcl_ret_t ret = rcl_logging_configure_with_output_handler(
34+
parsed_arguments_.get(), &allocator, rcl_logging_multiple_output_handler);
35+
if (ret != RCL_RET_OK) {
36+
RCUTILS_LOG_ERROR_NAMED(
37+
"agnocast", "Failed to configure logging: %s", rcl_get_error_string().str);
38+
rcl_reset_error();
39+
}
40+
41+
initialized_ = true;
2742
TRACEPOINT(agnocast_init, static_cast<const void *>(this));
2843
}
2944

@@ -33,4 +48,22 @@ void init(int argc, char const * const * argv)
3348
g_context.init(argc, argv);
3449
}
3550

51+
void shutdown()
52+
{
53+
std::lock_guard<std::mutex> lock(g_context_mtx);
54+
if (!g_context.is_initialized()) {
55+
return;
56+
}
57+
58+
// TODO(Koichi98): Add SignalHandler cleanup (uninstall signal handlers, reset state)
59+
// and notify all executors to stop spinning via SignalHandler::notify_all_executors().
60+
61+
rcl_ret_t ret = rcl_logging_fini();
62+
if (ret != RCL_RET_OK) {
63+
RCUTILS_LOG_ERROR_NAMED(
64+
"agnocast", "Failed to finalize logging: %s", rcl_get_error_string().str);
65+
rcl_reset_error();
66+
}
67+
}
68+
3669
} // namespace agnocast

src/agnocastlib/test/integration/message_filters/test_subscriber.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,8 +268,7 @@ class AgnocastNodeSubscriberTest : public ::testing::Test
268268
}
269269
executor_.reset();
270270
node_.reset();
271-
// TODO(Koichi98): Call agnocast::shutdown() once available.
272-
// See https://github.com/autowarefoundation/agnocast/issues/1019
271+
agnocast::shutdown();
273272
}
274273

275274
void waitFor(

src/agnocastlib/test/integration/test_agnocast_only_callback_isolated_executor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class AgnocastOnlyCallbackIsolatedExecutorTest : public ::testing::Test
7878
void TearDown() override
7979
{
8080
rclcpp::shutdown();
81-
// TODO(Koichi98): Call agnocast::shutdown() once available.
81+
agnocast::shutdown();
8282
}
8383
};
8484

0 commit comments

Comments
 (0)