Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve error message handling for spdlog #110

Draft
wants to merge 2 commits into
base: rolling
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion rcl_logging_spdlog/src/rcl_logging_spdlog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
8 changes: 8 additions & 0 deletions rcl_logging_spdlog/test/test_logging_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand All @@ -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
Expand All @@ -99,13 +103,17 @@ 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
ASSERT_TRUE(std::filesystem::create_directories(ros_dir));
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));

Expand Down