use several loggers for parts of weboob
This commit is contained in:
parent
a1a58c7c32
commit
84b4003bf4
14 changed files with 74 additions and 67 deletions
|
|
@ -19,11 +19,11 @@
|
|||
from __future__ import with_statement
|
||||
|
||||
from copy import copy
|
||||
from logging import debug
|
||||
from threading import Thread, Event, RLock, Timer
|
||||
|
||||
from weboob.capabilities.base import CapBaseObject
|
||||
from weboob.tools.misc import get_backtrace
|
||||
from weboob.tools.log import getLogger
|
||||
|
||||
|
||||
__all__ = ['BackendsCall', 'CallErrors']
|
||||
|
|
@ -44,6 +44,7 @@ class BackendsCall(object):
|
|||
@param function backends' method name, or callable object
|
||||
@param args, kwargs arguments given to called functions
|
||||
"""
|
||||
self.logger = getLogger('bcall')
|
||||
# Store if a backend is finished
|
||||
self.backends = {}
|
||||
for backend in backends:
|
||||
|
|
@ -64,7 +65,7 @@ class BackendsCall(object):
|
|||
# Create jobs for each backend
|
||||
with self.mutex:
|
||||
for backend in backends:
|
||||
debug('Creating a new thread for %s' % backend)
|
||||
self.logger.debug('Creating a new thread for %s' % backend)
|
||||
self.threads.append(Timer(0, self._caller, (backend, function, args, kwargs)).start())
|
||||
if not backends:
|
||||
self.finish_event.set()
|
||||
|
|
@ -82,21 +83,21 @@ class BackendsCall(object):
|
|||
self.response_event.set()
|
||||
|
||||
def _caller(self, backend, function, args, kwargs):
|
||||
debug('%s: Thread created successfully' % backend)
|
||||
self.logger.debug('%s: Thread created successfully' % backend)
|
||||
with backend:
|
||||
try:
|
||||
# Call method on backend
|
||||
try:
|
||||
debug('%s: Calling function %s' % (backend, function))
|
||||
self.logger.debug('%s: Calling function %s' % (backend, function))
|
||||
if callable(function):
|
||||
result = function(backend, *args, **kwargs)
|
||||
else:
|
||||
result = getattr(backend, function)(*args, **kwargs)
|
||||
except Exception, error:
|
||||
debug('%s: Called function %s raised an error: %r' % (backend, function, error))
|
||||
self.logger.debug('%s: Called function %s raised an error: %r' % (backend, function, error))
|
||||
self._store_error(backend, error)
|
||||
else:
|
||||
debug('%s: Called function %s returned: %r' % (backend, function, result))
|
||||
self.logger.debug('%s: Called function %s returned: %r' % (backend, function, result))
|
||||
|
||||
if hasattr(result, '__iter__') and not isinstance(result, basestring):
|
||||
# Loop on iterator
|
||||
|
|
|
|||
|
|
@ -19,12 +19,12 @@
|
|||
from __future__ import with_statement
|
||||
|
||||
import logging
|
||||
from logging import debug, error, exception
|
||||
import os
|
||||
import re
|
||||
|
||||
from weboob.capabilities.base import IBaseCap
|
||||
from weboob.tools.backend import BaseBackend
|
||||
from weboob.tools.log import getLogger
|
||||
|
||||
|
||||
__all__ = ['Module', 'ModulesLoader']
|
||||
|
|
@ -32,6 +32,7 @@ __all__ = ['Module', 'ModulesLoader']
|
|||
|
||||
class Module(object):
|
||||
def __init__(self, package):
|
||||
self.logger = getLogger('backend')
|
||||
self.package = package
|
||||
self.klass = None
|
||||
for attrname in dir(self.package):
|
||||
|
|
@ -89,14 +90,15 @@ class Module(object):
|
|||
return False
|
||||
|
||||
def create_instance(self, weboob, instance_name, config, storage):
|
||||
backend_instance = self.klass(weboob, instance_name, config, storage)
|
||||
debug(u'Created backend instance "%s" for backend "%s"' % (instance_name, self.name))
|
||||
backend_instance = self.klass(weboob, instance_name, config, storage, self.logger)
|
||||
self.logger.debug(u'Created backend instance "%s" for backend "%s"' % (instance_name, self.name))
|
||||
return backend_instance
|
||||
|
||||
|
||||
class ModulesLoader(object):
|
||||
def __init__(self):
|
||||
self.loaded = {}
|
||||
self.logger = getLogger('modules')
|
||||
|
||||
def get_or_load_module(self, module_name):
|
||||
if module_name not in self.loaded:
|
||||
|
|
@ -129,13 +131,13 @@ class ModulesLoader(object):
|
|||
except ImportError, e:
|
||||
msg = u'Unable to load module "%s": %s' % (module_name, e)
|
||||
if logging.root.level == logging.DEBUG:
|
||||
exception(msg)
|
||||
self.logger.exception(msg)
|
||||
return
|
||||
else:
|
||||
error(msg)
|
||||
self.logger.error(msg)
|
||||
return
|
||||
if module.name in self.loaded:
|
||||
debug('Module "%s" is already loaded from %s' % (module_name, module.package.__path__[0]))
|
||||
self.logger.debug('Module "%s" is already loaded from %s' % (module_name, module.package.__path__[0]))
|
||||
return
|
||||
self.loaded[module.name] = module
|
||||
debug('Loaded module "%s" from %s' % (module_name, module.package.__path__[0]))
|
||||
self.logger.debug('Loaded module "%s" from %s' % (module_name, module.package.__path__[0]))
|
||||
|
|
|
|||
|
|
@ -18,7 +18,6 @@
|
|||
|
||||
from __future__ import with_statement
|
||||
|
||||
from logging import warning
|
||||
import os
|
||||
|
||||
from weboob.core.bcall import BackendsCall
|
||||
|
|
@ -26,6 +25,7 @@ from weboob.core.modules import ModulesLoader
|
|||
from weboob.core.backendscfg import BackendsConfig
|
||||
from weboob.core.scheduler import Scheduler
|
||||
from weboob.tools.backend import BaseBackend
|
||||
from weboob.tools.log import getLogger
|
||||
|
||||
|
||||
__all__ = ['Weboob']
|
||||
|
|
@ -36,6 +36,7 @@ class Weboob(object):
|
|||
BACKENDS_FILENAME = 'backends'
|
||||
|
||||
def __init__(self, workdir=WORKDIR, backends_filename=None, scheduler=None, storage=None):
|
||||
self.logger = getLogger('weboob')
|
||||
self.workdir = workdir
|
||||
self.backend_instances = {}
|
||||
|
||||
|
|
@ -48,7 +49,7 @@ class Weboob(object):
|
|||
if not os.path.exists(self.workdir):
|
||||
os.mkdir(self.workdir, 0700)
|
||||
elif not os.path.isdir(self.workdir):
|
||||
warning(u'"%s" is not a directory' % self.workdir)
|
||||
self.logger.warning(u'"%s" is not a directory' % self.workdir)
|
||||
|
||||
# Backends loader
|
||||
self.modules_loader = ModulesLoader()
|
||||
|
|
@ -96,15 +97,15 @@ class Weboob(object):
|
|||
continue
|
||||
module = self.modules_loader.get_or_load_module(module_name)
|
||||
if module is None:
|
||||
warning(u'Backend "%s" is referenced in ~/.weboob/backends '
|
||||
'configuration file, but was not found. '
|
||||
'Hint: is it installed?' % module_name)
|
||||
self.logger.warning(u'Backend "%s" is referenced in ~/.weboob/backends '
|
||||
'configuration file, but was not found. '
|
||||
'Hint: is it installed?' % module_name)
|
||||
continue
|
||||
if caps is not None and not module.has_caps(caps):
|
||||
continue
|
||||
|
||||
if instance_name in self.backend_instances:
|
||||
warning(u'Oops, the backend "%s" is already loaded. Unload it before reloading...' % instance_name)
|
||||
self.logger.warning(u'Oops, the backend "%s" is already loaded. Unload it before reloading...' % instance_name)
|
||||
self.unload_backends(instance_name)
|
||||
|
||||
try:
|
||||
|
|
@ -186,7 +187,7 @@ class Weboob(object):
|
|||
else:
|
||||
backends.append(backend)
|
||||
else:
|
||||
warning(u'The "backends" value isn\'t supported: %r' % _backends)
|
||||
self.logger.warning(u'The "backends" value isn\'t supported: %r' % _backends)
|
||||
|
||||
if 'caps' in kwargs:
|
||||
caps = kwargs.pop('caps')
|
||||
|
|
|
|||
|
|
@ -18,8 +18,8 @@
|
|||
|
||||
from __future__ import with_statement
|
||||
|
||||
import logging
|
||||
from threading import Timer, Event, RLock
|
||||
from weboob.tools.log import getLogger
|
||||
|
||||
|
||||
__all__ = ['Scheduler']
|
||||
|
|
@ -40,6 +40,7 @@ class IScheduler(object):
|
|||
|
||||
class Scheduler(IScheduler):
|
||||
def __init__(self):
|
||||
self.logger = getLogger('scheduler')
|
||||
self.mutex = RLock()
|
||||
self.stop_event = Event()
|
||||
self.count = 0
|
||||
|
|
@ -51,7 +52,7 @@ class Scheduler(IScheduler):
|
|||
|
||||
with self.mutex:
|
||||
self.count += 1
|
||||
logging.debug('function "%s" will be called in %s seconds' % (function.__name__, interval))
|
||||
self.logger.debug('function "%s" will be called in %s seconds' % (function.__name__, interval))
|
||||
timer = Timer(interval, self._callback, (self.count, function, args))
|
||||
self.queue[self.count] = timer
|
||||
timer.start()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue