Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,12 @@ Use `MiniTrace.settracelengthto()` to set the length
```Python
MiniTrace.settracelengthto(10)
```
Use `MiniTrace.set_show_full_path(True)` to include file paths in the output.

Enable automatic saving of tracebacks:
```Python
MiniTrace.enable_auto_save("trace.log")
```
Disable with `MiniTrace.disable_auto_save()` when you are done.
# NOTES:
Pip does not support minitrace, you will need to download the file from raw code to activate it.
118 changes: 72 additions & 46 deletions minitrace.py
Original file line number Diff line number Diff line change
@@ -1,30 +1,50 @@
import sys
import traceback
from colorama import Fore, Style, init
from tkinter import Tk, filedialog
import os
import sys
import traceback
from colorama import Fore, Style, init
from tkinter import Tk, filedialog

init(autoreset=True)

class MiniTrace:
_trace_length = 5
class MiniTrace:
_trace_length = 5
_show_full_path = False
_auto_save = False
_auto_save_path = None

@classmethod
def settracelengthto(cls, length):
if not isinstance(length, int) or length <= 0:
raise ValueError("Trace length must be a positive integer.")
cls._trace_length = length
def settracelengthto(cls, length):
if not isinstance(length, int) or length <= 0:
raise ValueError("Trace length must be a positive integer.")
cls._trace_length = length

@classmethod
def set_show_full_path(cls, show):
cls._show_full_path = bool(show)

@classmethod
def enable_auto_save(cls, path=None):
cls._auto_save = True
cls._auto_save_path = path

@classmethod
def disable_auto_save(cls):
cls._auto_save = False
cls._auto_save_path = None

@classmethod
def format_traceback(cls, exc_type, exc_value, tb):
trace_lines = traceback.extract_tb(tb)[-cls._trace_length:]
formatted_lines = []

for idx, line in enumerate(reversed(trace_lines), 1):
code_line = line.line.strip() if line.line else "No code found"
if idx == 1:
formatted_lines.append(f"{Fore.RED}{line.lineno} >>> {code_line}{Style.RESET_ALL}")
else:
formatted_lines.append(f"{line.lineno} >>> {code_line}")
for idx, line in enumerate(reversed(trace_lines), 1):
code_line = line.line.strip() if line.line else "No code found"
filename = line.filename if cls._show_full_path else os.path.basename(line.filename)
info = f"{filename}:{line.lineno}"
if idx == 1:
formatted_lines.append(f"{Fore.RED}{info} >>> {code_line}{Style.RESET_ALL}")
else:
formatted_lines.append(f"{info} >>> {code_line}")

expectation = f"{Fore.RED}{exc_type.__name__}: {exc_value}{Style.RESET_ALL}"
formatted_lines.append(expectation)
Expand All @@ -34,10 +54,12 @@ def format_traceback(cls, exc_type, exc_value, tb):
@classmethod
def format_traceback_plain(cls, exc_type, exc_value, tb):
trace_lines = traceback.extract_tb(tb)[-cls._trace_length:]
formatted_lines = [
f"{line.lineno} >>> {line.line.strip() if line.line else 'No code found'}"
for line in reversed(trace_lines)
]
formatted_lines = []
for line in reversed(trace_lines):
code_line = line.line.strip() if line.line else 'No code found'
filename = line.filename if cls._show_full_path else os.path.basename(line.filename)
info = f"{filename}:{line.lineno}"
formatted_lines.append(f"{info} >>> {code_line}")
expectation = f"{exc_type.__name__}: {exc_value}"
formatted_lines.append(expectation)
return (
Expand All @@ -49,31 +71,35 @@ def init(cls):
sys.excepthook = cls.handle_exception

@classmethod
def handle_exception(cls, exc_type, exc_value, tb):
formatted_trace = cls.format_traceback(exc_type, exc_value, tb)
print(formatted_trace, file=sys.stderr)
try:
response = input("Would you like to save this traceback to a file? (y/n): ").strip().lower()
if response == "y":
cls.save_to_file(exc_type, exc_value, tb)
except KeyboardInterrupt:
print("\nSave operation canceled due to KeyboardInterrupt.")
def handle_exception(cls, exc_type, exc_value, tb):
formatted_trace = cls.format_traceback(exc_type, exc_value, tb)
print(formatted_trace, file=sys.stderr)
try:
if cls._auto_save:
cls.save_to_file(exc_type, exc_value, tb, cls._auto_save_path)
else:
response = input("Would you like to save this traceback to a file? (y/n): ").strip().lower()
if response == "y":
cls.save_to_file(exc_type, exc_value, tb)
except KeyboardInterrupt:
print("\nSave operation canceled due to KeyboardInterrupt.")

@classmethod
def save_to_file(cls, exc_type, exc_value, tb):
plain_traceback = cls.format_traceback_plain(exc_type, exc_value, tb)
Tk().withdraw()
file_path = filedialog.asksaveasfilename(
defaultextension=".log",
filetypes=[
("Log files", "*.log"),
("Text files", "*.txt"),
("All files", "*.*"),
]
)
if file_path:
with open(file_path, "w") as file:
file.write(plain_traceback)
print(f"Traceback saved to {file_path}")
else:
print("Save operation canceled.")
def save_to_file(cls, exc_type, exc_value, tb, path=None):
plain_traceback = cls.format_traceback_plain(exc_type, exc_value, tb)
if path is None:
Tk().withdraw()
path = filedialog.asksaveasfilename(
defaultextension=".log",
filetypes=[
("Log files", "*.log"),
("Text files", "*.txt"),
("All files", "*.*"),
]
)
if path:
with open(path, "w") as file:
file.write(plain_traceback)
print(f"Traceback saved to {path}")
else:
print("Save operation canceled.")
70 changes: 0 additions & 70 deletions minitracelegacy1.py

This file was deleted.