Skip to content

Commit 442033b

Browse files
committed
Update
Signed-off-by: JafarAbdi <[email protected]>
1 parent c9f4a41 commit 442033b

File tree

1 file changed

+34
-13
lines changed

1 file changed

+34
-13
lines changed

src/logging.c

+34-13
Original file line numberDiff line numberDiff line change
@@ -296,9 +296,9 @@ rcutils_ret_t rcutils_logging_initialize_with_allocator(rcutils_allocator_t allo
296296

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

314-
while (fscanf(
315-
logging_config_file, "%49[^=]=%9[^\n]\n", logger_name,
316-
severity) != EOF)
317-
{
312+
char logger_name[128];
313+
char severity[10]; // fatal/error/debug/info/warn case-insensitive
314+
char line[256]; // lines could have comments
315+
while (fgets(line, sizeof(line), logging_config_file)) {
316+
// If a line start with # ignore it since it's a comment
317+
if (line[0] == '#') {
318+
continue;
319+
}
320+
if (sscanf(
321+
line, "%127[^=]=%9[^\t\n ]\n", logger_name,
322+
severity) != 2)
323+
{
324+
RCUTILS_SET_ERROR_MSG_WITH_FORMAT_STRING(
325+
"Failed to parse line in logging config file [%s]: %s", logging_config_filename, line);
326+
fclose(logging_config_file);
327+
return RCUTILS_RET_ERROR;
328+
}
318329
int severity_level;
319330
if (RCUTILS_RET_OK != rcutils_logging_severity_level_from_string(
320331
severity, g_rcutils_logging_allocator, &severity_level))
321332
{
322-
RCUTILS_SAFE_FWRITE_TO_STDERR_WITH_FORMAT_STRING(
323-
"Logger has an invalid severity level: %s\n", severity);
333+
RCUTILS_SET_ERROR_MSG_WITH_FORMAT_STRING(
334+
"Logger has an invalid severity level: %s", severity);
335+
fclose(logging_config_file);
324336
return RCUTILS_RET_ERROR;
325337
}
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);
338+
// Special name for setting the default logger level
339+
if (strcmp(logger_name, "default_logger_level") == 0) {
340+
g_rcutils_logging_default_logger_level = severity_level;
341+
// NOLINTNEXTLINE
342+
} else if (
343+
RCUTILS_RET_OK !=
344+
rcutils_logging_set_logger_level(logger_name, severity_level))
345+
{
346+
RCUTILS_SET_ERROR_MSG_WITH_FORMAT_STRING(
347+
"Failed to set severity level: %s for logger '%s'", severity, logger_name);
348+
fclose(logging_config_file);
349+
return RCUTILS_RET_ERROR;
329350
}
330351
}
331352

0 commit comments

Comments
 (0)