asyncio: remove master.add_log, in favor of a persistent log.Log instance

This commit is contained in:
Aldo Cortesi 2018-04-04 14:46:29 +12:00 committed by Aldo Cortesi
parent ea213782d8
commit 6a08ef465f
6 changed files with 12 additions and 25 deletions

View File

@ -41,8 +41,8 @@ class StreamLog:
"""
A class for redirecting output using contextlib.
"""
def __init__(self, log):
self.log = log
def __init__(self, lg):
self.log = lg
def write(self, buf):
if buf.strip():
@ -55,13 +55,7 @@ class StreamLog:
@contextlib.contextmanager
def safecall():
# resolve ctx.master here.
# we want to be threadsafe, and ctx.master may already be cleared when an addon prints().
channel = ctx.master.channel
# don't use master.add_log (which is not thread-safe). Instead, put on event queue.
stdout_replacement = StreamLog(
lambda message: channel("log", log.LogEntry(message, "warn"))
)
stdout_replacement = StreamLog(lambda message: ctx.log.warn(message))
try:
with contextlib.redirect_stdout(stdout_replacement):
yield

View File

@ -1,4 +1,5 @@
import mitmproxy
from mitmproxy import ctx
class CheckCA:
@ -15,10 +16,9 @@ class CheckCA:
if has_ca:
self.failed = mitmproxy.ctx.master.server.config.certstore.default_ca.has_expired()
if self.failed:
mitmproxy.ctx.master.add_log(
ctx.log.warn(
"The mitmproxy certificate authority has expired!\n"
"Please delete all CA-related files in your ~/.mitmproxy folder.\n"
"The CA will be regenerated automatically after restarting mitmproxy.\n"
"Then make sure all your clients have the new CA installed.",
"warn",
)

View File

@ -56,6 +56,7 @@ class Master:
self._server = None
self.first_tick = True
self.waiting_flows = []
self.log = log.Log(self)
@property
def server(self):
@ -73,7 +74,7 @@ class Master:
yield
return
mitmproxy_ctx.master = self
mitmproxy_ctx.log = log.Log(self)
mitmproxy_ctx.log = self.log
mitmproxy_ctx.options = self.options
try:
yield
@ -82,12 +83,6 @@ class Master:
mitmproxy_ctx.log = None
mitmproxy_ctx.options = None
def add_log(self, e, level):
"""
level: debug, alert, info, warn, error
"""
self.addons.trigger("log", log.LogEntry(e, level))
def start(self):
self.should_exit.clear()
if self.server:

View File

@ -121,7 +121,7 @@ class FlowDetails(tabs.Tabs):
viewmode, message
)
if error:
self.master.add_log(error, "debug")
self.master.log.debug(error)
# Give hint that you have to tab for the response.
if description == "No content" and isinstance(message, http.HTTPRequest):
description = "No request content (press tab to view response)"

View File

@ -114,17 +114,15 @@ class WebMaster(master.Master):
iol.add_callback(self.start)
web_url = "http://{}:{}/".format(self.options.web_iface, self.options.web_port)
self.add_log(
"Web server listening at {}".format(web_url),
"info"
self.log.info(
"Web server listening at {}".format(web_url),
)
if self.options.web_open_browser:
success = open_browser(web_url)
if not success:
self.add_log(
self.log.info(
"No web browser found. Please open a browser and point it to {}".format(web_url),
"info"
)
try:
iol.start()

View File

@ -45,7 +45,7 @@ class TestApp(tornado.testing.AsyncHTTPTestCase):
f.id = "42"
m.view.add([f])
m.view.add([tflow.tflow(err=True)])
m.add_log("test log", "info")
m.log.info("test log")
self.master = m
self.view = m.view
self.events = m.events