Store logging. This will remove all other logging handlers, and return the handler to stdout when complete.
| 309 | |
| 310 | |
| 311 | class catch_logging: |
| 312 | """Store logging. |
| 313 | |
| 314 | This will remove all other logging handlers, and return the handler to |
| 315 | stdout when complete. |
| 316 | """ |
| 317 | |
| 318 | def __init__(self, verbose=None): |
| 319 | self.verbose = verbose |
| 320 | |
| 321 | def __enter__(self): # noqa: D105 |
| 322 | if self.verbose is not None: |
| 323 | self._ctx = use_log_level(self.verbose) |
| 324 | else: |
| 325 | self._ctx = contextlib.nullcontext() |
| 326 | self._data = ClosingStringIO() |
| 327 | self._lh = logging.StreamHandler(self._data) |
| 328 | self._lh.setFormatter(logging.Formatter("%(message)s")) |
| 329 | self._lh._mne_file_like = True # monkey patch for warn() use |
| 330 | _remove_close_handlers(logger) |
| 331 | logger.addHandler(self._lh) |
| 332 | self._ctx.__enter__() |
| 333 | return self._data |
| 334 | |
| 335 | def __exit__(self, *args): # noqa: D105 |
| 336 | self._ctx.__exit__(*args) |
| 337 | logger.removeHandler(self._lh) |
| 338 | set_log_file(None) |
| 339 | |
| 340 | |
| 341 | @contextlib.contextmanager |
no outgoing calls