-
Notifications
You must be signed in to change notification settings - Fork 113
Adds {severity_with_color} as a log format option. #526
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
base: rolling
Are you sure you want to change the base?
Changes from 4 commits
b7a4469
7eca440
a728158
8674a60
2aaaf97
577636d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -381,6 +381,12 @@ static const char * expand_file_name( | |||||
| return logging_output->buffer; | ||||||
| } | ||||||
|
|
||||||
| // Forward declare expand_serverity_with_color to associate it with tokens. | ||||||
| static const char * expand_severity_with_color( | ||||||
| const logging_input_t * logging_input, | ||||||
| rcutils_char_array_t * logging_output, | ||||||
| size_t start_offset, size_t end_offset); | ||||||
|
|
||||||
| typedef struct token_map_entry_s | ||||||
| { | ||||||
| const char * token; | ||||||
|
|
@@ -389,6 +395,7 @@ typedef struct token_map_entry_s | |||||
|
|
||||||
| static const token_map_entry_t tokens[] = { | ||||||
| {.token = "severity", .handler = expand_severity}, | ||||||
| {.token = "severity_with_color", .handler = expand_severity_with_color}, | ||||||
| {.token = "name", .handler = expand_name}, | ||||||
| {.token = "message", .handler = expand_message}, | ||||||
| {.token = "function_name", .handler = expand_function_name}, | ||||||
|
|
@@ -1407,13 +1414,24 @@ rcutils_ret_t rcutils_logging_format_message( | |||||
| # define SET_STANDARD_COLOR_IN_STREAM(is_colorized, status) | ||||||
| #endif | ||||||
|
|
||||||
| static bool rcutils_logging_is_colorized() | ||||||
|
||||||
| static bool rcutils_logging_is_colorized() | |
| static bool rcutils_logging_is_colorized(void) |
ahcorde marked this conversation as resolved.
Show resolved
Hide resolved
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why we have this padding space only for this handler? i do not think this is consistent behavior for logging format?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ahcorde what do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@fujitatomoya you are more familiar with the logging system. @alexmaclean6 do you mind to address @fujitatomoya comment?
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| // Copyright 2025 Open Source Robotics Foundation, Inc. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| #include <iostream> | ||
|
|
||
| #include "rcutils/error_handling.h" | ||
| #include "rcutils/logging.h" | ||
| #include "rcutils/types/rcutils_ret.h" | ||
|
|
||
| int main(int, char **) | ||
| { | ||
| rcutils_ret_t ret = rcutils_logging_initialize(); | ||
| if (ret != RCUTILS_RET_OK) { | ||
| std::cerr << "error initializing logging: " << rcutils_get_error_string().str << std::endl; | ||
| return -1; | ||
| } | ||
|
|
||
| rcutils_ret_t status = rcutils_logging_set_logger_level("name", RCUTILS_LOG_SEVERITY_DEBUG); | ||
| if (status != RCUTILS_RET_OK) { | ||
| std::cerr << "error setting logger level: " << rcutils_get_error_string().str << std::endl; | ||
| return -1; | ||
| } | ||
|
|
||
| // Log at all 5 severities to check the colorized severity in the log output. | ||
| rcutils_log_location_t location = {"func", "file", 42u}; | ||
| rcutils_log(&location, RCUTILS_LOG_SEVERITY_DEBUG, "name", "Debug message"); | ||
| rcutils_log(&location, RCUTILS_LOG_SEVERITY_INFO, "name", "Info message"); | ||
| rcutils_log(&location, RCUTILS_LOG_SEVERITY_WARN, "name", "Warn message"); | ||
| rcutils_log(&location, RCUTILS_LOG_SEVERITY_ERROR, "name", "Error message"); | ||
| rcutils_log(&location, RCUTILS_LOG_SEVERITY_FATAL, "name", "Fatal message"); | ||
|
|
||
| std::cout.flush(); | ||
|
|
||
| return 0; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| # Copyright 2025 Open Source Robotics Foundation, Inc. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import os | ||
| import unittest | ||
|
|
||
| from launch import LaunchDescription | ||
| from launch.actions import ExecuteProcess | ||
| from launch.actions import SetEnvironmentVariable | ||
|
|
||
| import launch_testing | ||
| import launch_testing.actions | ||
| import launch_testing.asserts | ||
| import launch_testing.markers | ||
|
|
||
|
|
||
| @launch_testing.markers.keep_alive | ||
| def generate_test_description(): | ||
| test_process_name = 'test_logging_severity_with_color' | ||
| launch_description = LaunchDescription() | ||
| # Set the output format to a "verbose" format that is expected by the executable output | ||
| launch_description.add_action( | ||
| SetEnvironmentVariable( | ||
| name='RCUTILS_CONSOLE_OUTPUT_FORMAT', | ||
| value='[{severity_with_color}] [{name}]: {message} ' | ||
| '({function_name}() at {file_name}:{line_number})' | ||
| ) | ||
| ) | ||
| launch_description.add_action( | ||
| SetEnvironmentVariable( | ||
| name='RCUTILS_COLORIZED_OUTPUT', | ||
| value='0' | ||
| ) | ||
| ) | ||
| executable = os.path.join(os.getcwd(), test_process_name) | ||
| if os.name == 'nt': | ||
| executable += '.exe' | ||
| process_name = test_process_name | ||
| launch_description.add_action( | ||
| ExecuteProcess(cmd=[executable], name=process_name, output='screen') | ||
| ) | ||
|
|
||
| launch_description.add_action( | ||
| launch_testing.actions.ReadyToTest() | ||
| ) | ||
| return launch_description, {'process_name': process_name} | ||
|
|
||
|
|
||
| class TestLoggingSeverityWithColor(unittest.TestCase): | ||
|
|
||
| def test_wait_for_shutdown(self, proc_info, proc_output, process_name): | ||
| """Wait for the process to complete so the log messages will be available to inspect.""" | ||
| proc_info.assertWaitForShutdown(process=process_name, timeout=10) | ||
|
|
||
|
|
||
| @launch_testing.post_shutdown_test() | ||
| class TestLoggingSeverityWithColorAfterShutdown(unittest.TestCase): | ||
|
|
||
| def test_logging_output(self, proc_output, process_name): | ||
| """Test executable output against expectation.""" | ||
| launch_testing.asserts.assertInStderr( | ||
| proc_output, | ||
| expected_output=launch_testing.tools.expected_output_from_file( | ||
| path=os.path.join(os.path.dirname(__file__), process_name), | ||
| encoding='unicode_escape' | ||
| ), | ||
| process=process_name, | ||
| strip_ansi_escape_sequences=False | ||
| ) | ||
|
|
||
| def test_processes_exit_codes(self, proc_info): | ||
| """Test that all executables finished cleanly.""" | ||
| launch_testing.asserts.assertExitCodes(proc_info) |
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,5 @@ | ||||||||||
| [\033[32mDEBUG\033[0m] [name]: Debug message (func() at file:42) | ||||||||||
| [\033[0mINFO \033[0m] [name]: Info message (func() at file:42) | ||||||||||
| [\033[33mWARN \033[0m] [name]: Warn message (func() at file:42) | ||||||||||
|
||||||||||
| [\033[0mINFO \033[0m] [name]: Info message (func() at file:42) | |
| [\033[33mWARN \033[0m] [name]: Warn message (func() at file:42) | |
| [\033[0mINFO\033[0m] [name]: Info message (func() at file:42) | |
| [\033[33mWARN\033[0m] [name]: Warn message (func() at file:42) |
Uh oh!
There was an error while loading. Please reload this page.