Browse Source

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
regtest_lnd
SomberNight 6 years ago
parent
commit
6940c424d1
No known key found for this signature in database GPG Key ID: B33B5F232C6271E9
  1. 2
      electrum/__init__.py
  2. 4
      electrum/commands.py
  3. 6
      electrum/gui/stdio.py
  4. 6
      electrum/gui/text.py
  5. 1
      electrum/interface.py
  6. 29
      electrum/logging.py
  7. 1
      electrum/network.py
  8. 1
      electrum/plugin.py
  9. 37
      electrum/util.py
  10. 1
      electrum/wallet.py
  11. 1
      electrum/x509.py
  12. 10
      run_electrum

2
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

4
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")

6
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 = ""

6
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

1
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()

29
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'):

1
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

1
electrum/plugin.py

@ -47,7 +47,6 @@ hooks = {}
class Plugins(DaemonThread):
verbosity_filter = 'p'
@profiler
def __init__(self, config: SimpleConfig, gui_name):

37
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")

1
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():

1
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)

10
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

Loading…
Cancel
Save