mirror of
https://github.com/zhenxun-org/zhenxun_bot.git
synced 2025-12-15 14:22:55 +08:00
40 lines
982 B
Python
40 lines
982 B
Python
import asyncio
|
|
import re
|
|
from typing import Awaitable, Callable, Dict, Generic, List, Set, TypeVar
|
|
from urllib.parse import urlparse
|
|
|
|
PATTERN = r"\x1b(\[.*?[@-~]|\].*?(\x07|\x1b\\))"
|
|
|
|
_T = TypeVar("_T")
|
|
LogListener = Callable[[_T], Awaitable[None]]
|
|
|
|
|
|
class LogStorage(Generic[_T]):
|
|
|
|
"""
|
|
日志存储
|
|
"""
|
|
|
|
def __init__(self, rotation: float = 5 * 60):
|
|
self.count, self.rotation = 0, rotation
|
|
self.logs: Dict[int, str] = {}
|
|
self.listeners: Set[LogListener[str]] = set()
|
|
|
|
async def add(self, log: str):
|
|
seq = self.count = self.count + 1
|
|
self.logs[seq] = log
|
|
asyncio.get_running_loop().call_later(self.rotation, self.remove, seq)
|
|
await asyncio.gather(
|
|
*map(lambda listener: listener(log), self.listeners),
|
|
return_exceptions=True,
|
|
)
|
|
return seq
|
|
|
|
def remove(self, seq: int):
|
|
del self.logs[seq]
|
|
return
|
|
|
|
|
|
LOG_STORAGE: LogStorage[str] = LogStorage[str]()
|
|
|