Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
Signed-off-by: JafarAbdi <[email protected]>
  • Loading branch information
JafarAbdi committed Oct 6, 2021
1 parent c9f4a41 commit 442033b
Showing 1 changed file with 34 additions and 13 deletions.
47 changes: 34 additions & 13 deletions src/logging.c
Original file line number Diff line number Diff line change
Expand Up @@ -296,9 +296,9 @@ rcutils_ret_t rcutils_logging_initialize_with_allocator(rcutils_allocator_t allo

// We load the logging configs after setting g_rcutils_logging_initialized
// to true otherwise rcutils_logging_set_logger_level will cause recursive
// call to this function due to RCUTILS_LOGGING_AUTOINIT Check for the
// call to this function due to RCUTILS_LOGGING_AUTOINIT
// Check for the environment variable for selecting logging level
const char * logging_config_filename;
const char * logging_config_filename = NULL;
ret_str = rcutils_get_env("RCUTILS_LOGGING_CONFIG_FILE", &logging_config_filename);
if (NULL == ret_str && strcmp(logging_config_filename, "") != 0) {
FILE * logging_config_file = fopen(logging_config_filename, "r");
Expand All @@ -308,24 +308,45 @@ rcutils_ret_t rcutils_logging_initialize_with_allocator(rcutils_allocator_t allo
logging_config_filename);
return RCUTILS_RET_ERROR;
}
char logger_name[50];
char severity[10]; // fatal error debug info warn case insensitive

while (fscanf(
logging_config_file, "%49[^=]=%9[^\n]\n", logger_name,
severity) != EOF)
{
char logger_name[128];
char severity[10]; // fatal/error/debug/info/warn case-insensitive
char line[256]; // lines could have comments
while (fgets(line, sizeof(line), logging_config_file)) {
// If a line start with # ignore it since it's a comment
if (line[0] == '#') {
continue;
}
if (sscanf(
line, "%127[^=]=%9[^\t\n ]\n", logger_name,
severity) != 2)
{
RCUTILS_SET_ERROR_MSG_WITH_FORMAT_STRING(
"Failed to parse line in logging config file [%s]: %s", logging_config_filename, line);
fclose(logging_config_file);
return RCUTILS_RET_ERROR;
}
int severity_level;
if (RCUTILS_RET_OK != rcutils_logging_severity_level_from_string(
severity, g_rcutils_logging_allocator, &severity_level))
{
RCUTILS_SAFE_FWRITE_TO_STDERR_WITH_FORMAT_STRING(
"Logger has an invalid severity level: %s\n", severity);
RCUTILS_SET_ERROR_MSG_WITH_FORMAT_STRING(
"Logger has an invalid severity level: %s", severity);
fclose(logging_config_file);
return RCUTILS_RET_ERROR;
}
if (RCUTILS_RET_OK != rcutils_logging_set_logger_level(logger_name, severity_level)) {
RCUTILS_SAFE_FWRITE_TO_STDERR_WITH_FORMAT_STRING(
"Failed to set severity level: %s for logger '%s'\n", severity, logger_name);
// Special name for setting the default logger level
if (strcmp(logger_name, "default_logger_level") == 0) {
g_rcutils_logging_default_logger_level = severity_level;
// NOLINTNEXTLINE
} else if (
RCUTILS_RET_OK !=
rcutils_logging_set_logger_level(logger_name, severity_level))
{
RCUTILS_SET_ERROR_MSG_WITH_FORMAT_STRING(
"Failed to set severity level: %s for logger '%s'", severity, logger_name);
fclose(logging_config_file);
return RCUTILS_RET_ERROR;
}
}

Expand Down

0 comments on commit 442033b

Please sign in to comment.