diff options
| author | Aldo Cortesi <aldo@nullcube.com> | 2018-04-04 07:28:43 +1200 |
|---|---|---|
| committer | Aldo Cortesi <aldo@corte.si> | 2018-04-07 09:02:10 +1200 |
| commit | 80f2bac356dd8abc50a28e4c8d9951a60979d168 (patch) | |
| tree | 1d60ebbf513b256bac0049253b6dd942c8bd9947 /mitmproxy | |
| parent | 0fa1280daa94729defa8411d86266bd2b52ad0b6 (diff) | |
| download | mitmproxy-80f2bac356dd8abc50a28e4c8d9951a60979d168.tar.gz mitmproxy-80f2bac356dd8abc50a28e4c8d9951a60979d168.tar.bz2 mitmproxy-80f2bac356dd8abc50a28e4c8d9951a60979d168.zip | |
asyncio: move log mechanism onto the event loop
Logs are now asynchronous, with a log entry pushed onto the event loop for
handling. To support this, the test mechanism grows an await_log method that
waits for a log entry to appear.
Diffstat (limited to 'mitmproxy')
| -rw-r--r-- | mitmproxy/addonmanager.py | 1 | ||||
| -rw-r--r-- | mitmproxy/log.py | 6 | ||||
| -rw-r--r-- | mitmproxy/master.py | 1 | ||||
| -rw-r--r-- | mitmproxy/test/taddons.py | 9 |
4 files changed, 14 insertions, 3 deletions
diff --git a/mitmproxy/addonmanager.py b/mitmproxy/addonmanager.py index a83b0e68..418ab046 100644 --- a/mitmproxy/addonmanager.py +++ b/mitmproxy/addonmanager.py @@ -3,7 +3,6 @@ import typing import traceback import contextlib import sys -import asyncio from mitmproxy import exceptions from mitmproxy import eventsequence diff --git a/mitmproxy/log.py b/mitmproxy/log.py index d2988011..30dbb473 100644 --- a/mitmproxy/log.py +++ b/mitmproxy/log.py @@ -1,3 +1,5 @@ +import asyncio + class LogEntry: def __init__(self, msg, level): @@ -54,7 +56,9 @@ class Log: self(txt, "error") def __call__(self, text, level="info"): - self.master.add_log(text, level) + asyncio.get_event_loop().call_soon( + self.master.addons.trigger, "log", LogEntry(text, level) + ) LogTierOrder = [ diff --git a/mitmproxy/master.py b/mitmproxy/master.py index b233aa97..be639dcd 100644 --- a/mitmproxy/master.py +++ b/mitmproxy/master.py @@ -49,7 +49,6 @@ class Master: asyncio.get_event_loop(), self.should_exit, ) - asyncio.ensure_future(self.tick()) self.options = opts or options.Options() # type: options.Options self.commands = command.CommandManager(self) diff --git a/mitmproxy/test/taddons.py b/mitmproxy/test/taddons.py index 82a935d2..5cef80f1 100644 --- a/mitmproxy/test/taddons.py +++ b/mitmproxy/test/taddons.py @@ -1,4 +1,5 @@ import contextlib +import asyncio import sys import mitmproxy.master @@ -42,6 +43,14 @@ class RecordingMaster(mitmproxy.master.Master): return True return False + async def await_log(self, txt, level=None): + for i in range(20): + if self.has_log(txt, level): + return True + else: + await asyncio.sleep(0.1) + return False + def has_event(self, name): for i in self.events: if i[0] == name: |
