1
0

logging: basics

This commit is contained in:
SomberNight
2019-04-26 18:52:26 +02:00
parent 4d64e132d7
commit 3385a94753
68 changed files with 681 additions and 563 deletions

View File

@@ -22,8 +22,6 @@
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import traceback
import sys
import os
import pkgutil
import importlib.util
@@ -32,16 +30,17 @@ import threading
from typing import NamedTuple, Any, Union, TYPE_CHECKING, Optional
from .i18n import _
from .util import (profiler, PrintError, DaemonThread, UserCancelled,
ThreadJob, print_error, UserFacingException)
from .util import (profiler, DaemonThread, UserCancelled, ThreadJob)
from . import bip32
from . import plugins
from .simple_config import SimpleConfig
from .logging import get_logger, Logger
if TYPE_CHECKING:
from .plugins.hw_wallet import HW_PluginBase
_logger = get_logger(__name__)
plugin_loaders = {}
hook_names = set()
hooks = {}
@@ -91,8 +90,7 @@ class Plugins(DaemonThread):
try:
self.load_plugin(name)
except BaseException as e:
traceback.print_exc(file=sys.stdout)
self.print_error("cannot initialize plugin %s:" % name, str(e))
self.logger.exception(f"cannot initialize plugin {name}: {e}")
def get(self, name):
return self.plugins.get(name)
@@ -116,7 +114,7 @@ class Plugins(DaemonThread):
raise Exception(f"Error loading {name} plugin: {repr(e)}") from e
self.add_jobs(plugin.thread_jobs())
self.plugins[name] = plugin
self.print_error("loaded", name)
self.logger.info(f"loaded {name}")
return plugin
def close_plugin(self, plugin):
@@ -136,7 +134,7 @@ class Plugins(DaemonThread):
return
self.plugins.pop(name)
p.close()
self.print_error("closed", name)
self.logger.info(f"closed {name}")
def toggle(self, name):
p = self.get(name)
@@ -151,7 +149,7 @@ class Plugins(DaemonThread):
try:
__import__(dep)
except ImportError as e:
self.print_error('Plugin', name, 'unavailable:', repr(e))
self.logger.warning(f'Plugin {name} unavailable: {repr(e)}')
return False
requires = d.get('requires_wallet_type', [])
return not requires or w.wallet_type in requires
@@ -168,8 +166,7 @@ class Plugins(DaemonThread):
plugin=p,
exception=None))
except Exception as e:
traceback.print_exc()
self.print_error("cannot load plugin for:", name)
self.logger.exception(f"cannot load plugin for: {name}")
out.append(HardwarePluginToScan(name=name,
description=details[2],
plugin=None,
@@ -178,7 +175,7 @@ class Plugins(DaemonThread):
def register_wallet_type(self, name, gui_good, wallet_type):
from .wallet import register_wallet_type, register_constructor
self.print_error("registering wallet type", (wallet_type, name))
self.logger.info(f"registering wallet type {(wallet_type, name)}")
def loader():
plugin = self.get_plugin(name)
register_constructor(wallet_type, plugin.wallet_class)
@@ -191,7 +188,7 @@ class Plugins(DaemonThread):
return self.get_plugin(name).keystore_class(d)
if details[0] == 'hardware':
self.hw_wallets[name] = (gui_good, details)
self.print_error("registering hardware %s: %s" %(name, details))
self.logger.info(f"registering hardware {name}: {details}")
register_keystore(details[1], dynamic_constructor)
def get_plugin(self, name):
@@ -218,8 +215,7 @@ def run_hook(name, *args):
try:
r = f(*args)
except Exception:
print_error("Plugin error")
traceback.print_exc(file=sys.stdout)
_logger.exception(f"Plugin error. plugin: {p}, hook: {name}")
r = False
if r:
results.append(r)
@@ -229,13 +225,14 @@ def run_hook(name, *args):
return results[0]
class BasePlugin(PrintError):
class BasePlugin(Logger):
def __init__(self, parent, config, name):
self.parent = parent # The plugins object
self.name = name
self.config = config
self.wallet = None
Logger.__init__(self)
# add self to hooks
for k in dir(self):
if k in hook_names:
@@ -243,9 +240,6 @@ class BasePlugin(PrintError):
l.append((self, getattr(self, k)))
hooks[k] = l
def diagnostic_name(self):
return self.name
def __str__(self):
return self.name
@@ -313,7 +307,7 @@ class HardwarePluginToScan(NamedTuple):
exception: Optional[Exception]
class DeviceMgr(ThreadJob, PrintError):
class DeviceMgr(ThreadJob):
'''Manages hardware clients. A client communicates over a hardware
channel with the device.
@@ -345,7 +339,7 @@ class DeviceMgr(ThreadJob, PrintError):
hidapi are implemented.'''
def __init__(self, config):
super(DeviceMgr, self).__init__()
ThreadJob.__init__(self)
# Keyed by xpub. The value is the device id
# has been paired, and None otherwise.
self.xpub_ids = {}
@@ -389,7 +383,7 @@ class DeviceMgr(ThreadJob, PrintError):
return client
client = plugin.create_client(device, handler)
if client:
self.print_error("Registering", client)
self.logger.info(f"Registering {client}")
with self.lock:
self.clients[client] = (device.path, device.id_)
return client
@@ -444,7 +438,7 @@ class DeviceMgr(ThreadJob, PrintError):
return self.client_lookup(id_)
def client_for_keystore(self, plugin, handler, keystore, force_pair):
self.print_error("getting client for keystore")
self.logger.info("getting client for keystore")
if handler is None:
raise Exception(_("Handler not found for") + ' ' + plugin.name + '\n' + _("A library is probably missing."))
handler.update_status(False)
@@ -457,7 +451,7 @@ class DeviceMgr(ThreadJob, PrintError):
client = self.force_pair_xpub(plugin, handler, info, xpub, derivation, devices)
if client:
handler.update_status(True)
self.print_error("end client for keystore")
self.logger.info("end client for keystore")
return client
def client_by_xpub(self, plugin, xpub, handler, devices):
@@ -518,7 +512,7 @@ class DeviceMgr(ThreadJob, PrintError):
try:
client = self.create_client(device, handler, plugin)
except Exception as e:
self.print_error(f'failed to create client for {plugin.name} at {device.path}: {repr(e)}')
self.logger.error(f'failed to create client for {plugin.name} at {device.path}: {repr(e)}')
if include_failing_clients:
infos.append(DeviceInfo(device=device, exception=e))
continue
@@ -595,7 +589,7 @@ class DeviceMgr(ThreadJob, PrintError):
return devices
def scan_devices(self):
self.print_error("scanning devices...")
self.logger.info("scanning devices...")
# First see what's connected that we know about
devices = self._scan_devices_with_hid()
@@ -605,8 +599,8 @@ class DeviceMgr(ThreadJob, PrintError):
try:
new_devices = f()
except BaseException as e:
self.print_error('custom device enum failed. func {}, error {}'
.format(str(f), str(e)))
self.logger.error('custom device enum failed. func {}, error {}'
.format(str(f), str(e)))
else:
devices.extend(new_devices)