-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalog.py
28 lines (22 loc) · 846 Bytes
/
alog.py
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
from logging import DEBUG
import asyncio
import sys
OUTPUT = None
MIN_LEVEL = DEBUG
async def init(min_level = DEBUG, stream=sys.stdout):
global MIN_LEVEL
global OUTPUT
MIN_LEVEL = min_level
loop = asyncio.get_event_loop()
reader = asyncio.StreamReader()
protocol = asyncio.StreamReaderProtocol(reader)
await loop.connect_read_pipe(lambda: protocol, sys.stdin)
w_transport, w_protocol = await loop.connect_write_pipe(asyncio.streams.FlowControlMixin, stream)
OUTPUT = asyncio.StreamWriter(w_transport, w_protocol, reader, loop)
async def log(level, message):
if level >= MIN_LEVEL:
OUTPUT.write(f"{asyncio.get_event_loop().time()} {message}\n".encode())
await OUTPUT.drain()
def log_no_wait(level, message):
if level >= MIN_LEVEL:
asyncio.create_task(log(level, message))