Skip to content

Commit 307f3b2

Browse files
committed
Generate traceback when running with --debug
The logging module already has support for this via the exc_info argument. Making this work requires a small change to our custom Formatter, since it's the Formatter's job to inspect exc_info and include the traceback in the string output. Signed-off-by: John Pennycook <john.pennycook@intel.com>
1 parent 8dee2db commit 307f3b2

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

codebasin/__main__.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
log = logging.getLogger("codebasin")
1717
version = "1.2.0"
1818

19+
_traceback = False
20+
1921

2022
def _help_string(*lines: str, is_long=False, is_last=False):
2123
"""
@@ -166,6 +168,8 @@ def _main():
166168
log.setLevel(logging.DEBUG)
167169
if args.debug:
168170
min_log_level = logging.DEBUG
171+
global _traceback
172+
_traceback = True
169173
else:
170174
min_log_level = logging.INFO
171175

@@ -285,7 +289,7 @@ def main():
285289
try:
286290
_main()
287291
except Exception as e:
288-
log.error(str(e))
292+
log.error(str(e), exc_info=_traceback)
289293
sys.exit(1)
290294

291295

codebasin/_detail/logging.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ def format(self, record: logging.LogRecord) -> str:
6161
color = RED
6262
else:
6363
color = DEFAULT
64+
65+
# Print a stack trace if explicitly requested.
66+
if record.exc_info:
67+
return self.formatException(record.exc_info)
68+
6469
return f"{BOLD}{color}{level}{RESET}: {msg}"
6570

6671

0 commit comments

Comments
 (0)