Skip to content

Commit 5dfee3f

Browse files
author
Tomoya Fujita
authored
rcutils_logging_allocator_initialize() support. (ros2#419)
Signed-off-by: Tomoya Fujita <Tomoya.Fujita@sony.com>
1 parent 3bbdf93 commit 5dfee3f

3 files changed

Lines changed: 75 additions & 4 deletions

File tree

‎include/rcutils/logging.h‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,30 @@ extern "C"
4646
RCUTILS_PUBLIC
4747
extern bool g_rcutils_logging_initialized;
4848

49+
/// Initialize the logging allocator.
50+
/**
51+
* This function is called automatically when using the logging macros.
52+
* Initialize the logging allocator only if it is not initialized yet.
53+
*
54+
* <hr>
55+
* Attribute | Adherence
56+
* ------------------ | -------------
57+
* Allocates Memory | No
58+
* Thread-Safe | No
59+
* Uses Atomics | No
60+
* Lock-Free | Yes
61+
*
62+
* \return #RCUTILS_RET_OK if successful initialized, or
63+
* \return #RCUTILS_RET_INVALID_ARGUMENT if the allocator is invalid.
64+
*/
65+
RCUTILS_PUBLIC
66+
RCUTILS_WARN_UNUSED
67+
rcutils_ret_t rcutils_logging_allocator_initialize(
68+
const rcutils_allocator_t * allocator);
69+
4970
/// Initialize the logging system using the specified allocator.
5071
/**
72+
* Call rcutils_logging_allocator_initialize() using allocator argument.
5173
* Initialize the logging system only if it was not in an initialized state.
5274
*
5375
* If an invalid allocator is passed, the initialization will fail.

‎src/logging.c‎

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -626,20 +626,56 @@ static void parse_and_create_handlers_list(void)
626626
}
627627
}
628628

629+
rcutils_ret_t rcutils_logging_allocator_initialize(
630+
const rcutils_allocator_t * allocator)
631+
{
632+
RCUTILS_CHECK_ALLOCATOR_WITH_MSG(
633+
allocator, "invalid allocator", return RCUTILS_RET_INVALID_ARGUMENT);
634+
635+
if (rcutils_allocator_is_valid(&g_rcutils_logging_allocator)) {
636+
return RCUTILS_RET_OK;
637+
}
638+
g_rcutils_logging_allocator = *allocator;
639+
640+
return RCUTILS_RET_OK;
641+
}
642+
629643
rcutils_ret_t rcutils_logging_initialize_with_allocator(rcutils_allocator_t allocator)
630644
{
631645
if (g_rcutils_logging_initialized) {
632646
return RCUTILS_RET_OK;
633647
}
634648

635-
if (!rcutils_allocator_is_valid(&allocator)) {
636-
RCUTILS_SET_ERROR_MSG("Provided allocator is invalid.");
649+
if (rcutils_logging_allocator_initialize(&allocator) != RCUTILS_RET_OK) {
637650
return RCUTILS_RET_INVALID_ARGUMENT;
638651
}
639-
g_rcutils_logging_allocator = allocator;
640652

641653
g_rcutils_logging_output_handler = &rcutils_logging_console_output_handler;
642-
g_rcutils_logging_default_logger_level = RCUTILS_DEFAULT_LOGGER_DEFAULT_LEVEL;
654+
// Check for the environment variable for default logger level
655+
const char * env_default_level;
656+
const char * ret_str_level =
657+
rcutils_get_env("RCUTILS_DEFAULT_LOGGER_DEFAULT_LEVEL", &env_default_level);
658+
if (NULL != ret_str_level) {
659+
RCUTILS_SET_ERROR_MSG_WITH_FORMAT_STRING(
660+
"Error getting environment variable RCUTILS_DEFAULT_LOGGER_DEFAULT_LEVEL: %s", ret_str_level);
661+
g_rcutils_logging_default_logger_level = RCUTILS_DEFAULT_LOGGER_DEFAULT_LEVEL;
662+
} else if (strcmp(env_default_level, "") == 0) {
663+
// Environment variable is empty, use default
664+
g_rcutils_logging_default_logger_level = RCUTILS_DEFAULT_LOGGER_DEFAULT_LEVEL;
665+
} else {
666+
// Try to convert the environment variable value to a severity level
667+
int env_severity;
668+
rcutils_ret_t severity_ret = rcutils_logging_severity_level_from_string(
669+
env_default_level, g_rcutils_logging_allocator, &env_severity);
670+
if (severity_ret == RCUTILS_RET_OK) {
671+
g_rcutils_logging_default_logger_level = env_severity;
672+
} else {
673+
RCUTILS_SET_ERROR_MSG_WITH_FORMAT_STRING(
674+
"Invalid severity level '%s' in RCUTILS_DEFAULT_LOGGER_DEFAULT_LEVEL. Using default.",
675+
env_default_level);
676+
g_rcutils_logging_default_logger_level = RCUTILS_DEFAULT_LOGGER_DEFAULT_LEVEL;
677+
}
678+
}
643679

644680
const char * line_buffered = NULL;
645681
const char * ret_str = rcutils_get_env("RCUTILS_CONSOLE_STDOUT_LINE_BUFFERED", &line_buffered);
@@ -802,6 +838,7 @@ rcutils_ret_t rcutils_logging_shutdown(void)
802838
g_rcutils_logging_severities_map_valid = false;
803839
}
804840
g_num_log_msg_handlers = 0;
841+
g_rcutils_logging_allocator = rcutils_get_zero_initialized_allocator();
805842
g_rcutils_logging_initialized = false;
806843

807844
#ifdef _WIN32

‎test/test_logging.cpp‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,18 @@
2424
#include "rcutils/logging.h"
2525
#include "rcutils/strdup.h"
2626

27+
TEST(TestLogging, test_logging_allocator_initialization) {
28+
rcutils_allocator_t allocator = rcutils_get_default_allocator();
29+
rcutils_allocator_t invalid_allocator = rcutils_get_zero_initialized_allocator();
30+
31+
ASSERT_EQ(RCUTILS_RET_INVALID_ARGUMENT, rcutils_logging_allocator_initialize(NULL));
32+
ASSERT_EQ(RCUTILS_RET_INVALID_ARGUMENT, rcutils_logging_allocator_initialize(&invalid_allocator));
33+
34+
ASSERT_EQ(RCUTILS_RET_OK, rcutils_logging_allocator_initialize(&allocator));
35+
// 2nd time will also succeed.
36+
ASSERT_EQ(RCUTILS_RET_OK, rcutils_logging_allocator_initialize(&allocator));
37+
}
38+
2739
TEST(TestLogging, test_logging_initialization) {
2840
EXPECT_FALSE(g_rcutils_logging_initialized);
2941
ASSERT_EQ(RCUTILS_RET_OK, rcutils_logging_initialize());

0 commit comments

Comments
 (0)