logging: basics
This commit is contained in:
@@ -10,9 +10,11 @@ from numbers import Real
|
||||
from copy import deepcopy
|
||||
|
||||
from . import util
|
||||
from .util import (user_dir, print_error, PrintError, make_dir,
|
||||
from .util import (user_dir, make_dir,
|
||||
NoDynamicFeeEstimates, format_fee_satoshis, quantize_feerate)
|
||||
from .i18n import _
|
||||
from .logging import get_logger, Logger
|
||||
|
||||
|
||||
FEE_ETA_TARGETS = [25, 10, 5, 2]
|
||||
FEE_DEPTH_TARGETS = [10000000, 5000000, 2000000, 1000000, 500000, 200000, 100000]
|
||||
@@ -27,6 +29,7 @@ FEERATE_STATIC_VALUES = [1000, 2000, 5000, 10000, 20000, 30000,
|
||||
|
||||
|
||||
config = None
|
||||
_logger = get_logger(__name__)
|
||||
|
||||
|
||||
def get_config():
|
||||
@@ -42,7 +45,7 @@ def set_config(c):
|
||||
FINAL_CONFIG_VERSION = 3
|
||||
|
||||
|
||||
class SimpleConfig(PrintError):
|
||||
class SimpleConfig(Logger):
|
||||
"""
|
||||
The SimpleConfig class is responsible for handling operations involving
|
||||
configuration files.
|
||||
@@ -59,6 +62,8 @@ class SimpleConfig(PrintError):
|
||||
if options is None:
|
||||
options = {}
|
||||
|
||||
Logger.__init__(self)
|
||||
|
||||
# This lock needs to be acquired for updating and reading the config in
|
||||
# a thread-safe way.
|
||||
self.lock = threading.RLock()
|
||||
@@ -119,7 +124,7 @@ class SimpleConfig(PrintError):
|
||||
path = os.path.join(path, 'simnet')
|
||||
make_dir(path, allow_symlink=False)
|
||||
|
||||
self.print_error("electrum directory", path)
|
||||
self.logger.info(f"electrum directory {path}")
|
||||
return path
|
||||
|
||||
def rename_config_keys(self, config, keypairs, deprecation_warning=False):
|
||||
@@ -130,21 +135,21 @@ class SimpleConfig(PrintError):
|
||||
if new_key not in config:
|
||||
config[new_key] = config[old_key]
|
||||
if deprecation_warning:
|
||||
self.print_stderr('Note that the {} variable has been deprecated. '
|
||||
'You should use {} instead.'.format(old_key, new_key))
|
||||
self.logger.warning('Note that the {} variable has been deprecated. '
|
||||
'You should use {} instead.'.format(old_key, new_key))
|
||||
del config[old_key]
|
||||
updated = True
|
||||
return updated
|
||||
|
||||
def set_key(self, key, value, save=True):
|
||||
if not self.is_modifiable(key):
|
||||
self.print_stderr("Warning: not changing config key '%s' set on the command line" % key)
|
||||
self.logger.warning(f"not changing config key '{key}' set on the command line")
|
||||
return
|
||||
try:
|
||||
json.dumps(key)
|
||||
json.dumps(value)
|
||||
except:
|
||||
self.print_error(f"json error: cannot save {repr(key)} ({repr(value)})")
|
||||
self.logger.info(f"json error: cannot save {repr(key)} ({repr(value)})")
|
||||
return
|
||||
self._set_key_in_user_config(key, value, save)
|
||||
|
||||
@@ -169,7 +174,7 @@ class SimpleConfig(PrintError):
|
||||
|
||||
def upgrade(self):
|
||||
with self.lock:
|
||||
self.print_error('upgrading config')
|
||||
self.logger.info('upgrading config')
|
||||
|
||||
self.convert_version_2()
|
||||
self.convert_version_3()
|
||||
@@ -222,8 +227,8 @@ class SimpleConfig(PrintError):
|
||||
def get_config_version(self):
|
||||
config_version = self.get('config_version', 1)
|
||||
if config_version > FINAL_CONFIG_VERSION:
|
||||
self.print_stderr('WARNING: config version ({}) is higher than ours ({})'
|
||||
.format(config_version, FINAL_CONFIG_VERSION))
|
||||
self.logger.warning('config version ({}) is higher than latest ({})'
|
||||
.format(config_version, FINAL_CONFIG_VERSION))
|
||||
return config_version
|
||||
|
||||
def is_modifiable(self, key):
|
||||
@@ -276,7 +281,7 @@ class SimpleConfig(PrintError):
|
||||
self.set_key('recently_open', recent)
|
||||
|
||||
def set_session_timeout(self, seconds):
|
||||
self.print_error("session timeout -> %d seconds" % seconds)
|
||||
self.logger.info(f"session timeout -> {seconds} seconds")
|
||||
self.set_key('session_timeout', seconds)
|
||||
|
||||
def get_session_timeout(self):
|
||||
@@ -576,7 +581,7 @@ def read_user_config(path):
|
||||
data = f.read()
|
||||
result = json.loads(data)
|
||||
except:
|
||||
print_error("Warning: Cannot read config file.", config_path)
|
||||
_logger.warning(f"Cannot read config file. {config_path}")
|
||||
return {}
|
||||
if not type(result) is dict:
|
||||
return {}
|
||||
|
||||
Reference in New Issue
Block a user