Skip to content

Commit 56a037a

Browse files
authored
Reapply "Add get_logging_directory method to rclcpp::Logger (ros2#1509)" (ros2#1513)
Signed-off-by: Ivan Santiago Paunovic <[email protected]>
1 parent 8d5af66 commit 56a037a

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

rclcpp/include/rclcpp/logger.hpp

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

2323
#include "rcl/node.h"
2424
#include "rcutils/logging.h"
25+
#include "rcpputils/filesystem_helper.hpp"
2526

2627
/**
2728
* \def RCLCPP_LOGGING_ENABLED
@@ -75,6 +76,18 @@ RCLCPP_PUBLIC
7576
Logger
7677
get_node_logger(const rcl_node_t * node);
7778

79+
/// Get the current logging directory.
80+
/**
81+
* For more details of how the logging directory is determined,
82+
* see \ref rcl_logging_get_logging_directory.
83+
*
84+
* \returns the logging directory being used.
85+
* \throws rclcpp::exceptions::RCLError if an unexpected error occurs.
86+
*/
87+
RCLCPP_PUBLIC
88+
rcpputils::fs::path
89+
get_logging_directory();
90+
7891
class Logger
7992
{
8093
public:

rclcpp/src/rclcpp/logger.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
#include <string>
1616

17+
#include "rcl_logging_interface/rcl_logging_interface.h"
18+
1719
#include "rclcpp/exceptions.hpp"
1820
#include "rclcpp/logger.hpp"
1921
#include "rclcpp/logging.hpp"
@@ -46,6 +48,20 @@ get_node_logger(const rcl_node_t * node)
4648
return rclcpp::get_logger(logger_name);
4749
}
4850

51+
rcpputils::fs::path
52+
get_logging_directory()
53+
{
54+
char * log_dir = NULL;
55+
auto allocator = rcutils_get_default_allocator();
56+
rcl_logging_ret_t ret = rcl_logging_get_logging_directory(allocator, &log_dir);
57+
if (RCL_LOGGING_RET_OK != ret) {
58+
rclcpp::exceptions::throw_from_rcl_error(ret);
59+
}
60+
std::string path{log_dir};
61+
allocator.deallocate(log_dir, allocator.state);
62+
return path;
63+
}
64+
4965
void
5066
Logger::set_level(Level level)
5167
{

rclcpp/test/rclcpp/test_logger.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#include <memory>
1818
#include <string>
1919

20+
#include "rcutils/env.h"
21+
2022
#include "rclcpp/logger.hpp"
2123
#include "rclcpp/logging.hpp"
2224
#include "rclcpp/node.hpp"
@@ -157,3 +159,22 @@ TEST(TestLogger, set_level) {
157159
rcutils_logging_set_output_handler(previous_output_handler);
158160
EXPECT_EQ(RCUTILS_RET_OK, rcutils_logging_shutdown());
159161
}
162+
163+
TEST(TestLogger, get_logging_directory) {
164+
ASSERT_EQ(true, rcutils_set_env("HOME", "/fake_home_dir"));
165+
ASSERT_EQ(true, rcutils_set_env("USERPROFILE", nullptr));
166+
ASSERT_EQ(true, rcutils_set_env("ROS_LOG_DIR", nullptr));
167+
ASSERT_EQ(true, rcutils_set_env("ROS_HOME", nullptr));
168+
169+
auto path = rclcpp::get_logging_directory();
170+
auto expected_path = rcpputils::fs::path{"/fake_home_dir"} / ".ros" / "log";
171+
172+
// TODO(ivanpauno): Add operator== to rcpputils::fs::path
173+
auto it = path.cbegin();
174+
auto eit = expected_path.cbegin();
175+
for (; it != path.cend() && eit != expected_path.cend(); ++it, ++eit) {
176+
EXPECT_EQ(*eit, *it);
177+
}
178+
EXPECT_EQ(it, path.cend());
179+
EXPECT_EQ(eit, expected_path.cend());
180+
}

0 commit comments

Comments
 (0)