Updated for written log

This commit is contained in:
Dimitris Zlatanidis 2024-03-20 12:19:07 +02:00
parent d3397695a3
commit 8e9727364c

View file

@ -22,6 +22,10 @@ class MultiProcess(Configs):
self.ascii = AsciiBox()
self.errors = Errors()
self.timestamp: str = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
self.head_message: str = f'Timestamp: {self.timestamp}'
self.bottom_message: str = 'EOF - End of log file'
self.option_for_silent: bool = self.utils.is_option(
('-n', '--silent'), flags)
@ -71,45 +75,54 @@ class MultiProcess(Configs):
Returns:
None.
"""
timestamp: str = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
head_message: str = f'Timestamp: {timestamp}'
bottom_message: str = 'EOF - End of log file'
process = subprocess.Popen(command, shell=True, stdout=stdout, stderr=stderr, text=True)
# Write the timestamp at the head of the log file.
with open(self.slpkg_log_file, 'a') as log:
log.write(f"{len(head_message) * '='}\n")
log.write(f'{head_message}\n')
log.write(f"{len(head_message) * '='}\n")
self._write_log_head()
# Write the process to the log file and to the terminal.
with process.stdout as output:
for i, line in enumerate(output):
if not self.silent_mode and not self.option_for_silent:
print(line.strip()) # Print to console
with open(self.slpkg_log_file, 'a') as log:
log.write(line) # Write to log file
if self.process_log:
with open(self.slpkg_log_file, 'a') as log:
log.write(line) # Write to log file
# Write the bottom of the log file.
with open(self.slpkg_log_file, 'a') as log:
log.write(f"\n{len(bottom_message) * '='}\n")
log.write(f'{bottom_message}\n')
log.write(f"{len(bottom_message) * '='}\n\n")
self._write_log_eof()
process.wait() # Wait for the process to finish
# If the process failed, return exit code.
if process.returncode != 0:
if not self.silent_mode and not self.option_for_silent:
message: str = f'Error occurred with process. Please check the log file.'
print()
print(len(message) * '=')
print(f'{self.bred}{message}{self.endc}')
print(len(message) * '=')
print()
self._error_process()
raise SystemExit(process.returncode)
def _error_process(self):
""" Prints error message for process. """
if not self.silent_mode and not self.option_for_silent:
message: str = f'Error occurred with process. Please check the log file.'
print()
print(len(message) * '=')
print(f'{self.bred}{message}{self.endc}')
print(len(message) * '=')
print()
def _write_log_head(self) -> None:
""" Write the timestamp at the head of the log file. """
if self.process_log:
with open(self.slpkg_log_file, 'a') as log:
log.write(f"{len(self.head_message) * '='}\n")
log.write(f'{self.head_message}\n')
log.write(f"{len(self.head_message) * '='}\n")
def _write_log_eof(self) -> None:
""" Write the bottom of the log file. """
if self.process_log:
with open(self.slpkg_log_file, 'a') as log:
log.write(f"\n{len(self.bottom_message) * '='}\n")
log.write(f'{self.bottom_message}\n')
log.write(f"{len(self.bottom_message) * '='}\n\n")
def process(self, command: str, stderr=None, stdout=None) -> None:
"""
Build the package and write a log file.