From a3bd99e972aa718b6e00669042606a1b175646de Mon Sep 17 00:00:00 2001 From: Raj Setaluri Date: Mon, 23 Jul 2018 11:25:04 -0700 Subject: [PATCH 1/4] Add logging module Similar to magma logging module (magma/magma/logging.py). --- fault/logging.py | 50 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 fault/logging.py 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") From 257138d3bdeed4e2951fa1d28a1804943cf94451 Mon Sep 17 00:00:00 2001 From: Raj Setaluri Date: Mon, 23 Jul 2018 12:48:30 -0700 Subject: [PATCH 2/4] Simplify error logging Remove magma specific logic. fault.logging.error() is now just a wrapper of python.logging.error(). --- fault/logging.py | 32 ++------------------------------ 1 file changed, 2 insertions(+), 30 deletions(-) diff --git a/fault/logging.py b/fault/logging.py index 9f0f90ff..672578ce 100644 --- a/fault/logging.py +++ b/fault/logging.py @@ -1,7 +1,3 @@ -""" -Note(rsetaluri): This file is taken almost verbatim from magma/magma/logging.py. -""" - from __future__ import absolute_import from __future__ import print_function @@ -14,22 +10,6 @@ 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) @@ -38,13 +18,5 @@ 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") +def error(message, *args, **kwargs): + log.error(message, *args, **kwargs) From 2c75d680dea22e467efdeb5200382b8025a5e0f7 Mon Sep 17 00:00:00 2001 From: Raj Setaluri Date: Mon, 23 Jul 2018 12:53:39 -0700 Subject: [PATCH 3/4] Add smoke test for fault.logging --- tests/test_logging.py | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 tests/test_logging.py diff --git a/tests/test_logging.py b/tests/test_logging.py new file mode 100644 index 00000000..1a041a48 --- /dev/null +++ b/tests/test_logging.py @@ -0,0 +1,6 @@ +import fault.logging + +def test_logging_smoke(): + fault.logging.info("some info msg") + fault.logging.warning("some warning msg") + fault.logging.error("some error msg") From 4ea906f74e24c08a9545bc94a3a1d8554f58c6e5 Mon Sep 17 00:00:00 2001 From: rsetaluri Date: Mon, 23 Jul 2018 12:54:35 -0700 Subject: [PATCH 4/4] Fix missing extra line --- tests/test_logging.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_logging.py b/tests/test_logging.py index 1a041a48..a83f059a 100644 --- a/tests/test_logging.py +++ b/tests/test_logging.py @@ -1,5 +1,6 @@ import fault.logging + def test_logging_smoke(): fault.logging.info("some info msg") fault.logging.warning("some warning msg")