|
50 | 50 | #include "rcutils/types/hash_map.h" |
51 | 51 |
|
52 | 52 |
|
| 53 | +#define RCUTILS_LOGGING_BACKSLASH_CHAR '\\' |
53 | 54 | #define RCUTILS_LOGGING_SEPARATOR_CHAR '.' |
54 | 55 |
|
55 | 56 | #define RCUTILS_LOGGING_MAX_OUTPUT_FORMAT_LEN (2048) |
@@ -414,6 +415,67 @@ static const char * copy_from_orig( |
414 | 415 | return logging_output->buffer; |
415 | 416 | } |
416 | 417 |
|
| 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 | + |
417 | 479 | static bool add_handler(token_handler handler, size_t start_offset, size_t end_offset) |
418 | 480 | { |
419 | 481 | 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 |
625 | 687 | } |
626 | 688 | } |
627 | 689 |
|
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); |
634 | 691 |
|
635 | 692 | g_rcutils_logging_severities_map = rcutils_get_zero_initialized_hash_map(); |
636 | 693 | rcutils_ret_t hash_map_ret = rcutils_hash_map_init( |
|
0 commit comments