Skip to content

Commit

Permalink
Adding a ton of checkpoints to keep XML logs valid
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Meisrimel authored and Peter Meisrimel committed May 28, 2024
1 parent 2dc38c5 commit a78b71e
Show file tree
Hide file tree
Showing 3 changed files with 429 additions and 59 deletions.
19 changes: 13 additions & 6 deletions src/common/log/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ def parse_fmu_xml_log(filename, modulename = 'Model', accept_errors=False):

return handler.get_root()

def extract_xml_log(dest, log, modulename = 'Model'):
def extract_xml_log(dest, log, modulename = 'Model', max_size = None):
"""
Extract the XML contents of a FMU log and write as a new file dest, or extract into a stream.
Input argument 'modulename' selects the module as recorded in the beginning of each line by
Expand All @@ -212,6 +212,9 @@ def extract_xml_log(dest, log, modulename = 'Model'):
modulename --
Selects the module as recorded in the beginning of each line by FMI Library.
Default: 'Model'
max_size --
Only read until max_size characters, if none: no limit
Default: None
"""
# if it is a string, we assume we write to a file (since the file doesnt exist yet)
dest_is_file = isinstance(dest, str)
Expand All @@ -223,30 +226,34 @@ def extract_xml_log(dest, log, modulename = 'Model'):
with open(log, 'r') as sourcefile:
if dest_is_file:
with open(dest, 'w') as destfile:
filter_xml_log(destfile.write, sourcefile, modulename)
filter_xml_log(destfile.write, sourcefile, modulename, max_size = max_size)
else:
filter_xml_log(dest.write, sourcefile, modulename)
filter_xml_log(dest.write, sourcefile, modulename, max_size = max_size)
elif hasattr(log, 'readlines'):
if dest_is_file:
with open(dest, 'w') as destfile:
filter_xml_log(destfile.write, log.readlines(), modulename)
filter_xml_log(destfile.write, log.readlines(), modulename, max_size = max_size)
else:
filter_xml_log(dest.write, log.readlines(), modulename)
filter_xml_log(dest.write, log.readlines(), modulename, max_size = max_size)
else:
raise FMUException(
"Input argument 'log' needs to be either a file, or a stream that supports 'readlines'."
)

def filter_xml_log(write, sourcefile, modulename = 'Model'):
def filter_xml_log(write, sourcefile, modulename = 'Model', max_size = None):
write('<?xml version="1.0" encoding="UTF-8"?>\n<JMILog category="info">\n')

pre_re = r'FMIL: module = ' + modulename + r', log level = ([0-9]+): \[([^]]+)\]\[FMU status:([^]]+)\] '
pre_pattern = re.compile(pre_re)

size_read = 0
for line in sourcefile:
size_read = size_read + len(line) # linebreaks?
m = pre_pattern.match(line)
if m is not None:
# log_level, category, fmu_status = m.groups()
write(line[m.end():])
if max_size is not None and size_read >= max_size:
break

write('</JMILog>\n')
1 change: 1 addition & 0 deletions src/pyfmi/fmi.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ cdef class ModelBase:
cdef public object file_object
cdef public object _additional_logger
cdef public object _max_log_size_msg_sent
cdef public object _log_checkpoint
cdef public object _result_file
cdef object _modelId
cdef public int _log_is_stream, _invoked_dealloc
Expand Down
Loading

0 comments on commit a78b71e

Please sign in to comment.