Skip to content

Commit

Permalink
Add the thread traceback to the log message.
Browse files Browse the repository at this point in the history
A dependency to mock was added in order to test the new feature.
  • Loading branch information
alga committed Oct 16, 2015
1 parent 9410406 commit ce40876
Show file tree
Hide file tree
Showing 3 changed files with 199 additions and 2 deletions.
1 change: 1 addition & 0 deletions setup.py
Expand Up @@ -48,6 +48,7 @@ def read(*rnames):
extras_require=dict(
test=[
'zope.testing',
'mock',
],
),
install_requires=[
Expand Down
20 changes: 19 additions & 1 deletion src/cipher/longrequest/longrequest.py
Expand Up @@ -20,8 +20,11 @@
import logging
import threading
import time
import io
import pprint
import re
import sys
import traceback

from cipher.background.thread import BackgroundWorkerThread
from cipher.background.contextmanagers import (ZopeInteraction, ZopeTransaction)
Expand Down Expand Up @@ -59,7 +62,10 @@
threads in use:%(threadsused)s
environment:%(worker_environ)s
username:%(username)s
form:%(form)s"""
form:%(form)s
Thread stack:
%(traceback)s
Top of stack"""

IGNORE_URLS = []

Expand Down Expand Up @@ -313,6 +319,17 @@ def getAllThreadInfo(omitThreads=()):
return infos


def getThreadTraceback(thread_id):
try:
frame = sys._current_frames()[thread_id]
buf = io.BytesIO()
traceback.print_stack(frame, file=buf)
return buf.getvalue().decode('utf-8').rstrip()
except KeyError:
# if thread is already finished
return ' ???'


def getFormattedThreadinfo(event):
username = ''
form = ''
Expand All @@ -336,6 +353,7 @@ def getFormattedThreadinfo(event):
worker_environ=worker_environ,
username=username,
form=form,
traceback=getThreadTraceback(event.thread_id),
threadsused=getThreadsUsed())
threadinfo = THREAD_TEMPLATE % data
return threadinfo
Expand Down

0 comments on commit ce40876

Please sign in to comment.