From 6940c424d191e266b39f2f1d60b79de31ed7b2cb Mon Sep 17 00:00:00 2001 From: SomberNight Date: Fri, 26 Apr 2019 20:45:23 +0200 Subject: [PATCH] logging: cli options to filter for modules using -v 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 --- electrum/__init__.py | 2 +- electrum/commands.py | 4 +--- electrum/gui/stdio.py | 6 ++++-- electrum/gui/text.py | 6 ++++-- electrum/interface.py | 1 - electrum/logging.py | 29 +++++++++++++++++++++++++++-- electrum/network.py | 1 - electrum/plugin.py | 1 - electrum/util.py | 37 ------------------------------------- electrum/wallet.py | 1 - electrum/x509.py | 1 - run_electrum | 10 +++++----- 12 files changed, 42 insertions(+), 57 deletions(-) diff --git a/electrum/__init__.py b/electrum/__init__.py index 41e71f4c7..45a1136f0 100644 --- a/electrum/__init__.py +++ b/electrum/__init__.py @@ -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 diff --git a/electrum/commands.py b/electrum/commands.py index 410e79bc2..609131c6e 100644 --- a/electrum/commands.py +++ b/electrum/commands.py @@ -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") diff --git a/electrum/gui/stdio.py b/electrum/gui/stdio.py index 6d7106010..13b640c35 100644 --- a/electrum/gui/stdio.py +++ b/electrum/gui/stdio.py @@ -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 @@ -30,7 +32,7 @@ class ElectrumGui: self.done = 0 self.last_balance = "" - set_verbosity(False) + console_stderr_handler.setLevel(logging.CRITICAL) self.str_recipient = "" self.str_description = "" diff --git a/electrum/gui/text.py b/electrum/gui/text.py index bc4af2d81..35e8b0307 100644 --- a/electrum/gui/text.py +++ b/electrum/gui/text.py @@ -5,15 +5,17 @@ import datetime 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 @@ -52,7 +54,7 @@ class ElectrumGui: 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 diff --git a/electrum/interface.py b/electrum/interface.py index 8e23c1798..8bfef8424 100644 --- a/electrum/interface.py +++ b/electrum/interface.py @@ -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() diff --git a/electrum/logging.py b/electrum/logging.py index 7f4dac8d1..22c7fc68b 100644 --- a/electrum/logging.py +++ b/electrum/logging.py @@ -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: @@ -73,6 +98,7 @@ def get_logger(name: str) -> logging.Logger: _logger = get_logger(__name__) +_logger.setLevel(logging.INFO) class Logger: @@ -98,8 +124,7 @@ class Logger: 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'): diff --git a/electrum/network.py b/electrum/network.py index 7ae100e1c..bf3d959f7 100644 --- a/electrum/network.py +++ b/electrum/network.py @@ -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 diff --git a/electrum/plugin.py b/electrum/plugin.py index cb00f04df..d197a787b 100644 --- a/electrum/plugin.py +++ b/electrum/plugin.py @@ -47,7 +47,6 @@ hooks = {} class Plugins(DaemonThread): - verbosity_filter = 'p' @profiler def __init__(self, config: SimpleConfig, gui_name): diff --git a/electrum/util.py b/electrum/util.py index 29c2c4e67..4d2d95e6f 100644 --- a/electrum/util.py +++ b/electrum/util.py @@ -218,29 +218,6 @@ class MyEncoder(json.JSONEncoder): 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 @@ -282,7 +259,6 @@ class DebugMem(ThreadJob): class DaemonThread(threading.Thread, Logger): """ daemon thread that terminates cleanly """ - verbosity_filter = 'd' def __init__(self): threading.Thread.__init__(self) @@ -334,19 +310,6 @@ class DaemonThread(threading.Thread, Logger): 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") diff --git a/electrum/wallet.py b/electrum/wallet.py index 2d2f08cd5..48be1b509 100644 --- a/electrum/wallet.py +++ b/electrum/wallet.py @@ -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(): diff --git a/electrum/x509.py b/electrum/x509.py index 5a75df900..c38edc77a 100644 --- a/electrum/x509.py +++ b/electrum/x509.py @@ -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) diff --git a/run_electrum b/run_electrum index fd642c6cd..654cf15fb 100755 --- a/run_electrum +++ b/run_electrum @@ -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 @@ -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: @@ -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