Skip to content

Commit c23c69d

Browse files
authored
Merge pull request #177 from Pennycook/feature/debug
Add `--debug` option
2 parents 5c81ed5 + 0fd1c27 commit c23c69d

File tree

2 files changed

+31
-4
lines changed

2 files changed

+31
-4
lines changed

codebasin/__main__.py

+26-4
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
"""
@@ -89,6 +91,14 @@ def _main():
8991
default=0,
9092
help=_help_string("Decrease verbosity level."),
9193
)
94+
parser.add_argument(
95+
"--debug",
96+
dest="debug",
97+
action="store_const",
98+
default=False,
99+
const=True,
100+
help=_help_string("Enable debug output."),
101+
)
92102
parser.add_argument(
93103
"-R",
94104
"--report",
@@ -152,23 +162,35 @@ def _main():
152162
# Configure logging such that:
153163
# - All messages are written to a log file
154164
# - Only errors are written to the terminal by default
155-
# - Messages written to terminal are based on -q and -v flags
165+
# - Messages written to terminal are based on -q, -v and --debug flags
156166
# - Meta-warnings and statistics are generated by a WarningAggregator
157167
aggregator = WarningAggregator()
158168
log.setLevel(logging.DEBUG)
169+
if args.debug:
170+
min_log_level = logging.DEBUG
171+
global _traceback
172+
_traceback = True
173+
else:
174+
min_log_level = logging.INFO
159175

160176
file_handler = logging.FileHandler("cbi.log", mode="w")
161-
file_handler.setLevel(logging.INFO)
177+
file_handler.setLevel(min_log_level)
162178
file_handler.setFormatter(Formatter())
163179
file_handler.addFilter(aggregator)
164180
log.addHandler(file_handler)
165181

182+
if args.debug:
183+
log.debug(f"Code Base Investigator {version}; Python {sys.version}")
184+
166185
# Inform the user that a log file has been created.
167186
# 'print' instead of 'log' to ensure the message is visible in the output.
168187
log_path = os.path.abspath("cbi.log")
169188
print(f"Log file created at {log_path}")
170189

171-
log_level = max(1, logging.ERROR - 10 * (args.verbose - args.quiet))
190+
log_level = max(
191+
min_log_level,
192+
logging.ERROR - 10 * (args.verbose - args.quiet),
193+
)
172194
stdout_handler = logging.StreamHandler(sys.stdout)
173195
stdout_handler.setLevel(log_level)
174196
stdout_handler.setFormatter(Formatter(colors=sys.stdout.isatty()))
@@ -266,7 +288,7 @@ def main():
266288
try:
267289
_main()
268290
except Exception as e:
269-
log.error(str(e))
291+
log.error(str(e), exc_info=_traceback)
270292
sys.exit(1)
271293

272294

codebasin/_detail/logging.py

+5
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)