From 1fd7e56f4ce46f96142fc277a5e0904b78a6c33c Mon Sep 17 00:00:00 2001 From: Christophe Bedard Date: Wed, 28 Feb 2024 15:00:03 -0800 Subject: [PATCH 1/2] Improve error message handling for spdlog Signed-off-by: Christophe Bedard --- rcl_logging_spdlog/src/rcl_logging_spdlog.cpp | 2 +- rcl_logging_spdlog/test/test_logging_interface.cpp | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/rcl_logging_spdlog/src/rcl_logging_spdlog.cpp b/rcl_logging_spdlog/src/rcl_logging_spdlog.cpp index af94684..8f7b4b8 100644 --- a/rcl_logging_spdlog/src/rcl_logging_spdlog.cpp +++ b/rcl_logging_spdlog/src/rcl_logging_spdlog.cpp @@ -133,7 +133,7 @@ rcl_logging_ret_t rcl_logging_external_initialize( if (RCL_LOGGING_RET_OK != dir_ret) { // We couldn't get the log directory, so get out of here without setting up // logging. - RCUTILS_SET_ERROR_MSG("Failed to get logging directory"); + RCUTILS_SET_ERROR_MSG_AND_APPEND_PREV_ERROR("Failed to get logging directory"); return dir_ret; } RCPPUTILS_SCOPE_EXIT( diff --git a/rcl_logging_spdlog/test/test_logging_interface.cpp b/rcl_logging_spdlog/test/test_logging_interface.cpp index 71bd01f..fb02634 100644 --- a/rcl_logging_spdlog/test/test_logging_interface.cpp +++ b/rcl_logging_spdlog/test/test_logging_interface.cpp @@ -68,14 +68,17 @@ TEST_F(LoggingTest, init_invalid) EXPECT_EQ( RCL_LOGGING_RET_ERROR, rcl_logging_external_initialize(nullptr, "anything", allocator)); + EXPECT_TRUE(rcutils_error_is_set()); rcutils_reset_error(); EXPECT_EQ( RCL_LOGGING_RET_ERROR, rcl_logging_external_initialize("anything", nullptr, bad_allocator)); + EXPECT_TRUE(rcutils_error_is_set()); rcutils_reset_error(); EXPECT_EQ( RCL_LOGGING_RET_ERROR, rcl_logging_external_initialize(nullptr, nullptr, invalid_allocator)); + EXPECT_TRUE(rcutils_error_is_set()); rcutils_reset_error(); } @@ -88,6 +91,7 @@ TEST_F(LoggingTest, init_failure) ASSERT_EQ(true, rcutils_set_env("HOME", nullptr)); ASSERT_EQ(true, rcutils_set_env("USERPROFILE", nullptr)); EXPECT_EQ(RCL_LOGGING_RET_ERROR, rcl_logging_external_initialize(nullptr, nullptr, allocator)); + EXPECT_TRUE(rcutils_error_is_set()); rcutils_reset_error(); // Force failure to create directories @@ -99,6 +103,8 @@ TEST_F(LoggingTest, init_failure) std::filesystem::path ros_dir = fake_home / ".ros"; std::fstream(ros_dir.string(), std::ios_base::out).close(); EXPECT_EQ(RCL_LOGGING_RET_ERROR, rcl_logging_external_initialize(nullptr, nullptr, allocator)); + EXPECT_TRUE(rcutils_error_is_set()); + rcutils_reset_error(); ASSERT_TRUE(std::filesystem::remove(ros_dir)); // ...fail to create .ros/log dir @@ -106,6 +112,8 @@ TEST_F(LoggingTest, init_failure) std::filesystem::path ros_log_dir = ros_dir / "log"; std::fstream(ros_log_dir.string(), std::ios_base::out).close(); EXPECT_EQ(RCL_LOGGING_RET_ERROR, rcl_logging_external_initialize(nullptr, nullptr, allocator)); + EXPECT_TRUE(rcutils_error_is_set()); + rcutils_reset_error(); ASSERT_TRUE(std::filesystem::remove(ros_log_dir)); ASSERT_TRUE(std::filesystem::remove(ros_dir)); From 14808a7f8fc5a187df0f1ac1040aad8c03a3039d Mon Sep 17 00:00:00 2001 From: Christophe Bedard Date: Wed, 28 Feb 2024 15:11:41 -0800 Subject: [PATCH 2/2] Mention error msg in rcl_logging_external_initialize docs Signed-off-by: Christophe Bedard --- .../include/rcl_logging_interface/rcl_logging_interface.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/rcl_logging_interface/include/rcl_logging_interface/rcl_logging_interface.h b/rcl_logging_interface/include/rcl_logging_interface/rcl_logging_interface.h index 11c1782..3498dc2 100644 --- a/rcl_logging_interface/include/rcl_logging_interface/rcl_logging_interface.h +++ b/rcl_logging_interface/include/rcl_logging_interface/rcl_logging_interface.h @@ -45,7 +45,8 @@ typedef enum * an rcutils_allocator_t rather than an rcl_allocator_t to ensure that the * rcl_logging_* packages don't have a circular dependency back to rcl. * \return RCL_LOGGING_RET_OK if initialized successfully. - * \return RCL_LOGGING_RET_ERROR if an unspecified error occurs. + * \return RCL_LOGGING_RET_ERROR if an unspecified error occurs. An rcutils error message will be + * set. * \return RCL_LOGGING_RET_CONFIG_FILE_DOESNT_EXIST if a config_file is provided * but the file doesn't exist. * \return RCL_LOGGING_RET_CONFIG_FILE_INVALID if a config_file is provided but