Skip to content

Context and logging

context

Global, thread-local-free configuration object, modelled after pwntools' context.

Example

from hwntools import context
context.log_level = "debug"
context.timeout = 5
with context.local(endian="big"):
    ...

context module-attribute

context = Context()

The global Context instance.

SerialDefaults dataclass

SerialDefaults(baudrate: int = 115200, bytesize: int = 8, parity: str = 'N', stopbits: float = 1, xonxoff: bool = False, rtscts: bool = False)

Default line settings used by hwntools.serial when not given explicitly.

Context dataclass

Context(timeout: float = 10.0, tool_timeout: float = 600.0, newline: bytes = b'\n', arch: str = 'arm', bits: int = 32, endian: Endian = 'little', serial: SerialDefaults = SerialDefaults(), _log_level: int = INFO)

Process-wide settings. Use the module level context instance.

log_level property writable

log_level: int

Current log level as an int (logging.INFO etc.).

Assign either an int or one of debug, info, warn, error, silent.

word_size property

word_size: int

Word size in bytes (bits // 8).

set_arch

set_arch(arch: str) -> None

Set arch and derive bits/endian from it.

update

update(**kwargs: Any) -> None

Set several attributes at once. arch also updates bits/endian.

local

local(**kwargs: Any) -> Iterator[Context]

Temporarily override settings inside a with block.

reset

reset() -> None

Restore every setting to its default value.

log

pwntools-style console logging: [*] info, [+] success, [!] warn, progress lines.

Messages go to stderr so that tool output on stdout stays clean. Output is filtered by context.log_level.

log module-attribute

log = Logger()

The global Logger instance.

Logger

Logger(stream: IO[str] | None = None)

Small logger with pwntools' look and feel.

debug

debug(msg: str, *args: Any) -> None

Log at debug level ([DEBUG]).

info

info(msg: str, *args: Any) -> None

Log an informational message ([*]).

success

success(msg: str, *args: Any) -> None

Log a success message ([+]).

failure

failure(msg: str, *args: Any) -> None

Log a failure message ([-]) without raising.

warn

warn(msg: str, *args: Any) -> None

Log a warning ([!]).

error

error(msg: str, *args: Any) -> NoReturn

Log an error and raise HwnError (like pwntools).

hexdump

hexdump(data: bytes, level: int = DEBUG) -> None

Log a hexdump of data at level (debug by default).

progress

progress(msg: str, status: str = '') -> Progress

Start a progress line; update it with Progress.status.

Progress

Progress(logger: Logger, msg: str, status: str = '', min_interval: float = 2.0)

A single status line that can be updated in place.

On a TTY the line is rewritten; otherwise updates are rate limited to one every few seconds so logs stay readable.

status

status(status: str, *args: Any, force: bool = False) -> None

Update the progress line (rate-limited without a terminal unless force).

success

success(status: str = 'Done', *args: Any) -> None

Finish the progress line as successful.

failure

failure(status: str = 'Failed', *args: Any) -> None

Finish the progress line as failed.