Wrap stdout writes to logger.info, with an optional indent prefix. Parameters ---------- indent : str The indentation to add. cull_newlines : bool If True, cull any new/blank lines at the end.
(indent="", cull_newlines=False)
| 474 | |
| 475 | @contextlib.contextmanager |
| 476 | def wrapped_stdout(indent="", cull_newlines=False): |
| 477 | """Wrap stdout writes to logger.info, with an optional indent prefix. |
| 478 | |
| 479 | Parameters |
| 480 | ---------- |
| 481 | indent : str |
| 482 | The indentation to add. |
| 483 | cull_newlines : bool |
| 484 | If True, cull any new/blank lines at the end. |
| 485 | """ |
| 486 | orig_stdout = sys.stdout |
| 487 | my_out = ClosingStringIO() |
| 488 | sys.stdout = my_out |
| 489 | try: |
| 490 | yield |
| 491 | finally: |
| 492 | sys.stdout = orig_stdout |
| 493 | pending_newlines = 0 |
| 494 | for line in my_out.getvalue().split("\n"): |
| 495 | if not line.strip() and cull_newlines: |
| 496 | pending_newlines += 1 |
| 497 | continue |
| 498 | for _ in range(pending_newlines): |
| 499 | logger.info("\n") |
| 500 | logger.info(indent + line) |
| 501 | |
| 502 | |
| 503 | def _frame_info(n): |
no test coverage detected