Use thread name in log output.

This commit is contained in:
Bastian Kleineidam 2014-01-05 16:17:34 +01:00
parent 1a3d3f517b
commit 1affe58370
3 changed files with 17 additions and 7 deletions

View file

@ -1,5 +1,8 @@
Dosage 2.10 (released xx.xx.2014) Dosage 2.10 (released xx.xx.2014)
Features:
- comics: Comic strips are downloaded in parallel.
Changes: Changes:
- cmdline: Ensure only one instance of dosage is running to prevent - cmdline: Ensure only one instance of dosage is running to prevent
accidental DoS when fetching multiple comics of one site. accidental DoS when fetching multiple comics of one site.

10
dosage
View file

@ -224,6 +224,7 @@ class ComicGetter(threading.Thread):
"""Store options.""" """Store options."""
super(ComicGetter, self).__init__() super(ComicGetter, self).__init__()
self.options = options self.options = options
self.origname = self.getName()
def run(self): def run(self):
"""Process from queue until it is empty.""" """Process from queue until it is empty."""
@ -231,6 +232,7 @@ class ComicGetter(threading.Thread):
while True: while True:
try: try:
scraperobj = jobs.get(False) scraperobj = jobs.get(False)
self.setName(scraperobj.getName())
with lock: with lock:
host_lock = get_host_lock(scraperobj.url) host_lock = get_host_lock(scraperobj.url)
with host_lock: with host_lock:
@ -238,6 +240,7 @@ class ComicGetter(threading.Thread):
with lock: with lock:
comic_errors += errors comic_errors += errors
jobs.task_done() jobs.task_done()
self.setName(self.origname)
except Empty: except Empty:
break break
@ -281,9 +284,9 @@ def voteComics(options):
def voteComic(scraperobj): def voteComic(scraperobj):
"""Vote for given comic scraper.""" """Vote for given comic scraper."""
errors = 0 errors = 0
name = scraperobj.getName() out.context = getScraperName(scraperobj)
out.context = name
try: try:
name = scraperobj.getName()
answer = scraperobj.vote() answer = scraperobj.vote()
out.debug(u'Vote answer %r' % answer) out.debug(u'Vote answer %r' % answer)
if answer == 'counted': if answer == 'counted':
@ -313,7 +316,6 @@ def getStrips(scraperobj, options):
else: else:
# get current strip # get current strip
numstrips = 1 numstrips = 1
out.context = scraperobj.getName()
try: try:
if scraperobj.isComplete(options.basepath): if scraperobj.isComplete(options.basepath):
out.info(u"All comics are already downloaded.") out.info(u"All comics are already downloaded.")
@ -331,8 +333,6 @@ def getStrips(scraperobj, options):
except Exception as msg: except Exception as msg:
out.exception(msg) out.exception(msg)
errors += 1 errors += 1
finally:
out.context = u''
return errors return errors

View file

@ -11,6 +11,11 @@ from .ansicolor import Colorizer
lock = threading.Lock() lock = threading.Lock()
def get_threadname():
"""Return name of current thread."""
return threading.current_thread().getName()
class Output(object): class Output(object):
"""Print output with context, indentation and optional timestamps.""" """Print output with context, indentation and optional timestamps."""
@ -65,13 +70,15 @@ class Output(object):
"""Write message with indentation, context and optional timestamp.""" """Write message with indentation, context and optional timestamp."""
if level > self.level: if level > self.level:
return return
if self.level > 1 or self.timestamps: if self.timestamps:
timestamp = time.strftime(u'%H:%M:%S ') timestamp = time.strftime(u'%H:%M:%S ')
else: else:
timestamp = u'' timestamp = u''
with lock: with lock:
if self.context or timestamp: if self.context:
self.stream.write(u'%s%s> ' % (timestamp, self.context)) self.stream.write(u'%s%s> ' % (timestamp, self.context))
else:
self.stream.write(u'%s%s> ' % (timestamp, get_threadname()))
self.stream.write(u'%s' % s, color=color) self.stream.write(u'%s' % s, color=color)
try: try:
text_type = unicode text_type = unicode