ROS2日志如何打印时分秒格式时间
-
这是一个来自鱼香ROS社区的问题,问题原文以及小鱼提供的解决方案如下:
问题: ros2日志打印具体时间而不是时间戳
ros2默认的打印接口get_logger打印输出的内容中时间是以时间戳格式打印,如图:
查看官方文档似乎只有时间戳time的选项,没有其他时间格式
有没有办法让其改为‘年-月-日 时:分:秒’的格式以方便系统维护和故障排查?
小鱼提供的解决方案:
没猜错你想要的应该是下面这种效果
ros2 run demo_nodes_cpp talker --- [INFO] [12:34:26.755] [talker]: Publishing: 'Hello World: 1' [INFO] [12:34:27.755] [talker]: Publishing: 'Hello World: 2' [INFO] [12:34:28.755] [talker]: Publishing: 'Hello World: 3' [INFO] [12:34:29.755] [talker]: Publishing: 'Hello World: 4' [INFO] [12:34:30.755] [talker]: Publishing: 'Hello World: 5' [INFO] [12:34:31.755] [talker]: Publishing: 'Hello World: 6' [INFO] [12:34:32.755] [talker]: Publishing: 'Hello World: 7' [INFO] [12:34:33.755] [talker]: Publishing: 'Hello World: 8'
我看了下,目前来说没有一个好的原生方案,不过可以手动改源码实现。
首先下载 rcutils 源码,并安装依赖:
git clone https://github.com/ros2/rcutils.git -b humble sudo apt install ros-humble-mimick-vendor -y
打开文件:rcutils/include/rcutils/time.h ,添加代码
RCUTILS_PUBLIC RCUTILS_WARN_UNUSED rcutils_ret_t rcutils_time_point_value_as_hmsms_string( const rcutils_time_point_value_t *time_point, char *str, size_t str_size);
打开文件:rcutils/src/time.c,添加代码
rcutils_ret_t rcutils_time_point_value_as_hmsms_string( const rcutils_time_point_value_t * time_point, char * str, size_t str_size) { RCUTILS_CHECK_ARGUMENT_FOR_NULL(time_point, RCUTILS_RET_INVALID_ARGUMENT); RCUTILS_CHECK_ARGUMENT_FOR_NULL(str, RCUTILS_RET_INVALID_ARGUMENT); if (0 == str_size) { return RCUTILS_RET_OK; } // best to abs it to avoid issues with negative values in C89, see: // https://stackoverflow.com/a/3604984/671658 uint64_t abs_time_point = (uint64_t)llabs(*time_point); // Calculate hours, minutes, seconds, and milliseconds uint64_t milliseconds = abs_time_point / 1000000u; // Convert nanoseconds to milliseconds uint64_t seconds = milliseconds / 1000u; uint64_t minutes = seconds / 60u; uint64_t hours = minutes / 60u; milliseconds %= 1000u; seconds %= 60u; minutes %= 60u; hours %= 24u; if (rcutils_snprintf( str, str_size, "%s%02" PRId64 ":%02" PRId64 ":%02" PRId64 ".%03" PRId64, (*time_point >= 0) ? "" : "->", hours, minutes, seconds, milliseconds) < 0) { RCUTILS_SET_ERROR_MSG("failed to format time point into string"); return RCUTILS_RET_ERROR; } return RCUTILS_RET_OK; }
打开文件: rcutils/src/logging.c ,修改代码:
// 修改前: const char * expand_time_as_seconds( const logging_input * logging_input, rcutils_char_array_t * logging_output) { return expand_time(logging_input, logging_output, rcutils_time_point_value_as_seconds_string); } // 修改后: const char * expand_time_as_seconds( const logging_input * logging_input, rcutils_char_array_t * logging_output) { return expand_time(logging_input, logging_output, rcutils_time_point_value_as_hmsms_string); }
编译代码:
colcon build
然后source 后运行任意日志打印就可以了,也可以直接替换系统库里的头文件和库
-
小鱼你好,这样改的话,now函数得到的时间戳也会是时分秒的格式吗?
-
@2681126835 不会,只会影响打印
-
@小鱼 在 ROS2日志如何打印时分秒格式时间 中说:
colcon build
小鱼你好,我按照这个步骤走下去在编译的时候报了这个错
--- stderr: rcutils CMake Error at CMakeLists.txt:182 (find_package): By not providing "Findosrf_testing_tools_cpp.cmake" in CMAKE_MODULE_PATH this project has asked CMake to find a package configuration file provided by "osrf_testing_tools_cpp", but CMake did not find one. Could not find a package configuration file provided by "osrf_testing_tools_cpp" with any of the following names: osrf_testing_tools_cppConfig.cmake osrf_testing_tools_cpp-config.cmake Add the installation prefix of "osrf_testing_tools_cpp" to CMAKE_PREFIX_PATH or set "osrf_testing_tools_cpp_DIR" to a directory containing one of the above files. If "osrf_testing_tools_cpp" provides a separate development package or SDK, be sure it has been installed. --- Failed <<< rcutils [1.10s, exited with code 1]
请问下怎么解决
环境:
Ubuntu 20.04
ROS2 foxy -
@2681126835 你看看你安装源码的第一步是不是没改版本,放弃所有更改,切换到foxy分支就可以了
-
@2681126835 我也遇到了相同的问题,环境和你一样。使用如下命令解决问题
sudo apt install ros-humble-performance-test-fixture