Skip to content

Commit

Permalink
src/__init__.py: log(): make slightly more robust.
Browse files Browse the repository at this point in the history
  • Loading branch information
julian-smith-artifex-com committed Jul 22, 2024
1 parent 550411c commit f49f756
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions src/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,22 @@ def log( text='', caller=1):
'''
For development/debugging diagnostics.
'''
frame_record = inspect.stack( context=0)[ caller]
filename = os.path.relpath(frame_record.filename)
line = frame_record.lineno
function = frame_record.function
text = f'{filename}:{line}:{function}: {text}'
try:
stack = inspect.stack(context=0)
except StopIteration:
pass
else:
frame_record = stack[caller]
try:
filename = os.path.relpath(frame_record.filename)
except Exception: # Can fail on windows.
filename = frame_record.filename
line = frame_record.lineno
function = frame_record.function
text = f'{filename}:{line}:{function}(): {text}'
if _g_log_items_active:
_g_log_items.append(text)
print(text, file=_g_out_log)
_g_out_log.flush()
print(text, file=_g_out_log, flush=1)


def message(text=''):
Expand Down

0 comments on commit f49f756

Please sign in to comment.