Skip to content

Commit

Permalink
logging: cli options to filter for modules using -v
Browse files Browse the repository at this point in the history
old style "-v" still works

filtering examples:
-v=debug,network=error,interface=error      // effectively blacklists network and interface
-v=warning,network=debug,interface=debug    // effectively whitelists network and interface
  • Loading branch information
SomberNight committed May 2, 2019
1 parent 3385a94 commit 6940c42
Show file tree
Hide file tree
Showing 12 changed files with 42 additions and 57 deletions.
2 changes: 1 addition & 1 deletion electrum/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .version import ELECTRUM_VERSION
from .util import format_satoshis, set_verbosity
from .util import format_satoshis
from .wallet import Wallet
from .storage import WalletStorage
from .coinchooser import COIN_CHOOSERS
Expand Down
4 changes: 1 addition & 3 deletions electrum/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -927,9 +927,7 @@ def add_network_options(parser):

def add_global_options(parser):
group = parser.add_argument_group('global options')
# const is for when no argument is given to verbosity
# default is for when the flag is missing
group.add_argument("-v", dest="verbosity", help="Set verbosity filter", default='', const='*', nargs='?')
group.add_argument("-v", dest="verbosity", help="Set verbosity filter", default='')
group.add_argument("-D", "--dir", dest="electrum_path", help="electrum directory")
group.add_argument("-P", "--portable", action="store_true", dest="portable", default=False, help="Use local 'electrum_data' directory")
group.add_argument("-w", "--wallet", dest="wallet_path", help="wallet path")
Expand Down
6 changes: 4 additions & 2 deletions electrum/gui/stdio.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
from decimal import Decimal
import getpass
import datetime
import logging

from electrum import WalletStorage, Wallet
from electrum.util import format_satoshis, set_verbosity
from electrum.util import format_satoshis
from electrum.bitcoin import is_address, COIN, TYPE_ADDRESS
from electrum.transaction import TxOutput
from electrum.network import TxBroadcastError, BestEffortRequestFailed
from electrum.logging import console_stderr_handler

_ = lambda x:x # i18n

Expand All @@ -30,7 +32,7 @@ def __init__(self, config, daemon, plugins):
self.done = 0
self.last_balance = ""

set_verbosity(False)
console_stderr_handler.setLevel(logging.CRITICAL)

self.str_recipient = ""
self.str_description = ""
Expand Down
6 changes: 4 additions & 2 deletions electrum/gui/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@
import locale
from decimal import Decimal
import getpass
import logging

import electrum
from electrum.util import format_satoshis, set_verbosity
from electrum.util import format_satoshis
from electrum.bitcoin import is_address, COIN, TYPE_ADDRESS
from electrum.transaction import TxOutput
from electrum.wallet import Wallet
from electrum.storage import WalletStorage
from electrum.network import NetworkParameters, TxBroadcastError, BestEffortRequestFailed
from electrum.interface import deserialize_server
from electrum.logging import console_stderr_handler

_ = lambda x:x # i18n

Expand Down Expand Up @@ -52,7 +54,7 @@ def __init__(self, config, daemon, plugins):
self.set_cursor(0)
self.w = curses.newwin(10, 50, 5, 5)

set_verbosity(False)
console_stderr_handler.setLevel(logging.CRITICAL)
self.tab = 0
self.pos = 0
self.popup_pos = 0
Expand Down
1 change: 0 additions & 1 deletion electrum/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,6 @@ def serialize_server(host: str, port: Union[str, int], protocol: str) -> str:


class Interface(Logger):
verbosity_filter = 'i'

def __init__(self, network: 'Network', server: str, proxy: Optional[dict]):
self.ready = asyncio.Future()
Expand Down
29 changes: 27 additions & 2 deletions electrum/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,31 @@ def _configure_file_logging(log_directory: pathlib.Path):
root_logger.addHandler(file_handler)


def _configure_verbosity(config):
verbosity = config.get('verbosity')
if not verbosity:
return
console_stderr_handler.setLevel(logging.DEBUG)
if verbosity == '*' or not isinstance(verbosity, str):
return
# example verbosity:
# debug,network=error,interface=error // effectively blacklists network and interface
# warning,network=debug,interface=debug // effectively whitelists network and interface
filters = verbosity.split(',')
for filt in filters:
if not filt: continue
items = filt.split('=')
if len(items) == 1:
level = items[0]
electrum_logger.setLevel(level.upper())
elif len(items) == 2:
logger_name, level = items
logger = get_logger(logger_name)
logger.setLevel(level.upper())
else:
raise Exception(f"invalid log filter: {filt}")


# --- External API

def get_logger(name: str) -> logging.Logger:
Expand All @@ -73,6 +98,7 @@ def get_logger(name: str) -> logging.Logger:


_logger = get_logger(__name__)
_logger.setLevel(logging.INFO)


class Logger:
Expand All @@ -98,8 +124,7 @@ def diagnostic_name(self):


def configure_logging(config):
if config.get('verbosity'):
console_stderr_handler.setLevel(logging.DEBUG)
_configure_verbosity(config)

is_android = 'ANDROID_DATA' in os.environ
if is_android or config.get('disablefilelogging'):
Expand Down
1 change: 0 additions & 1 deletion electrum/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,6 @@ class Network(Logger):
"""The Network class manages a set of connections to remote electrum
servers, each connected socket is handled by an Interface() object.
"""
verbosity_filter = 'n'

def __init__(self, config: SimpleConfig=None):
global INSTANCE
Expand Down
1 change: 0 additions & 1 deletion electrum/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@


class Plugins(DaemonThread):
verbosity_filter = 'p'

@profiler
def __init__(self, config: SimpleConfig, gui_name):
Expand Down
37 changes: 0 additions & 37 deletions electrum/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,29 +218,6 @@ def default(self, obj):
return list(obj)
return super().default(obj)

class PrintError(object):
'''A handy base class'''
verbosity_filter = ''

def diagnostic_name(self):
return ''

def log_name(self):
msg = self.verbosity_filter or self.__class__.__name__
d = self.diagnostic_name()
if d: msg += "][" + d
return "[%s]" % msg

def print_error(self, *msg):
if self.verbosity_filter in verbosity or verbosity == '*':
print_error(self.log_name(), *msg)

def print_stderr(self, *msg):
print_stderr(self.log_name(), *msg)

def print_msg(self, *msg):
print_msg(self.log_name(), *msg)


class ThreadJob(Logger):
"""A job that is run periodically from a thread's main loop. run() is
Expand Down Expand Up @@ -282,7 +259,6 @@ def run(self):

class DaemonThread(threading.Thread, Logger):
""" daemon thread that terminates cleanly """
verbosity_filter = 'd'

def __init__(self):
threading.Thread.__init__(self)
Expand Down Expand Up @@ -334,19 +310,6 @@ def on_stop(self):
self.logger.info("stopped")


verbosity = ''
def set_verbosity(filters: Union[str, bool]):
global verbosity
if type(filters) is bool: # backwards compat
verbosity = '*' if filters else ''
return
verbosity = filters


def print_error(*args):
if not verbosity: return
print_stderr(*args)

def print_stderr(*args):
args = [str(item) for item in args]
sys.stderr.write(" ".join(args) + "\n")
Expand Down
1 change: 0 additions & 1 deletion electrum/wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,6 @@ class Abstract_Wallet(AddressSynchronizer):

max_change_outputs = 3
gap_limit_for_change = 6
verbosity_filter = 'w'

def __init__(self, storage: WalletStorage):
if storage.requires_upgrade():
Expand Down
1 change: 0 additions & 1 deletion electrum/x509.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,5 @@ def load_certificates(ca_path):
if __name__ == "__main__":
import certifi

util.set_verbosity(True)
ca_path = certifi.where()
ca_list, ca_keyID = load_certificates(ca_path)
10 changes: 5 additions & 5 deletions run_electrum
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ from electrum import SimpleConfig
from electrum.wallet import Wallet
from electrum.storage import WalletStorage, get_derivation_used_for_hw_device_encryption
from electrum.util import print_msg, print_stderr, json_encode, json_decode, UserCancelled
from electrum.util import set_verbosity, InvalidPassword
from electrum.util import InvalidPassword
from electrum.commands import get_parser, known_commands, Commands, config_variables
from electrum import daemon
from electrum import keystore
Expand Down Expand Up @@ -277,6 +277,9 @@ if __name__ == '__main__':
sys.argv.append('-h')

# old '-v' syntax
# Due to this workaround that keeps old -v working,
# more advanced usages of -v need to use '-v='.
# e.g. -v=debug,network=warning,interface=error
try:
i = sys.argv.index('-v')
except ValueError:
Expand Down Expand Up @@ -324,10 +327,7 @@ if __name__ == '__main__':
if config_options.get('portable'):
config_options['electrum_path'] = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'electrum_data')

# kivy sometimes freezes when we write to sys.stderr
log_verbosity = config_options.get('verbosity') if config_options.get('gui') != 'kivy' else ''
set_verbosity(log_verbosity)
if not log_verbosity:
if not config_options.get('verbosity'):
warnings.simplefilter('ignore', DeprecationWarning)

# check uri
Expand Down

0 comments on commit 6940c42

Please sign in to comment.