[sans-io] improve logging

This commit is contained in:
Maximilian Hils 2017-08-07 03:09:13 +02:00
parent 967d48bdfa
commit 47e83d73cb
5 changed files with 24 additions and 8 deletions

View File

@ -29,6 +29,8 @@ class ProxyConnectionHandler(server.ConnectionHandler):
super().__init__(r, w, options) super().__init__(r, w, options)
async def handle_hook(self, hook: commands.Hook) -> None: async def handle_hook(self, hook: commands.Hook) -> None:
if isinstance(hook, commands.Log):
return # FIXME: these are already logged at the server, the "real" log messes up order.
q = asyncio.Queue() q = asyncio.Queue()
submit = lambda x: self.loop.call_soon_threadsafe(lambda: q.put_nowait(x)) submit = lambda x: self.loop.call_soon_threadsafe(lambda: q.put_nowait(x))
hook.data.reply = AsyncReply(submit, hook.data) hook.data.reply = AsyncReply(submit, hook.data)

View File

@ -81,8 +81,16 @@ class Hook(Command):
class Log(Hook): class Log(Hook):
blocking = False
# this is more of a hack at the moment.
def __init__(self, *args, level="info"): def __init__(self, *args, level="info"):
super().__init__("log", log.LogEntry(repr(args), level)) if len(args) == 1:
args = args[0]
super().__init__("log", log.LogEntry(str(args), level))
def __repr__(self):
return f"Log: {self.data.msg}"
TCommandGenerator = typing.Generator[Command, typing.Any, None] TCommandGenerator = typing.Generator[Command, typing.Any, None]

View File

@ -108,6 +108,9 @@ class NextLayer(Layer):
self.events = [] self.events = []
self.layer = None self.layer = None
def __repr__(self):
return f"NextLayer:{repr(self.layer)}"
def _handle_event(self, event: mevents.Event): def _handle_event(self, event: mevents.Event):
self.events.append(event) self.events.append(event)

View File

@ -33,7 +33,7 @@ class HTTPLayer(Layer):
def log_event(orig): def log_event(orig):
def next_event(): def next_event():
e = orig() e = orig()
print(e, file=sys.__stdout__) yield commands.Log(str(e))
return e return e
return next_event return next_event
@ -46,6 +46,8 @@ class HTTPLayer(Layer):
@expect(events.Start, events.DataReceived, events.ConnectionClosed) @expect(events.Start, events.DataReceived, events.ConnectionClosed)
def _handle_event(self, event: events.Event) -> commands.TCommandGenerator: def _handle_event(self, event: events.Event) -> commands.TCommandGenerator:
if isinstance(event, events.Start):
return
if isinstance(event, events.DataReceived): if isinstance(event, events.DataReceived):
if event.connection == self.context.client: if event.connection == self.context.client:
self.client_conn.receive_data(event.data) self.client_conn.receive_data(event.data)
@ -57,7 +59,7 @@ class HTTPLayer(Layer):
yield from self.state() yield from self.state()
def read_request_headers(self): def read_request_headers(self):
event = self.client_conn.next_event() event = yield from self.client_conn.next_event()
if event is h11.NEED_DATA: if event is h11.NEED_DATA:
return return
elif isinstance(event, h11.Request): elif isinstance(event, h11.Request):
@ -75,7 +77,7 @@ class HTTPLayer(Layer):
def read_request_body(self): def read_request_body(self):
while True: while True:
event = self.client_conn.next_event() event = yield from self.client_conn.next_event()
if event is h11.NEED_DATA: if event is h11.NEED_DATA:
return return
elif isinstance(event, h11.Data): elif isinstance(event, h11.Data):
@ -102,7 +104,7 @@ class HTTPLayer(Layer):
self.state = self.read_response_headers self.state = self.read_response_headers
def read_response_headers(self): def read_response_headers(self):
event = self.server_conn.next_event() event = yield from self.server_conn.next_event()
if event is h11.NEED_DATA: if event is h11.NEED_DATA:
return return
elif isinstance(event, h11.Response): elif isinstance(event, h11.Response):
@ -123,7 +125,7 @@ class HTTPLayer(Layer):
def read_response_body(self): def read_response_body(self):
while True: while True:
event = self.server_conn.next_event() event = yield from self.server_conn.next_event()
if event is h11.NEED_DATA: if event is h11.NEED_DATA:
return return
elif isinstance(event, h11.Data): elif isinstance(event, h11.Data):

View File

@ -73,7 +73,7 @@ class ConnectionHandler(metaclass=abc.ABCMeta):
reader, writer = self.transports[connection] reader, writer = self.transports[connection]
while True: while True:
try: try:
data = await reader.read(4096) data = await reader.read(65535)
except socket.error: except socket.error:
data = b"" data = b""
if data: if data:
@ -129,6 +129,7 @@ class SimpleConnectionHandler(ConnectionHandler):
"""Simple handler that does not process any hooks.""" """Simple handler that does not process any hooks."""
async def handle_hook(self, hook: commands.Hook) -> None: async def handle_hook(self, hook: commands.Hook) -> None:
if hook.blocking:
self.server_event(events.HookReply(hook, None)) self.server_event(events.HookReply(hook, None))