Skip to content

Commit f078c8c

Browse files
Chen LihuiTomoya Fujitaclalancette
authored
make escape characters work (ros2#426)
* make escape characters work Signed-off-by: Chen Lihui <lihui.chen@sony.com> * remove \\ and \0 cases as it seems unnecessary add the extra one position only if an expected char found * use a different name for variables and address flake8 * decode once at the beginning * reduce the times of memcpy * update test to make the escape characters parsered by rcutils not by python itself * not to process the \f and \v escaped characters Signed-off-by: Chen Lihui <lihui.chen@sony.com> Co-authored-by: Tomoya Fujita <Tomoya.Fujita@sony.com> Co-authored-by: Chris Lalancette <clalancette@gmail.com>
1 parent 629d554 commit f078c8c

4 files changed

Lines changed: 88 additions & 7 deletions

‎src/logging.c‎

Lines changed: 63 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
#include "rcutils/types/hash_map.h"
5151

5252

53+
#define RCUTILS_LOGGING_BACKSLASH_CHAR '\\'
5354
#define RCUTILS_LOGGING_SEPARATOR_CHAR '.'
5455

5556
#define RCUTILS_LOGGING_MAX_OUTPUT_FORMAT_LEN (2048)
@@ -414,6 +415,67 @@ static const char * copy_from_orig(
414415
return logging_output->buffer;
415416
}
416417

418+
// copy buffers and decode escape characters if they exist
419+
static void create_format_string(
420+
const char * logging_output_format_string)
421+
{
422+
size_t dest_buffer_index = 0;
423+
size_t start_offset = 0;
424+
size_t start_offset_previous_not_copy = 0;
425+
size_t back_slash_index = 0;
426+
427+
size_t length = strlen(logging_output_format_string);
428+
if (length > RCUTILS_LOGGING_MAX_OUTPUT_FORMAT_LEN - 1) {
429+
length = RCUTILS_LOGGING_MAX_OUTPUT_FORMAT_LEN - 1;
430+
}
431+
432+
for (size_t i = 0; i < length; ) {
433+
back_slash_index = rcutils_findn(
434+
logging_output_format_string + i, RCUTILS_LOGGING_BACKSLASH_CHAR, length - i);
435+
if (back_slash_index == SIZE_MAX) {
436+
memcpy(
437+
g_rcutils_logging_output_format_string + dest_buffer_index,
438+
logging_output_format_string + start_offset - start_offset_previous_not_copy,
439+
length - start_offset + start_offset_previous_not_copy);
440+
break;
441+
} else {
442+
const char * expected_char = NULL;
443+
switch (logging_output_format_string[i + back_slash_index + 1]) {
444+
case 'a': expected_char = "\a"; break; // alert
445+
case 'b': expected_char = "\b"; break; // backspace
446+
case 'n': expected_char = "\n"; break; // new line
447+
case 'r': expected_char = "\r"; break; // carriage return
448+
case 't': expected_char = "\t"; break; // horizontal tab
449+
default:
450+
break;
451+
}
452+
453+
if (expected_char) {
454+
if (back_slash_index > 0) {
455+
// copy previous buffer first
456+
size_t len = back_slash_index + start_offset_previous_not_copy;
457+
memcpy(
458+
g_rcutils_logging_output_format_string + dest_buffer_index,
459+
logging_output_format_string + start_offset,
460+
len);
461+
dest_buffer_index += len;
462+
start_offset += len;
463+
start_offset_previous_not_copy = 0;
464+
}
465+
466+
// copy the decoded character
467+
g_rcutils_logging_output_format_string[dest_buffer_index] = expected_char[0];
468+
dest_buffer_index += 1;
469+
start_offset += 2;
470+
} else {
471+
start_offset_previous_not_copy += (back_slash_index + 2);
472+
}
473+
474+
i += (back_slash_index + 2);
475+
}
476+
}
477+
}
478+
417479
static bool add_handler(token_handler handler, size_t start_offset, size_t end_offset)
418480
{
419481
if (g_num_log_msg_handlers >= (sizeof(g_handlers) / sizeof(g_handlers[0]))) {
@@ -625,12 +687,7 @@ rcutils_ret_t rcutils_logging_initialize_with_allocator(rcutils_allocator_t allo
625687
}
626688
}
627689

628-
size_t chars_to_copy = strlen(output_format);
629-
if (chars_to_copy > RCUTILS_LOGGING_MAX_OUTPUT_FORMAT_LEN - 1) {
630-
chars_to_copy = RCUTILS_LOGGING_MAX_OUTPUT_FORMAT_LEN - 1;
631-
}
632-
memcpy(g_rcutils_logging_output_format_string, output_format, chars_to_copy);
633-
g_rcutils_logging_output_format_string[chars_to_copy] = '\0';
690+
create_format_string(output_format);
634691

635692
g_rcutils_logging_severities_map = rcutils_get_zero_initialized_hash_map();
636693
rcutils_ret_t hash_map_ret = rcutils_hash_map_init(

‎test/test_logging_output_format.py‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,25 @@ def generate_test_description():
7272
))
7373
processes_to_test.append(name)
7474

75+
env_simple_escape_tokens = dict(os.environ)
76+
# This custom output is to check that escape characters work correctly.
77+
env_simple_escape_tokens['RCUTILS_CONSOLE_OUTPUT_FORMAT'] = '\\t{name}\\n\\t{message}\\n'
78+
name = 'test_logging_output_format_simple_escape'
79+
launch_description.add_action(ExecuteProcess(
80+
cmd=[executable], env=env_simple_escape_tokens, name=name, output='screen'
81+
))
82+
processes_to_test.append(name)
83+
84+
env_complicated_escape_tokens = dict(os.environ)
85+
# This custom output is to check that escape characters work correctly.
86+
env_complicated_escape_tokens['RCUTILS_CONSOLE_OUTPUT_FORMAT'] = \
87+
'{name} 0\\a1\\b23\\n44\\r5\\t67 {message}'
88+
name = 'test_logging_output_format_complicated_escape'
89+
launch_description.add_action(ExecuteProcess(
90+
cmd=[executable], env=env_complicated_escape_tokens, name=name, output='screen'
91+
))
92+
processes_to_test.append(name)
93+
7594
launch_description.add_action(
7695
launch_testing.actions.ReadyToTest()
7796
)
@@ -95,7 +114,8 @@ def test_logging_output(self, proc_output, processes_to_test):
95114
launch_testing.asserts.assertInStderr(
96115
proc_output,
97116
expected_output=launch_testing.tools.expected_output_from_file(
98-
path=os.path.join(os.path.dirname(__file__), process_name)
117+
path=os.path.join(os.path.dirname(__file__), process_name),
118+
encoding='unicode_escape'
99119
),
100120
process=process_name
101121
)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
name1 0\a1\b23\n44\r5\t67 Xx{2045}X
2+
name2 0\a1\b23\n44\r5\t67 X42x{2043}X
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
\tname1\n\tXx{2045}X\n
2+
\tname2\n\tX42x{2043}X\n

0 commit comments

Comments
 (0)