Skip to content

Commit 584676f

Browse files
allow custom names for main log file in ros2 launch
Signed-off-by: Tanishq Chaudhary <[email protected]>
1 parent e716d1f commit 584676f

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

launch/launch/launch_service.py

+11-4
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ def __init__(
5555
*,
5656
argv: Optional[Iterable[Text]] = None,
5757
noninteractive: bool = False,
58-
debug: bool = False
58+
debug: bool = False,
59+
log_file_name: str = 'launch.log'
5960
) -> None:
6061
"""
6162
Create a LaunchService.
@@ -67,12 +68,18 @@ def __init__(
6768
"""
6869
# Setup logging and debugging.
6970
launch.logging.launch_config.level = logging.DEBUG if debug else logging.INFO
71+
self._log_file_name = log_file_name
72+
# Ensure the log file name ends with `.log`
73+
if not self._log_file_name.endswith('.log'):
74+
self._log_file_name += '.log'
75+
launch.logging.launch_config.log_file_name = self._log_file_name
76+
# Setup logging
77+
self._logger_name = self._log_file_name.removesuffix('.log')
78+
self.__logger = launch.logging.get_logger(self._logger_name)
79+
7080
self.__debug = debug
7181
self.__argv = argv if argv is not None else []
7282

73-
# Setup logging
74-
self.__logger = launch.logging.get_logger('launch')
75-
7683
# Setup context and register a built-in event handler for bootstrapping.
7784
self.__context = LaunchContext(argv=self.__argv, noninteractive=noninteractive)
7885
self.__context.register_event_handler(OnIncludeLaunchDescription())

launch/launch/logging/__init__.py

+19-4
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ def reset(self):
113113
self.screen_formatter = None
114114
self.file_formatter = None
115115
self._log_handler_factory = None
116+
self._log_file_name = 'launch.log'
116117
logging.root.setLevel(logging.INFO)
117118
self.set_screen_format('default')
118119
self.set_log_format('default')
@@ -130,6 +131,20 @@ def level(self, new_level):
130131
"""
131132
logging.root.setLevel(new_level)
132133

134+
@property
135+
def log_file_name(self) -> str:
136+
"""Get the current log file name."""
137+
return self._log_file_name
138+
139+
@log_file_name.setter
140+
def log_file_name(self, log_file_name: str):
141+
"""
142+
Set the name of the log file.
143+
144+
:param log_file_name: the name of the log file where logger output should be written.
145+
"""
146+
self._log_file_name = log_file_name
147+
133148
@property
134149
def log_dir(self):
135150
"""Get the current log directory, generating it if necessary."""
@@ -341,13 +356,13 @@ def log_launch_config(*, logger=logging.root):
341356
)))
342357

343358

344-
def get_logger(name=None) -> logging.Logger:
359+
def get_logger(name=None, main_log_file_name = 'launch.log') -> logging.Logger:
345360
"""Get named logger, configured to output to screen and launch main log file."""
346361
logger = logging.getLogger(name)
347362
screen_handler = launch_config.get_screen_handler()
348363
if screen_handler not in logger.handlers:
349364
logger.addHandler(screen_handler)
350-
launch_log_file_handler = launch_config.get_log_file_handler()
365+
launch_log_file_handler = launch_config.get_log_file_handler(main_log_file_name)
351366
if launch_log_file_handler not in logger.handlers:
352367
logger.addHandler(launch_log_file_handler)
353368
return logger
@@ -416,7 +431,7 @@ def _normalize_output_configuration(config):
416431
return normalized_config
417432

418433

419-
def get_output_loggers(process_name, output_config):
434+
def get_output_loggers(process_name, output_config, main_log_file_name="launch.log"):
420435
"""
421436
Get the stdout and stderr output loggers for the given process name.
422437
@@ -476,7 +491,7 @@ def get_output_loggers(process_name, output_config):
476491
# If a 'log' output is configured for this source or for
477492
# 'both' sources, this logger should output to launch main log file.
478493
if 'log' in (output_config['both'] | output_config[source]):
479-
launch_log_file_handler = launch_config.get_log_file_handler()
494+
launch_log_file_handler = launch_config.get_log_file_handler(main_log_file_name)
480495
# Add launch main log file handler if necessary.
481496
if launch_log_file_handler not in logger.handlers:
482497
launch_log_file_handler.setFormatterFor(

0 commit comments

Comments
 (0)