-
Notifications
You must be signed in to change notification settings - Fork 6.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add /logs/raw and /logs/subscribe for getting logs on frontend Hijacks stderr/stdout to send all output data to the client on flush * Use existing send sync method * Fix get_logs should return string * Fix bug * pass no server * fix tests * Fix output flush on linux
- Loading branch information
1 parent
dd5b57e
commit 6ee066a
Showing
5 changed files
with
131 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
from app.logger import on_flush | ||
import os | ||
|
||
|
||
class TerminalService: | ||
def __init__(self, server): | ||
self.server = server | ||
self.cols = None | ||
self.rows = None | ||
self.subscriptions = set() | ||
on_flush(self.send_messages) | ||
|
||
def update_size(self): | ||
sz = os.get_terminal_size() | ||
changed = False | ||
if sz.columns != self.cols: | ||
self.cols = sz.columns | ||
changed = True | ||
|
||
if sz.lines != self.rows: | ||
self.rows = sz.lines | ||
changed = True | ||
|
||
if changed: | ||
return {"cols": self.cols, "rows": self.rows} | ||
|
||
return None | ||
|
||
def subscribe(self, client_id): | ||
self.subscriptions.add(client_id) | ||
|
||
def unsubscribe(self, client_id): | ||
self.subscriptions.discard(client_id) | ||
|
||
def send_messages(self, entries): | ||
if not len(entries) or not len(self.subscriptions): | ||
return | ||
|
||
new_size = self.update_size() | ||
|
||
for client_id in self.subscriptions.copy(): # prevent: Set changed size during iteration | ||
if client_id not in self.server.sockets: | ||
# Automatically unsub if the socket has disconnected | ||
self.unsubscribe(client_id) | ||
continue | ||
|
||
self.server.send_sync("logs", {"entries": entries, "size": new_size}, client_id) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,73 @@ | ||
import logging | ||
from logging.handlers import MemoryHandler | ||
from collections import deque | ||
from datetime import datetime | ||
import io | ||
import logging | ||
import sys | ||
import threading | ||
|
||
logs = None | ||
formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s") | ||
stdout_interceptor = None | ||
stderr_interceptor = None | ||
|
||
|
||
class LogInterceptor(io.TextIOWrapper): | ||
def __init__(self, stream, *args, **kwargs): | ||
buffer = stream.buffer | ||
encoding = stream.encoding | ||
super().__init__(buffer, *args, **kwargs, encoding=encoding, line_buffering=stream.line_buffering) | ||
self._lock = threading.Lock() | ||
self._flush_callbacks = [] | ||
self._logs_since_flush = [] | ||
|
||
def write(self, data): | ||
entry = {"t": datetime.now().isoformat(), "m": data} | ||
with self._lock: | ||
self._logs_since_flush.append(entry) | ||
|
||
# Simple handling for cr to overwrite the last output if it isnt a full line | ||
# else logs just get full of progress messages | ||
if isinstance(data, str) and data.startswith("\r") and not logs[-1]["m"].endswith("\n"): | ||
logs.pop() | ||
logs.append(entry) | ||
super().write(data) | ||
|
||
def flush(self): | ||
super().flush() | ||
for cb in self._flush_callbacks: | ||
cb(self._logs_since_flush) | ||
self._logs_since_flush = [] | ||
|
||
def on_flush(self, callback): | ||
self._flush_callbacks.append(callback) | ||
|
||
|
||
def get_logs(): | ||
return "\n".join([formatter.format(x) for x in logs]) | ||
return logs | ||
|
||
|
||
def on_flush(callback): | ||
if stdout_interceptor is not None: | ||
stdout_interceptor.on_flush(callback) | ||
if stderr_interceptor is not None: | ||
stderr_interceptor.on_flush(callback) | ||
|
||
def setup_logger(log_level: str = 'INFO', capacity: int = 300): | ||
global logs | ||
if logs: | ||
return | ||
|
||
# Override output streams and log to buffer | ||
logs = deque(maxlen=capacity) | ||
|
||
global stdout_interceptor | ||
global stderr_interceptor | ||
stdout_interceptor = sys.stdout = LogInterceptor(sys.stdout) | ||
stderr_interceptor = sys.stderr = LogInterceptor(sys.stderr) | ||
|
||
# Setup default global logger | ||
logger = logging.getLogger() | ||
logger.setLevel(log_level) | ||
|
||
stream_handler = logging.StreamHandler() | ||
stream_handler.setFormatter(logging.Formatter("%(message)s")) | ||
logger.addHandler(stream_handler) | ||
|
||
# Create a memory handler with a deque as its buffer | ||
logs = deque(maxlen=capacity) | ||
memory_handler = MemoryHandler(capacity, flushLevel=logging.INFO) | ||
memory_handler.buffer = logs | ||
memory_handler.setFormatter(formatter) | ||
logger.addHandler(memory_handler) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters