Revamp verbosity options

Split verbosity into termlog_verbosity and console_eventlog_verbosity.

This patch also removes printing to console if there are unknown options in the
command-line. Options now live in separate addons, so having uknown options
remaining is common and expected. We definitely shoould have some other way for
users to see what was ignored so they can catch typos and the like, but that's
a different patch.
This commit is contained in:
Aldo Cortesi 2018-02-26 08:56:23 +13:00
parent 77e5d823e0
commit 2fb85553cc
12 changed files with 36 additions and 28 deletions

View File

@ -14,13 +14,20 @@ class TermLog:
def __init__(self, outfile=None):
self.outfile = outfile
def load(self, loader):
loader.add_option(
"termlog_verbosity", str, 'info',
"Log verbosity.",
choices=log.LogTierOrder
)
def log(self, e):
if log.log_tier(e.level) == log.log_tier("error"):
outfile = self.outfile or realstderr
else:
outfile = self.outfile or realstdout
if log.log_tier(ctx.options.verbosity) >= log.log_tier(e.level):
if log.log_tier(ctx.options.termlog_verbosity) >= log.log_tier(e.level):
click.secho(
e.msg,
file=outfile,

View File

@ -57,5 +57,14 @@ class Log:
self.master.add_log(text, level)
LogTierOrder = [
"error",
"warn",
"info",
"alert",
"debug",
]
def log_tier(level):
return dict(error=0, warn=1, info=2, alert=2, debug=3).get(level)

View File

@ -3,13 +3,6 @@ from typing import Optional, Sequence
from mitmproxy import optmanager
from mitmproxy.net import tls
log_verbosity = [
"error",
"warn",
"info",
"alert",
"debug",
]
CA_DIR = "~/.mitmproxy"
LISTEN_PORT = 8080
@ -51,7 +44,6 @@ class Options(optmanager.OptManager):
# FIXME: Options that must be migrated to addons, but are complicated
# because they're used by more than one addon, or because they're
# embedded in the core code somehow.
verbosity = None # type: str
view_filter = None # type: Optional[str]
def __init__(self, **kwargs) -> None:
@ -64,11 +56,6 @@ class Options(optmanager.OptManager):
"showhost", bool, False,
"Use the Host header to construct URLs for display."
)
self.add_option(
"verbosity", str, 'info',
"Log verbosity.",
choices=log_verbosity
)
# Proxy options
self.add_option(

View File

@ -7,6 +7,7 @@ from mitmproxy import command
from mitmproxy import exceptions
from mitmproxy import flow
from mitmproxy import http
from mitmproxy import log
from mitmproxy import contentviews
from mitmproxy.utils import strutils
import mitmproxy.types
@ -81,6 +82,11 @@ class ConsoleAddon:
"The default content view mode.",
choices = [i.name.lower() for i in contentviews.views]
)
loader.add_option(
"console_eventlog_verbosity", str, 'info',
"EventLog verbosity.",
choices=log.LogTierOrder
)
loader.add_option(
"console_layout", str, "single",
"Console layout.",

View File

@ -21,7 +21,7 @@ class EventLog(urwid.ListBox, layoutwidget.LayoutWidget):
master.events.sig_add.connect(self.add_event)
master.events.sig_refresh.connect(self.refresh_events)
self.master.options.subscribe(self.refresh_events, ["verbosity"])
self.master.options.subscribe(self.refresh_events, ["console_eventlog_verbosity"])
self.refresh_events()
super().__init__(self.walker)
@ -44,7 +44,7 @@ class EventLog(urwid.ListBox, layoutwidget.LayoutWidget):
return super().keypress(size, key)
def add_event(self, event_store, entry: log.LogEntry):
if log.log_tier(self.master.options.verbosity) < log.log_tier(entry.level):
if log.log_tier(self.master.options.console_eventlog_verbosity) < log.log_tier(entry.level):
return
txt = "%s: %s" % (entry.level, str(entry.msg))
if entry.level in ("error", "warn", "alert"):

View File

@ -87,7 +87,7 @@ class ConsoleMaster(master.Master):
)
def sig_add_log(self, event_store, entry: log.LogEntry):
if log.log_tier(self.options.verbosity) < log.log_tier(entry.level):
if log.log_tier(self.options.console_eventlog_verbosity) < log.log_tier(entry.level):
return
if entry.level in ("error", "warn", "alert"):
if self.first_tick:

View File

@ -46,10 +46,10 @@ def process_options(parser, opts, args):
if args.quiet or args.options or args.commands:
# also reduce log verbosity if --options or --commands is passed,
# we don't want log messages from regular startup then.
args.verbosity = 'error'
args.termlog_verbosity = 'error'
args.flow_detail = 0
if args.verbose:
args.verbosity = 'debug'
args.termlog_verbosity = 'debug'
args.flow_detail = 2
adict = {}
@ -104,9 +104,7 @@ def run(
master.server = server
master.addons.trigger("configure", opts.keys())
master.addons.trigger("tick")
remaining = opts.update_known(**unknown)
if remaining and log.log_tier(opts.verbosity) > 1:
print("Ignored options: %s" % remaining)
opts.update_known(**unknown)
if args.options:
print(optmanager.dump_defaults(opts))
sys.exit(0)

View File

@ -310,6 +310,9 @@ class TestXSSScanner():
def __init__(self):
self.args = []
def info(self, str):
self.args.append(str)
def error(self, str):
self.args.append(str)
return Logger()

View File

@ -147,7 +147,7 @@ class TestContentView:
sio = io.StringIO()
d = dumper.Dumper(sio)
with taddons.context(d) as ctx:
ctx.configure(d, flow_detail=4, verbosity='debug')
ctx.configure(d, flow_detail=4)
d.response(tflow.tflow())
assert ctx.master.has_log("content viewer failed")

View File

@ -3,7 +3,6 @@ import pytest
from mitmproxy.addons import termlog
from mitmproxy import log
from mitmproxy.options import Options
from mitmproxy.test import taddons
@ -16,7 +15,8 @@ class TestTermLog:
])
def test_output(self, outfile, expected_out, expected_err, capfd):
t = termlog.TermLog(outfile=outfile)
with taddons.context(options=Options(verbosity='info')) as tctx:
with taddons.context(t) as tctx:
tctx.options.termlog_verbosity = "info"
tctx.configure(t)
t.log(log.LogEntry("one", "info"))
t.log(log.LogEntry("two", "debug"))

View File

@ -7,8 +7,6 @@ from ... import tservers
class TestMaster(tservers.MasterTest):
def mkmaster(self, **opts):
if "verbosity" not in opts:
opts["verbosity"] = 'warn'
o = options.Options(**opts)
m = console.master.ConsoleMaster(o)
m.addons.trigger("configure", o.keys())

View File

@ -11,7 +11,7 @@ from .. import tservers
class TestDumpMaster(tservers.MasterTest):
def mkmaster(self, flt, **opts):
o = options.Options(view_filter=flt, verbosity='error', **opts)
o = options.Options(view_filter=flt, **opts)
m = dump.DumpMaster(o, with_termlog=False, with_dumper=False)
return m