Skip to content

Commit 75a80b8

Browse files
committed
feat(agnocastlib, test): add --disable-stdout-logs support and corresponidng test
Signed-off-by: PeterWrighten <peterwrighten@gmail.com>
1 parent 78e665d commit 75a80b8

4 files changed

Lines changed: 107 additions & 4 deletions

File tree

docs/agnocast_node_interface_comparison.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,9 +200,9 @@ This provides the same argument parsing functionality as rcl.
200200
| `--` (end marker) || **Full Support** | - | ROS arguments end marker |
201201
| `-r node:old:=new` || **Full Support** | - | Node-specific remapping |
202202
| `--log-level` || **Full Support** | - | Set log level |
203-
| `--enable-rosout-logs` | | **Unsupported** | TBD | Enable logging to rosout |
204-
| `--disable-external-lib-logs` | | **Unsupported** | TBD | Disable external library logs |
205-
| `--disable-stdout-logs` | | **Unsupported** | TBD | Disable stdout logging |
203+
| `--enable-rosout-logs` | - | **Supportable** | TBD | Enable logging to rosout |
204+
| `--disable-external-lib-logs` | - | **Default** | - | Disable external library logs |
205+
| `--disable-stdout-logs` | | **Full Support** | - | Disable stdout logging |
206206
| `-e` (enclave) || **Unsupported** | TBD | Specify security enclave |
207207

208208
### 3.2 Parameter Override Resolution

src/agnocastlib/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,8 @@ if(BUILD_TESTING)
131131
test/unit/test_mocked_agnocast.cpp
132132
test/unit/test_agnocast_executors.cpp
133133
test/unit/test_node_topics.cpp
134-
test/unit/test_cie_client_utils.cpp)
134+
test/unit/test_cie_client_utils.cpp
135+
test/unit/test_agnocast_context.cpp)
135136
target_include_directories(test_unit_${PROJECT_NAME} PRIVATE include)
136137
target_link_libraries(test_unit_${PROJECT_NAME} agnocast)
137138
ament_target_dependencies(test_unit_${PROJECT_NAME} std_msgs)

src/agnocastlib/src/node/agnocast_context.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "agnocast/node/agnocast_context.hpp"
2+
#include <rcl/arguments.h>
23
#include <rcl/log_level.h>
34
#include <rcutils/logging.h>
45

@@ -10,6 +11,12 @@ namespace agnocast
1011
Context g_context;
1112
std::mutex g_context_mtx;
1213

14+
static void noop_log_output_handler(
15+
const rcutils_log_location_t *, int, const char *, rcutils_time_point_value_t, const char *,
16+
va_list *)
17+
{
18+
}
19+
1320
void Context::init(int argc, char const * const * argv)
1421
{
1522
if (initialized_) {
@@ -41,6 +48,22 @@ void Context::init(int argc, char const * const * argv)
4148
rcl_reset_error();
4249
}
4350

51+
// Apply --disable-stdout-logs if present within --ros-args scope.
52+
// There is no public rcl API to extract this flag from rcl_arguments_t, so we scan argv directly.
53+
// rcl_logging_configure() cannot be used as it would initialize spdlog and attempt to set up a
54+
// rosout publisher (which requires rcl_node_t), neither of which exist in agnocast.
55+
bool in_ros_args = false;
56+
for (const auto & arg : args) {
57+
if (arg == "--ros-args") {
58+
in_ros_args = true;
59+
} else if (arg == "--") {
60+
in_ros_args = false;
61+
} else if (in_ros_args && arg == "--disable-stdout-logs") {
62+
rcutils_logging_set_output_handler(noop_log_output_handler);
63+
break;
64+
}
65+
}
66+
4467
initialized_ = true;
4568
}
4669

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#include "agnocast/node/agnocast_context.hpp"
2+
3+
#include <rcutils/logging.h>
4+
5+
#include <gtest/gtest.h>
6+
7+
// =========================================
8+
// agnocast::Context --disable-stdout-logs tests
9+
// =========================================
10+
11+
class AgnocastContextStdoutLogsTest : public ::testing::Test
12+
{
13+
protected:
14+
void SetUp() override { original_handler_ = rcutils_logging_get_output_handler(); }
15+
16+
void TearDown() override
17+
{
18+
// Restore the original handler so other tests are not affected.
19+
rcutils_logging_set_output_handler(original_handler_);
20+
}
21+
22+
rcutils_logging_output_handler_t original_handler_;
23+
};
24+
25+
TEST_F(AgnocastContextStdoutLogsTest, disable_stdout_logs_changes_handler)
26+
{
27+
// Arrange
28+
const char * argv[] = {"program", "--ros-args", "--disable-stdout-logs"};
29+
int argc = 3;
30+
agnocast::Context ctx;
31+
32+
// Act
33+
ctx.init(argc, argv);
34+
35+
// Assert: output handler must have been replaced with a no-op
36+
EXPECT_NE(rcutils_logging_get_output_handler(), original_handler_);
37+
}
38+
39+
TEST_F(AgnocastContextStdoutLogsTest, no_flag_keeps_handler_unchanged)
40+
{
41+
// Arrange
42+
const char * argv[] = {"program", "--ros-args", "--log-level", "info"};
43+
int argc = 4;
44+
agnocast::Context ctx;
45+
46+
// Act
47+
ctx.init(argc, argv);
48+
49+
// Assert: output handler must not have changed
50+
EXPECT_EQ(rcutils_logging_get_output_handler(), original_handler_);
51+
}
52+
53+
TEST_F(AgnocastContextStdoutLogsTest, flag_outside_ros_args_is_ignored)
54+
{
55+
// Arrange: --disable-stdout-logs appears before --ros-args, so it is not a ROS argument
56+
const char * argv[] = {"program", "--disable-stdout-logs", "--ros-args", "--log-level", "info"};
57+
int argc = 5;
58+
agnocast::Context ctx;
59+
60+
// Act
61+
ctx.init(argc, argv);
62+
63+
// Assert: flag outside --ros-args scope must be ignored
64+
EXPECT_EQ(rcutils_logging_get_output_handler(), original_handler_);
65+
}
66+
67+
TEST_F(AgnocastContextStdoutLogsTest, flag_after_double_dash_terminator_is_ignored)
68+
{
69+
// Arrange: --disable-stdout-logs appears after -- (end of ROS args), so it is not a ROS argument
70+
const char * argv[] = {"program", "--ros-args", "--", "--disable-stdout-logs"};
71+
int argc = 4;
72+
agnocast::Context ctx;
73+
74+
// Act
75+
ctx.init(argc, argv);
76+
77+
// Assert: flag after -- must be ignored
78+
EXPECT_EQ(rcutils_logging_get_output_handler(), original_handler_);
79+
}

0 commit comments

Comments
 (0)