Skip to content

Commit

Permalink
Add warnings in a separate file too.
Browse files Browse the repository at this point in the history
This commit configures the logger so that when a log file is
requested, another file with the suffix `_warnings` is added,
containing only the warnings. The warnings are still being placed in
the log file as well. The file name for the warnings file takes care
of any file extensions, even when there are periods in the file name
itself before the file extension. Extra spaces and color formatting is
removed from the warnings so that the log file doesn't contain escape
sequences used for coloring the terminal output.
  • Loading branch information
ioannis-vm committed May 18, 2024
1 parent 92fe50f commit 0deafd5
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
33 changes: 30 additions & 3 deletions pelicun/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,11 +289,18 @@ def __init__(self, verbose, log_show_ms, log_file, print_log):

if log_file is None:
self.log_file = None
self.warning_file = None
else:
try:
filepath = Path(log_file).resolve()
self.log_file = str(filepath)
with open(filepath, 'w', encoding='utf-8') as f:
path = Path(log_file)
self.log_file = str(path.resolve())
name, extension = split_file_name(self.log_file)
self.warning_file = (
path.parent / (name + '_warnings' + extension)
).resolve()
with open(self.log_file, 'w', encoding='utf-8') as f:
f.write('')
with open(self.warning_file, 'w', encoding='utf-8') as f:
f.write('')
except BaseException as err:
print(
Expand Down Expand Up @@ -396,6 +403,14 @@ def emit_warnings(self):
for message in self.warning_stack:
if message not in self.emitted:
warnings.warn(message, PelicunWarning)
if self.warning_file is not None:
with open(self.warning_file, 'a', encoding='utf-8') as f:
f.write(
message.replace(Fore.RED, '')
.replace(Style.RESET_ALL, '')
.replace(self.spaces, '')
)

self.emitted = self.emitted.union(set(self.warning_stack))
self.warning_stack = []

Expand Down Expand Up @@ -445,6 +460,18 @@ def print_system_info(self):
pelicun_path = Path(os.path.dirname(os.path.abspath(__file__)))


def split_file_name(file_path: str):
"""
Separates a file name from the extension accounting for the case
where the file name itself contains periods.
"""
path = Path(file_path)
name = path.stem
extension = path.suffix
return name, extension


def control_warnings():
"""
Convenience function to turn warnings on/off
Expand Down
12 changes: 12 additions & 0 deletions pelicun/tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,18 @@ def test_logger_div():
os.remove('log.txt')


def test_split_file_name():
file_path = "example.file.name.txt"
name, extension = base.split_file_name(file_path)
assert name == 'example.file.name'
assert extension == '.txt'

file_path = "example"
name, extension = base.split_file_name(file_path)
assert name == 'example'
assert extension == ''


def test_print_system_info():
# create a logger object
log_config = {
Expand Down

0 comments on commit 0deafd5

Please sign in to comment.