diff options
author | Aldo Cortesi <aldo@nullcube.com> | 2018-04-04 14:46:29 +1200 |
---|---|---|
committer | Aldo Cortesi <aldo@corte.si> | 2018-04-07 09:02:10 +1200 |
commit | 6a08ef465f3af6cb2044baf98279de1071538f5a (patch) | |
tree | 183c5821a7a5d4debd8bd779c5596c280c48cabc /mitmproxy | |
parent | ea213782d8adaa21aa99f2de818172ee872cf3e9 (diff) | |
download | mitmproxy-6a08ef465f3af6cb2044baf98279de1071538f5a.tar.gz mitmproxy-6a08ef465f3af6cb2044baf98279de1071538f5a.tar.bz2 mitmproxy-6a08ef465f3af6cb2044baf98279de1071538f5a.zip |
asyncio: remove master.add_log, in favor of a persistent log.Log instance
Diffstat (limited to 'mitmproxy')
-rw-r--r-- | mitmproxy/addonmanager.py | 12 | ||||
-rw-r--r-- | mitmproxy/addons/check_ca.py | 4 | ||||
-rw-r--r-- | mitmproxy/master.py | 9 | ||||
-rw-r--r-- | mitmproxy/tools/console/flowview.py | 2 | ||||
-rw-r--r-- | mitmproxy/tools/web/master.py | 8 |
5 files changed, 11 insertions, 24 deletions
diff --git a/mitmproxy/addonmanager.py b/mitmproxy/addonmanager.py index 418ab046..7c446c80 100644 --- a/mitmproxy/addonmanager.py +++ b/mitmproxy/addonmanager.py @@ -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 diff --git a/mitmproxy/addons/check_ca.py b/mitmproxy/addons/check_ca.py index f786af5a..447ba64d 100644 --- a/mitmproxy/addons/check_ca.py +++ b/mitmproxy/addons/check_ca.py @@ -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", ) diff --git a/mitmproxy/master.py b/mitmproxy/master.py index be639dcd..323f73dc 100644 --- a/mitmproxy/master.py +++ b/mitmproxy/master.py @@ -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: diff --git a/mitmproxy/tools/console/flowview.py b/mitmproxy/tools/console/flowview.py index 650a9366..837d71a8 100644 --- a/mitmproxy/tools/console/flowview.py +++ b/mitmproxy/tools/console/flowview.py @@ -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)" diff --git a/mitmproxy/tools/web/master.py b/mitmproxy/tools/web/master.py index b7eddcce..575694c7 100644 --- a/mitmproxy/tools/web/master.py +++ b/mitmproxy/tools/web/master.py @@ -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() |