Skip to content

stacktrace

dev_tool.services.stacktrace

__all__ = ['StacktraceEntry', 'StacktraceService'] module-attribute

StacktraceEntry dataclass

A data class representing a stored stacktrace entry.

This class contains the stacktrace content and metadata.

content instance-attribute

message instance-attribute

timestamp instance-attribute

StacktraceService

A thread-safe singleton service for storing stacktraces from a session.

This class provides centralized stacktrace storage with support for adding, retrieving, and clearing stacktraces.

The constructor for the StacktraceService class.

Initializes the service with an empty stacktrace list.

Source code in dev_tool/services/stacktrace/service.py
def __init__(self) -> None:
    """
    The constructor for the StacktraceService class.

    Initializes the service with an empty stacktrace list.
    """

    if hasattr(self, '_initialized'):
        return

    self._entries: list[StacktraceEntry] = []
    self._initialized = True
    self._lock = threading.Lock()

__new__

A class method that creates a singleton instance with thread safety.

Returns:

  • Self

    The singleton StacktraceService instance.

Source code in dev_tool/services/stacktrace/service.py
def __new__(cls) -> Self:
    """
    A class method that creates a singleton instance with thread safety.

    :return: The singleton StacktraceService instance.
    """

    if cls._instance is None:
        with cls._lock:
            if cls._instance is None:
                cls._instance = super().__new__(cls)

    assert isinstance(cls._instance, cls)
    return cls._instance

add

A method that adds a stacktrace to the storage.

Parameters:

  • content (str) –

    The stacktrace content.

  • message (str) –

    The error message associated with the stacktrace.

Source code in dev_tool/services/stacktrace/service.py
def add(self, content: str, message: str) -> None:
    """
    A method that adds a stacktrace to the storage.

    :param content: The stacktrace content.
    :param message: The error message associated with the stacktrace.
    """

    entry = StacktraceEntry(
        content=content,
        message=message,
        timestamp=time.time()
    )

    with self._lock:
        self._entries.append(entry)

clear

A method that clears all stored stacktraces.

Source code in dev_tool/services/stacktrace/service.py
def clear(self) -> None:
    """A method that clears all stored stacktraces."""

    with self._lock:
        self._entries.clear()

get

A method that gets a stacktrace by index.

Parameters:

  • index (int) –

    The index of the stacktrace.

Returns:

Source code in dev_tool/services/stacktrace/service.py
def get(self, index: int) -> StacktraceEntry | None:
    """
    A method that gets a stacktrace by index.

    :param index: The index of the stacktrace.
    :return: The stacktrace entry, or None if not found.
    """

    with self._lock:
        if 0 <= index < len(self._entries):
            return self._entries[index]

        return None

get_all

A method that gets all stored stacktraces.

Returns:

Source code in dev_tool/services/stacktrace/service.py
def get_all(self) -> list[StacktraceEntry]:
    """
    A method that gets all stored stacktraces.

    :return: A list of all stacktrace entries.
    """

    with self._lock:
        return self._entries.copy()

get_count

A method that gets the number of stored stacktraces.

Returns:

  • int

    The number of stored stacktraces.

Source code in dev_tool/services/stacktrace/service.py
def get_count(self) -> int:
    """
    A method that gets the number of stored stacktraces.

    :return: The number of stored stacktraces.
    """

    with self._lock:
        return len(self._entries)

get_latest

A method that gets the most recent stacktrace.

Returns:

  • StacktraceEntry | None

    The most recent stacktrace entry, or None if empty.

Source code in dev_tool/services/stacktrace/service.py
def get_latest(self) -> StacktraceEntry | None:
    """
    A method that gets the most recent stacktrace.

    :return: The most recent stacktrace entry, or None if empty.
    """

    with self._lock:
        if self._entries:
            return self._entries[-1]

        return None