Skip to content

Commit a63135b

Browse files
committed
Add option to set logger severity level using a config file
1 parent 304e143 commit a63135b

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

src/logging.c

+38
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,44 @@ rcutils_ret_t rcutils_logging_initialize_with_allocator(rcutils_allocator_t allo
293293
}
294294

295295
g_rcutils_logging_initialized = true;
296+
297+
// We load the logging configs after setting g_rcutils_logging_initialized
298+
// to true otherwise rcutils_logging_set_logger_level will cause recursive
299+
// call to this function due to RCUTILS_LOGGING_AUTOINIT Check for the
300+
// Check for the environment variable for selecting logging level
301+
const char * logging_config_filename;
302+
ret_str = rcutils_get_env("RCUTILS_LOGGING_CONFIG_FILE", &logging_config_filename);
303+
if (NULL == ret_str && strcmp(logging_config_filename, "") != 0) {
304+
FILE * logging_config_file = fopen(logging_config_filename, "r");
305+
if (NULL == logging_config_file) {
306+
RCUTILS_SET_ERROR_MSG_WITH_FORMAT_STRING(
307+
"Failed to open logging config file `[%s]`.",
308+
logging_config_filename);
309+
return RCUTILS_RET_ERROR;
310+
}
311+
char logger_name[50];
312+
char severity[10]; // fatal error debug info warn case insensitive
313+
314+
while (fscanf(
315+
logging_config_file, "%49[^=]=%9[^\n]\n", logger_name,
316+
severity) != EOF)
317+
{
318+
int severity_level;
319+
if (RCUTILS_RET_OK != rcutils_logging_severity_level_from_string(
320+
severity, g_rcutils_logging_allocator, &severity_level))
321+
{
322+
RCUTILS_SAFE_FWRITE_TO_STDERR_WITH_FORMAT_STRING(
323+
"Logger has an invalid severity level: %s\n", severity);
324+
return RCUTILS_RET_ERROR;
325+
}
326+
if (RCUTILS_RET_OK != rcutils_logging_set_logger_level(logger_name, severity_level)) {
327+
RCUTILS_SAFE_FWRITE_TO_STDERR_WITH_FORMAT_STRING(
328+
"Failed to set severity level: %s for logger '%s'\n", severity, logger_name);
329+
}
330+
}
331+
332+
fclose(logging_config_file);
333+
}
296334
}
297335
return ret;
298336
}

0 commit comments

Comments
 (0)