Skip to content

Commit b11f212

Browse files
updated test based on custom and no naming of log files
Signed-off-by: Tanishq Chaudhary <[email protected]>
1 parent edb57b1 commit b11f212

File tree

2 files changed

+29
-11
lines changed

2 files changed

+29
-11
lines changed

launch/launch/logging/__init__.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,8 @@ def _normalize_output_configuration(config):
430430
return normalized_config
431431

432432

433-
def get_output_loggers(process_name, output_config):
433+
def get_output_loggers(
434+
process_name, output_config, main_log_file_name='launch.log'):
434435
"""
435436
Get the stdout and stderr output loggers for the given process name.
436437
@@ -471,6 +472,7 @@ def get_output_loggers(process_name, output_config):
471472
:param process_name: the process-like action whose outputs want to be logged.
472473
:param output_config: configuration for the output loggers,
473474
see above for details.
475+
:param main_log_file: the main log file from the ros2launch
474476
:returns: a tuple with the stdout and stderr output loggers.
475477
"""
476478
output_config = _normalize_output_configuration(output_config)
@@ -490,6 +492,7 @@ def get_output_loggers(process_name, output_config):
490492
# If a 'log' output is configured for this source or for
491493
# 'both' sources, this logger should output to launch main log file.
492494
if 'log' in (output_config['both'] | output_config[source]):
495+
launch_config.log_file_name = main_log_file_name
493496
launch_log_file_handler = launch_config.get_log_file_handler()
494497
# Add launch main log file handler if necessary.
495498
if launch_log_file_handler not in logger.handlers:
@@ -517,8 +520,6 @@ def get_output_loggers(process_name, output_config):
517520
if combined_log_file_handler not in logger.handlers:
518521
logger.addHandler(combined_log_file_handler)
519522

520-
# Reset log file name since we will iterate over the sources
521-
launch_config._log_file_name = None
522523
# Retrieve both loggers.
523524
return (
524525
logging.getLogger(process_name + '-stdout'),

launch/test/launch/test_logging.py

+25-8
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def test_output_loggers_bad_configuration(log_dir):
6767
launch.logging.get_output_loggers('some-proc', {'stdout': {'garbage'}})
6868

6969

70-
@pytest.mark.parametrize('config,checks', [
70+
configs = [
7171
('screen', {'stdout': {'screen'}, 'stderr': {'screen'}}),
7272
('log', {'stdout': {'log'}, 'stderr': {'log', 'screen'}}),
7373
('both', {'both': {'log', 'screen'}}),
@@ -88,16 +88,32 @@ def test_output_loggers_bad_configuration(log_dir):
8888
'stderr': {'own_log'}
8989
},
9090
)
91-
])
92-
def test_output_loggers_configuration(capsys, log_dir, config, checks, mock_clean_env):
91+
]
92+
log_file_names = ['custom-log-name.log', None]
93+
params = [
94+
(config, checks, log_file_name)
95+
for (config, checks) in configs
96+
for log_file_name in log_file_names
97+
]
98+
99+
100+
@pytest.mark.parametrize('config,checks,main_log_file_name', params)
101+
def test_output_loggers_configuration(
102+
capsys, log_dir, config, checks, main_log_file_name, mock_clean_env
103+
):
93104
checks = {'stdout': set(), 'stderr': set(), 'both': set(), **checks}
94105
launch.logging.reset()
106+
if main_log_file_name is None:
107+
main_log_file_name = launch.logging.launch_config.log_file_name
108+
logger_name = main_log_file_name.removesuffix('.log')
95109
launch.logging.launch_config.log_dir = log_dir
96-
logger = launch.logging.get_logger('some-proc')
110+
launch.logging.launch_config.log_file_name = main_log_file_name
111+
logger = launch.logging.get_logger(logger_name)
97112
logger.addHandler(launch.logging.launch_config.get_screen_handler())
98113
logger.addHandler(launch.logging.launch_config.get_log_file_handler())
99114
logger.setLevel(logging.ERROR)
100-
stdout_logger, stderr_logger = launch.logging.get_output_loggers('some-proc', config)
115+
stdout_logger, stderr_logger = launch.logging.get_output_loggers(
116+
'some-proc', config, main_log_file_name)
101117

102118
logger.debug('oops')
103119
logger.error('baz')
@@ -106,21 +122,22 @@ def test_output_loggers_configuration(capsys, log_dir, config, checks, mock_clea
106122

107123
capture = capsys.readouterr()
108124
lines = list(reversed(capture.out.splitlines()))
109-
assert '[ERROR] [some-proc]: baz' == lines.pop()
125+
assert f'[ERROR] [{logger_name}]: baz' == lines.pop()
110126
if 'screen' in (checks['stdout'] | checks['both']):
111127
assert 'foo' == lines.pop()
112128
if 'screen' in (checks['stderr'] | checks['both']):
113129
assert 'bar' == lines.pop()
114130
assert 0 == len(lines)
115131
assert 0 == len(capture.err)
116132

117-
launch.logging.launch_config.log_file_name = 'launch.log'
133+
launch.logging.launch_config.log_file_name = main_log_file_name
118134
main_log_path = launch.logging.launch_config.get_log_file_path()
119135
assert os.path.exists(main_log_path)
120136
assert 0 != os.stat(main_log_path).st_size
121137
with open(main_log_path, 'r') as f:
122138
lines = list(reversed(f.readlines()))
123-
assert re.match(r'[0-9]+\.[0-9]+ \[ERROR\] \[some-proc\]: baz', lines.pop()) is not None
139+
assert re.match(rf'[0-9]+\.[0-9]+ \[ERROR\] \[{logger_name}\]: baz',
140+
lines.pop()) is not None
124141
if 'log' in (checks['stdout'] | checks['both']):
125142
assert re.match(r'[0-9]+\.[0-9]+ foo', lines.pop()) is not None
126143
if 'log' in (checks['stderr'] | checks['both']):

0 commit comments

Comments
 (0)