Point the tee at *path* (``txt/ .log``), or ``None`` for terminal only. The file is opened in append mode so nothing is overwritten; a one-line session header is written the first time a given log is opened this run. Reopens only when the path actually changes, so switching between
(path)
| 68 | |
| 69 | |
| 70 | def set_log_path(path) -> None: |
| 71 | """Point the tee at *path* (``txt/<name>.log``), or ``None`` for terminal only. |
| 72 | |
| 73 | The file is opened in append mode so nothing is overwritten; a one-line |
| 74 | session header is written the first time a given log is opened this run. |
| 75 | Reopens only when the path actually changes, so switching between schematics |
| 76 | keeps each log intact. |
| 77 | """ |
| 78 | global _logfile, _log_path |
| 79 | path = Path(path) if path is not None else None |
| 80 | if path == _log_path: |
| 81 | return |
| 82 | |
| 83 | if _logfile is not None: |
| 84 | try: |
| 85 | _logfile.close() |
| 86 | except OSError: |
| 87 | pass |
| 88 | _logfile = None |
| 89 | _log_path = path |
| 90 | if path is None: |
| 91 | return |
| 92 | |
| 93 | try: |
| 94 | path.parent.mkdir(parents=True, exist_ok=True) # create txt/ if missing |
| 95 | first = path not in _opened |
| 96 | _logfile = open(path, "a", buffering=1, encoding="utf-8") |
| 97 | _opened.add(path) |
| 98 | if first: |
| 99 | _logfile.write( |
| 100 | f"\n===== SLiCAP schematic session " |
| 101 | f"{datetime.now():%Y-%m-%d %H:%M:%S} =====\n" |
| 102 | ) |
| 103 | _logfile.flush() |
| 104 | except OSError: |
| 105 | _logfile = None |