Spaces:
Running
Running
stream from printed output
Browse files
src/backend/gradio_logsview/logsview.py
CHANGED
@@ -1,11 +1,13 @@
|
|
1 |
"""LogsView() custom component"""
|
|
|
2 |
|
|
|
3 |
import logging
|
4 |
import queue
|
5 |
import subprocess
|
6 |
import time
|
7 |
import traceback
|
8 |
-
from contextlib import contextmanager
|
9 |
from dataclasses import dataclass
|
10 |
from datetime import datetime
|
11 |
from functools import wraps
|
@@ -100,8 +102,8 @@ class LogsViewRunner:
|
|
100 |
|
101 |
def _log(record: logging.LogRecord) -> bool:
|
102 |
"""Handle log record and return True if log should be yielded."""
|
103 |
-
if record.thread != self.thread.ident:
|
104 |
-
|
105 |
if logger_name and not record.name.startswith(logger_name):
|
106 |
return False # Skip if not from the logger
|
107 |
if record.levelno < log_level:
|
@@ -113,17 +115,26 @@ class LogsViewRunner:
|
|
113 |
)
|
114 |
return True
|
115 |
|
|
|
|
|
116 |
with capture_logging(log_level) as log_queue:
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
while
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
127 |
|
128 |
# After the thread completes, yield any remaining logs
|
129 |
while True:
|
@@ -132,6 +143,12 @@ class LogsViewRunner:
|
|
132 |
yield self.logs
|
133 |
except queue.Empty:
|
134 |
break
|
|
|
|
|
|
|
|
|
|
|
|
|
135 |
|
136 |
try:
|
137 |
error = error_queue.get_nowait()
|
@@ -287,3 +304,21 @@ def non_failing_fn(fn: Callable, *args, **kwargs) -> Callable:
|
|
287 |
error_queue.put(e)
|
288 |
|
289 |
return error_queue, _inner
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
"""LogsView() custom component"""
|
2 |
+
from __future__ import annotations
|
3 |
|
4 |
+
import io
|
5 |
import logging
|
6 |
import queue
|
7 |
import subprocess
|
8 |
import time
|
9 |
import traceback
|
10 |
+
from contextlib import contextmanager, redirect_stdout
|
11 |
from dataclasses import dataclass
|
12 |
from datetime import datetime
|
13 |
from functools import wraps
|
|
|
102 |
|
103 |
def _log(record: logging.LogRecord) -> bool:
|
104 |
"""Handle log record and return True if log should be yielded."""
|
105 |
+
# if record.thread != self.thread.ident:
|
106 |
+
# return False # Skip if not from the thread
|
107 |
if logger_name and not record.name.startswith(logger_name):
|
108 |
return False # Skip if not from the logger
|
109 |
if record.levelno < log_level:
|
|
|
115 |
)
|
116 |
return True
|
117 |
|
118 |
+
stdout_queue = queue.Queue()
|
119 |
+
stream = LineQueueStream(stdout_queue)
|
120 |
with capture_logging(log_level) as log_queue:
|
121 |
+
with redirect_stdout(stream):
|
122 |
+
# Start thread and loop to capture and yield logs from the thread
|
123 |
+
self.thread.start()
|
124 |
+
while self.thread.is_alive():
|
125 |
+
while True:
|
126 |
+
try:
|
127 |
+
if _log(log_queue.get_nowait()):
|
128 |
+
yield self.logs
|
129 |
+
except queue.Empty:
|
130 |
+
break
|
131 |
+
while True:
|
132 |
+
try:
|
133 |
+
line = stdout_queue.get_nowait()
|
134 |
+
yield self.log(line)
|
135 |
+
except queue.Empty:
|
136 |
+
break
|
137 |
+
self.thread.join(timeout=0.1) # adjust the timeout as needed
|
138 |
|
139 |
# After the thread completes, yield any remaining logs
|
140 |
while True:
|
|
|
143 |
yield self.logs
|
144 |
except queue.Empty:
|
145 |
break
|
146 |
+
while True:
|
147 |
+
try:
|
148 |
+
line = stdout_queue.get_nowait()
|
149 |
+
yield self.log(line)
|
150 |
+
except queue.Empty:
|
151 |
+
break
|
152 |
|
153 |
try:
|
154 |
error = error_queue.get_nowait()
|
|
|
304 |
error_queue.put(e)
|
305 |
|
306 |
return error_queue, _inner
|
307 |
+
|
308 |
+
class LineQueueStream(io.StringIO):
|
309 |
+
def __init__(self, queue: queue.Queue, *args, **kwargs):
|
310 |
+
super().__init__(*args, **kwargs)
|
311 |
+
self._queue = queue
|
312 |
+
self._current_line = ""
|
313 |
+
|
314 |
+
def write(self, s: str) -> int:
|
315 |
+
if "\n" in s:
|
316 |
+
lines = s.split("\n")
|
317 |
+
for line in lines[:-1]:
|
318 |
+
self._current_line += line
|
319 |
+
self._queue.put(self._current_line)
|
320 |
+
self._current_line = ""
|
321 |
+
self._current_line += lines[-1]
|
322 |
+
else:
|
323 |
+
self._current_line += s
|
324 |
+
return super().write(s)
|