logging

Utility functions for logging.

Classes

TeeStream

Mirror a text stream to sink while passing writes through to stream.

Functions

atomic_print

Decorator to prevent interleaved output in distributed/multi-threaded environments.

capture_io

Capture stdout and stderr within the invoked context.

no_stdout

Silences stdout within the invoked context.

num2hrb

Convert big floating number to human readable string.

print_args

Pretty-print an argparse.Namespace (one entry per line) on rank 0.

print_rank_0

Prints only on the master process.

silence_matched_warnings

Silences warnings that match a given pattern.

warn_rank_0

Issues a warning only on the master process.

exception DeprecatedError

Bases: NotImplementedError

Error for deprecated functions.

class TeeStream

Bases: object

Mirror a text stream to sink while passing writes through to stream.

Scripts that report progress with bare print() have no log file; wrapping sys.stdout/sys.stderr in this is what produces one. Attribute access falls through to the wrapped stream so isatty() keeps progress bars behaving. Native (C-level) writes go straight to the real file descriptor and are not captured.

__init__(stream, sink)

Wrap stream, mirroring everything written to it into the open file sink.

flush()

Flush both the original stream and the sink.

Return type:

None

write(data)

Write to both the original stream and the sink.

Parameters:

data (str)

Return type:

int

atomic_print(func)

Decorator to prevent interleaved output in distributed/multi-threaded environments.

capture_io(capture_stderr=True)

Capture stdout and stderr within the invoked context.

Parameters:

capture_stderr (bool) – Whether to capture stderr. Defaults to True.

Returns:

An iterator that yields a StringIO object that contains the captured output.

Return type:

Iterator[StringIO]

Example:

with capture_io() as buf:
    print("Hello, world!")
print(buf.getvalue())
no_stdout()

Silences stdout within the invoked context.

num2hrb(num, suffix='')

Convert big floating number to human readable string.

Parameters:

num (float)

Return type:

str

print_args(args, title='Arguments')

Pretty-print an argparse.Namespace (one entry per line) on rank 0.

Parameters:
  • args (Namespace)

  • title (str)

Return type:

None

print_rank_0(*args, **kwargs)

Prints only on the master process.

silence_matched_warnings(pattern=None)

Silences warnings that match a given pattern.

Parameters:

pattern (str) – The pattern to match against warning messages. Defaults to None.

warn_rank_0(message, *args, **kwargs)

Issues a warning only on the master process.

Auto-bumps stacklevel by 1 to skip this wrapper frame, so callers can pass the same stacklevel they would to warnings.warn directly and the warning still points at the user’s call site.