From a1a8d3a05d3377bed595f160f6c866ec197a3d97 Mon Sep 17 00:00:00 2001 From: Dmitri Bogomolov <4glitch@gmail.com> Date: Sat, 21 Jul 2018 12:16:22 +0300 Subject: [PATCH] Fixed undesirable log_level replacement in depends --- src/bitmessageqt/__init__.py | 4 +-- src/debug.py | 47 ++++++++++++++++++------------------ src/depends.py | 8 +++--- src/namecoin.py | 2 +- 4 files changed, 32 insertions(+), 29 deletions(-) diff --git a/src/bitmessageqt/__init__.py b/src/bitmessageqt/__init__.py index aab1dd72..e8bc9769 100644 --- a/src/bitmessageqt/__init__.py +++ b/src/bitmessageqt/__init__.py @@ -2581,7 +2581,7 @@ class MyForm(settingsmixin.SMainWindow): os.remove(state.appdata + 'knownnodes.dat') previousAppdataLocation = state.appdata state.appdata = paths.lookupExeFolder() - debug.restartLoggingInUpdatedAppdataLocation() + debug.resetLogging() try: os.remove(previousAppdataLocation + 'debug.log') os.remove(previousAppdataLocation + 'debug.log.1') @@ -2599,7 +2599,7 @@ class MyForm(settingsmixin.SMainWindow): knownnodes.saveKnownNodes(state.appdata) os.remove(paths.lookupExeFolder() + 'keys.dat') os.remove(paths.lookupExeFolder() + 'knownnodes.dat') - debug.restartLoggingInUpdatedAppdataLocation() + debug.resetLogging() try: os.remove(paths.lookupExeFolder() + 'debug.log') os.remove(paths.lookupExeFolder() + 'debug.log.1') diff --git a/src/debug.py b/src/debug.py index e851bd02..d3730d7f 100644 --- a/src/debug.py +++ b/src/debug.py @@ -23,6 +23,7 @@ Use: `from debug import logger` to import this facility into whatever module you """ +import ConfigParser import logging import logging.config import os @@ -44,28 +45,26 @@ def log_uncaught_exceptions(ex_cls, ex, tb): def configureLogging(): - have_logging = False + fail_msg = '' try: - logging.config.fileConfig(os.path.join(state.appdata, 'logging.dat')) - have_logging = True - print( - 'Loaded logger configuration from %s' % - os.path.join(state.appdata, 'logging.dat')) - except: - if os.path.isfile(os.path.join(state.appdata, 'logging.dat')): - print( - 'Failed to load logger configuration from %s, using default' - ' logging config\n%s' % - (os.path.join(state.appdata, 'logging.dat'), sys.exc_info())) + logging_config = os.path.join(state.appdata, 'logging.dat') + logging.config.fileConfig(logging_config) + return ( + False, + 'Loaded logger configuration from %s' % logging_config + ) + except (OSError, ConfigParser.NoSectionError): + if os.path.isfile(logging_config): + fail_msg = \ + 'Failed to load logger configuration from %s, using default' \ + ' logging config\n%s' % \ + (logging_config, sys.exc_info()) else: # no need to confuse the user if the logger config is missing entirely - print "Using default logger configuration" + fail_msg = 'Using default logger configuration' sys.excepthook = log_uncaught_exceptions - if have_logging: - return False - logging.config.dictConfig({ 'version': 1, 'formatters': { @@ -84,7 +83,7 @@ def configureLogging(): 'class': 'logging.handlers.RotatingFileHandler', 'formatter': 'default', 'level': log_level, - 'filename': state.appdata + 'debug.log', + 'filename': os.path.join(state.appdata, 'debug.log'), 'maxBytes': 2097152, # 2 MiB 'backupCount': 1, 'encoding': 'UTF-8', @@ -109,26 +108,28 @@ def configureLogging(): 'handlers': ['console'], }, }) - return True + return True, fail_msg -# TODO (xj9): Get from a config file. -# logger = logging.getLogger('console_only') def initLogging(): - if configureLogging(): + preconfigured, msg = configureLogging() + if preconfigured: if '-c' in sys.argv: logger = logging.getLogger('file_only') else: logger = logging.getLogger('both') else: logger = logging.getLogger('default') + + if msg: + logger.log(logging.WARNING if preconfigured else logging.INFO, msg) return logger -def restartLoggingInUpdatedAppdataLocation(): +def resetLogging(): global logger - for i in list(logger.handlers): + for i in logger.handlers.iterkeys(): logger.removeHandler(i) i.flush() i.close() diff --git a/src/depends.py b/src/depends.py index d284a11b..f4f4420b 100755 --- a/src/depends.py +++ b/src/depends.py @@ -19,15 +19,14 @@ import os from importlib import import_module # We can now use logging so set up a simple configuration -formatter = logging.Formatter( - '%(levelname)s: %(message)s' -) +formatter = logging.Formatter('%(levelname)s: %(message)s') handler = logging.StreamHandler(sys.stdout) handler.setFormatter(formatter) logger = logging.getLogger('both') logger.addHandler(handler) logger.setLevel(logging.ERROR) + OS_RELEASE = { "Debian GNU/Linux".lower(): "Debian", "fedora": "Fedora", @@ -464,3 +463,6 @@ def check_dependencies(verbose=False, optional=False): 'PyBitmessage cannot start. One or more dependencies are' ' unavailable.' ) + + +logger.setLevel(0) diff --git a/src/namecoin.py b/src/namecoin.py index 7f081bc6..917b77ad 100644 --- a/src/namecoin.py +++ b/src/namecoin.py @@ -34,7 +34,7 @@ import os import socket import sys -import logging as logger +from debug import logger import defaults import tr # translate from bmconfigparser import BMConfigParser