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)
Features:
- comics: Comic strips are downloaded in parallel.
Changes:
- cmdline: Ensure only one instance of dosage is running to prevent
accidental DoS when fetching multiple comics of one site.

10
dosage
View file

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

View file

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