Log all ankisyncd events under ankisyncd.*
This commit is contained in:
@@ -3,7 +3,7 @@ from ankisyncd.collection import CollectionWrapper, CollectionManager
|
||||
from threading import Thread
|
||||
from queue import Queue
|
||||
|
||||
import time, logging
|
||||
import time, logging, os.path
|
||||
|
||||
class ThreadingCollectionWrapper:
|
||||
"""Provides the same interface as CollectionWrapper, but it creates a new Thread to
|
||||
@@ -12,6 +12,9 @@ class ThreadingCollectionWrapper:
|
||||
def __init__(self, path, setup_new_collection=None):
|
||||
self.path = path
|
||||
self.wrapper = CollectionWrapper(path, setup_new_collection)
|
||||
# FIXME: this might not work for other collection wrappers, introduce self.wrapper.name?
|
||||
self._name = os.path.basename(os.path.dirname(self.wrapper.path))
|
||||
self.logger = logging.getLogger("ankisyncd." + str(self))
|
||||
|
||||
self._queue = Queue()
|
||||
self._thread = None
|
||||
@@ -20,6 +23,9 @@ class ThreadingCollectionWrapper:
|
||||
|
||||
self.start()
|
||||
|
||||
def __str__(self):
|
||||
return "CollectionThread[{}]".format(self._name) # TODO: self.wrapper.name?
|
||||
|
||||
@property
|
||||
def running(self):
|
||||
return self._running
|
||||
@@ -53,7 +59,7 @@ class ThreadingCollectionWrapper:
|
||||
return ret
|
||||
|
||||
def _run(self):
|
||||
logging.info('CollectionThread[%s]: Starting...', self.path)
|
||||
self.logger.info("Starting...")
|
||||
|
||||
try:
|
||||
while self._running:
|
||||
@@ -64,13 +70,13 @@ class ThreadingCollectionWrapper:
|
||||
else:
|
||||
func_name = func.__class__.__name__
|
||||
|
||||
logging.info('CollectionThread[%s]: Running %s(*%s, **%s)', self.path, func_name, repr(args), repr(kw))
|
||||
self.logger.info("Running %s(*%s, **%s)", func_name, repr(args), repr(kw))
|
||||
self.last_timestamp = time.time()
|
||||
|
||||
try:
|
||||
ret = self.wrapper.execute(func, args, kw, return_queue)
|
||||
except Exception as e:
|
||||
logging.error('CollectionThread[%s]: Unable to %s(*%s, **%s): %s',
|
||||
self.logger.error("Unable to %s(*%s, **%s): %s",
|
||||
self.path, func_name, repr(args), repr(kw), e, exc_info=True)
|
||||
# we return the Exception which will be raise'd on the other end
|
||||
ret = e
|
||||
@@ -78,7 +84,7 @@ class ThreadingCollectionWrapper:
|
||||
if return_queue is not None:
|
||||
return_queue.put(ret)
|
||||
except Exception as e:
|
||||
logging.error('CollectionThread[%s]: Thread crashed! Exception: %s', self.path, e, exc_info=True)
|
||||
self.logger.error("Thread crashed! Exception: %s", e, exc_info=True)
|
||||
finally:
|
||||
self.wrapper.close()
|
||||
# clean out old thread object
|
||||
@@ -86,7 +92,7 @@ class ThreadingCollectionWrapper:
|
||||
# in case we got here via an exception
|
||||
self._running = False
|
||||
|
||||
logging.info('CollectionThread[%s]: Stopped!', self.path)
|
||||
self.logger.info("Stopped!")
|
||||
|
||||
def start(self):
|
||||
if not self._running:
|
||||
@@ -134,6 +140,7 @@ class ThreadingCollectionManager(CollectionManager):
|
||||
|
||||
self.monitor_frequency = 15
|
||||
self.monitor_inactivity = 90
|
||||
self.logger = logging.getLogger("ankisyncd.ThreadingCollectionManager")
|
||||
|
||||
monitor = Thread(target=self._monitor_run)
|
||||
monitor.daemon = True
|
||||
@@ -153,7 +160,7 @@ class ThreadingCollectionManager(CollectionManager):
|
||||
cur = time.time()
|
||||
for path, thread in self.collections.items():
|
||||
if thread.running and thread.wrapper.opened() and thread.qempty() and cur - thread.last_timestamp >= self.monitor_inactivity:
|
||||
logging.info('Monitor is closing collection on inactive CollectionThread[%s]', thread.path)
|
||||
self.logger.info("Monitor is closing collection on inactive %s", thread)
|
||||
thread.close()
|
||||
time.sleep(self.monitor_frequency)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user