clearex.io.log

Functions

capture_c_level_output(func, *args, **kwargs)

Capture Python and C-level stdout/stderr from a function call.

initialize_logging(log_directory[, ...])

Initialize logging if not already configured.

initiate_logger(log_directory)

Set up logging to a file in the specified directory.

clearex.io.log.initialize_logging(log_directory, enable_logging=False)

Initialize logging if not already configured.

This function checks if the root logger has any handlers configured. If not, it initializes logging to write to a log file in the specified directory. If logging is already configured, it simply returns the existing root logger.

Parameters:
  • log_directory (os.PathLike[str] | str | None) – The directory path where the log file should be created if logging is not already configured.

  • enable_logging (bool | None, optional) – Flag indicating whether to initialize logging if it is not already set up.

Returns:

The root logger instance.

Return type:

logging.Logger

clearex.io.log.initiate_logger(log_directory)

Set up logging to a file in the specified directory.

This function configures the root logger to write log messages to a file located in the specified base path. The log file is named with a timestamp to ensure uniqueness. The logging level is set to INFO, and the log format includes timestamps.

Parameters:

log_directory (str) – The directory where the log file will be created.

Returns:

The configured root logger instance.

Return type:

logging.Logger

clearex.io.log.capture_c_level_output(func, *args, **kwargs)

Capture Python and C-level stdout/stderr from a function call.

Parameters:
  • func (collections.abc.Callable) – Callable to execute while stdout and stderr are temporarily redirected.

  • *args (Any) – Positional arguments forwarded to func.

  • **kwargs (Any) – Keyword arguments forwarded to func.

Returns:

Tuple (result, stdout_text, stderr_text) containing the callable’s return value and the captured output streams.

Return type:

tuple

Notes

This helper redirects file descriptors, so it captures output emitted by native libraries in addition to normal Python print statements.