-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path_writers.py
More file actions
118 lines (101 loc) · 3.99 KB
/
_writers.py
File metadata and controls
118 lines (101 loc) · 3.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
"""Internal logging writers for stdout/stderr redirection."""
import logging
from typing import TextIO
from uipath.runtime.logging._context import current_execution_id
class LoggerWriter:
"""Redirect stdout/stderr to logging system.
Maintains per-execution-context buffers so that concurrent async tasks
(e.g. parallel eval runs) do not interleave partial lines.
"""
def __init__(
self,
logger: logging.Logger,
level: int,
min_level: int,
sys_file: TextIO,
):
"""Initialize the LoggerWriter."""
self.logger = logger
self.level = level
self.min_level = min_level
# Keyed by current_execution_id (None for master context).
# A single shared buffer would interleave partial lines from
# concurrent async tasks writing to the same sys.stdout.
self._buffers: dict[str | None, str] = {}
self.sys_file = sys_file
self._in_logging = False # Recursion guard
def write(self, message: str) -> None:
"""Write message to the logger, buffering until newline."""
# Prevent infinite recursion when logging.handleError writes to stderr
if self._in_logging:
if self.sys_file:
try:
self.sys_file.write(message)
except (OSError, IOError):
pass # Fail silently if we can't write
return
try:
self._in_logging = True
ctx = current_execution_id.get()
buf = self._buffers.get(ctx, "") + message
while "\n" in buf:
line, buf = buf.split("\n", 1)
# Only log if the message is not empty and the level is sufficient
if line and self.level >= self.min_level:
self.logger._log(self.level, line, ())
if buf:
self._buffers[ctx] = buf
else:
self._buffers.pop(ctx, None)
finally:
self._in_logging = False
def flush(self) -> None:
"""Flush the current execution context's buffered messages to the logger."""
if self._in_logging:
if self.sys_file:
try:
self.sys_file.flush()
except (OSError, IOError):
pass # Fail silently if we can't flush
return
try:
self._in_logging = True
ctx = current_execution_id.get()
buf = self._buffers.pop(ctx, "")
if buf and self.level >= self.min_level:
self.logger._log(self.level, buf, ())
finally:
self._in_logging = False
def flush_all(self) -> None:
"""Flush all execution contexts' buffered messages. Called by master teardown.
Intentionally ignores current_execution_id — iterates all keys
directly so that no context's partial lines are lost.
"""
if self._in_logging:
return
try:
self._in_logging = True
for buf in self._buffers.values():
if buf and self.level >= self.min_level:
self.logger._log(self.level, buf, ())
self._buffers.clear()
finally:
self._in_logging = False
def fileno(self) -> int:
"""Get the file descriptor of the original sys.stdout/sys.stderr."""
try:
return self.sys_file.fileno()
except Exception:
return -1
def isatty(self) -> bool:
"""Check if the original sys.stdout/sys.stderr is a TTY."""
try:
return hasattr(self.sys_file, "isatty") and self.sys_file.isatty()
except (AttributeError, OSError, ValueError):
return False
def writable(self) -> bool:
"""Check if the original sys.stdout/sys.stderr is writable."""
return True
def __getattr__(self, name):
"""Delegate attribute access to the original sys.stdout/sys.stderr."""
return getattr(self.sys_file, name)