|
14 | 14 |
|
15 | 15 | #include <gtest/gtest.h> |
16 | 16 |
|
| 17 | +#include <cstdlib> |
17 | 18 | #include <string> |
18 | 19 | #include <vector> |
19 | 20 |
|
20 | 21 | #include "osrf_testing_tools_cpp/scope_exit.hpp" |
21 | 22 | #include "rcutils/logging.h" |
| 23 | +#include "rcutils/types/char_array.h" |
| 24 | + |
| 25 | +#ifdef _WIN32 |
| 26 | +#define test_setenv(name, value) _putenv_s(name, value) |
| 27 | +#define test_unsetenv(name) _putenv_s(name, "") |
| 28 | +#else |
| 29 | +#define test_setenv(name, value) setenv(name, value, 1) |
| 30 | +#define test_unsetenv(name) unsetenv(name) |
| 31 | +#endif |
22 | 32 |
|
23 | 33 | static void call_handler( |
24 | 34 | const rcutils_log_location_t * location, |
@@ -95,3 +105,82 @@ TEST(TestLoggingConsoleOutputHandler, bad_inputs) { |
95 | 105 | call_handler( |
96 | 106 | &log_location, RCUTILS_LOG_SEVERITY_INFO, log_name, timestamp, "bad format", "part1", "part2"); |
97 | 107 | } |
| 108 | + |
| 109 | +TEST(TestLoggingConsoleOutputHandler, short_file_name_extracts_basename) { |
| 110 | + // Set the output format to use {short_file_name} before initializing |
| 111 | + test_setenv("RCUTILS_CONSOLE_OUTPUT_FORMAT", "{short_file_name}"); |
| 112 | + OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT( |
| 113 | + { |
| 114 | + test_unsetenv("RCUTILS_CONSOLE_OUTPUT_FORMAT"); |
| 115 | + }); |
| 116 | + |
| 117 | + ASSERT_EQ(RCUTILS_RET_OK, rcutils_logging_initialize()); |
| 118 | + OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT( |
| 119 | + { |
| 120 | + EXPECT_EQ(RCUTILS_RET_OK, rcutils_logging_shutdown()); |
| 121 | + }); |
| 122 | + |
| 123 | + rcutils_allocator_t allocator = rcutils_get_default_allocator(); |
| 124 | + rcutils_char_array_t output_buf; |
| 125 | + ASSERT_EQ(RCUTILS_RET_OK, rcutils_char_array_init(&output_buf, 1024, &allocator)); |
| 126 | + OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT( |
| 127 | + { |
| 128 | + EXPECT_EQ(RCUTILS_RET_OK, rcutils_char_array_fini(&output_buf)); |
| 129 | + }); |
| 130 | + |
| 131 | + rcutils_log_location_t location = { |
| 132 | + "test_function", |
| 133 | + "/some/long/path/to/my_source_file.cpp", |
| 134 | + 42, |
| 135 | + }; |
| 136 | + |
| 137 | + ASSERT_EQ( |
| 138 | + RCUTILS_RET_OK, |
| 139 | + rcutils_logging_format_message( |
| 140 | + &location, RCUTILS_LOG_SEVERITY_INFO, "test_logger", 0, |
| 141 | + "hello", &output_buf)); |
| 142 | + |
| 143 | + std::string output(output_buf.buffer); |
| 144 | + EXPECT_NE(std::string::npos, output.find("my_source_file.cpp")) |
| 145 | + << "Expected basename in output: " << output; |
| 146 | + EXPECT_EQ(std::string::npos, output.find("/some/long/path/to/")) |
| 147 | + << "Full path should not appear in output: " << output; |
| 148 | +} |
| 149 | + |
| 150 | +TEST(TestLoggingConsoleOutputHandler, short_file_name_without_path_unchanged) { |
| 151 | + test_setenv("RCUTILS_CONSOLE_OUTPUT_FORMAT", "{short_file_name}"); |
| 152 | + OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT( |
| 153 | + { |
| 154 | + test_unsetenv("RCUTILS_CONSOLE_OUTPUT_FORMAT"); |
| 155 | + }); |
| 156 | + |
| 157 | + ASSERT_EQ(RCUTILS_RET_OK, rcutils_logging_initialize()); |
| 158 | + OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT( |
| 159 | + { |
| 160 | + EXPECT_EQ(RCUTILS_RET_OK, rcutils_logging_shutdown()); |
| 161 | + }); |
| 162 | + |
| 163 | + rcutils_allocator_t allocator = rcutils_get_default_allocator(); |
| 164 | + rcutils_char_array_t output_buf; |
| 165 | + ASSERT_EQ(RCUTILS_RET_OK, rcutils_char_array_init(&output_buf, 1024, &allocator)); |
| 166 | + OSRF_TESTING_TOOLS_CPP_SCOPE_EXIT( |
| 167 | + { |
| 168 | + EXPECT_EQ(RCUTILS_RET_OK, rcutils_char_array_fini(&output_buf)); |
| 169 | + }); |
| 170 | + |
| 171 | + rcutils_log_location_t location = { |
| 172 | + "test_function", |
| 173 | + "bare_file.cpp", |
| 174 | + 10, |
| 175 | + }; |
| 176 | + |
| 177 | + ASSERT_EQ( |
| 178 | + RCUTILS_RET_OK, |
| 179 | + rcutils_logging_format_message( |
| 180 | + &location, RCUTILS_LOG_SEVERITY_INFO, "test_logger", 0, |
| 181 | + "test", &output_buf)); |
| 182 | + |
| 183 | + std::string output(output_buf.buffer); |
| 184 | + EXPECT_NE(std::string::npos, output.find("bare_file.cpp")) |
| 185 | + << "Expected bare filename in output: " << output; |
| 186 | +} |
0 commit comments