diff --git a/fault/logging.py b/fault/logging.py new file mode 100644 index 00000000..9f0f90ff --- /dev/null +++ b/fault/logging.py @@ -0,0 +1,50 @@ +""" +Note(rsetaluri): This file is taken almost verbatim from magma/magma/logging.py. +""" + +from __future__ import absolute_import +from __future__ import print_function + +import logging +import traceback +import inspect +import sys + + +log = logging.getLogger("fault") + + +def get_original_wire_call_stack_frame(): + for frame in inspect.stack(): + if sys.version_info < (3, 5): + function = inspect.getframeinfo(frame[0]).function + else: + function = frame.function + if function not in ["wire", "connect", + "get_original_wire_call_stack_frame", + "error", "warn"]: + break + if sys.version_info < (3, 5): + return frame[0] + else: + return frame.frame + + +def info(message, *args, **kwargs): + log.info(message, *args, **kwargs) + + +def warning(message, *args, **kwargs): + log.warning(message, *args, **kwargs) + + +def error(message, include_wire_traceback=False, *args, **kwargs): + if include_wire_traceback: + sys.stderr.write("="*80 + "\n") + stack_frame = get_original_wire_call_stack_frame() + traceback.print_stack(f=stack_frame, limit=10, file=sys.stderr) + # FIXME: In python 2, log.error doesn't go to stderr by default + # log.error(message, *args, **kwargs) + print(message, file=sys.stderr, *args, **kwargs) + if include_wire_traceback: + sys.stderr.write("="*80 + "\n")