Skip to content

Commit

Permalink
Merge pull request #2946 from openatv/IanSav-StackTrace.py
Browse files Browse the repository at this point in the history
[StackTrace.py] Small code clean up
  • Loading branch information
jbleyel authored Jun 18, 2023
2 parents a7a548b + 61f1efe commit c6ca0e5
Showing 1 changed file with 19 additions and 20 deletions.
39 changes: 19 additions & 20 deletions lib/python/Components/StackTrace.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from __future__ import print_function
from __future__ import absolute_import
import os
from threading import Thread, current_thread
from os import remove
from os.path import isfile
from sys import _current_frames
from traceback import extract_stack
from threading import Thread, current_thread
from time import sleep
from traceback import extract_stack

from Components.config import config


Expand All @@ -16,43 +16,42 @@ def getInstance(self):
instance = None

def __init__(self):
print("initializing StackTracePrinter")
StackTracePrinter.instance = self
Thread.__init__(self)
print("[StackTrace] Initializing StackTracePrinter.")
StackTracePrinter.instance = self
self.__running = False

def activate(self, MainThread_ident):
print("activating StackTracePrinter")
print("[StackTrace] Activating StackTracePrinter.")
self.MainThread_ident = MainThread_ident
if not self.__running:
self.__running = True
self.start()

def run(self):
while (self.__running == True):
if (os.path.isfile("/tmp/doPythonStackTrace")):
os.remove("/tmp/doPythonStackTrace")
if (isfile("/tmp/doPythonStackTrace")):
remove("/tmp/doPythonStackTrace")
if config.crash.pystackonspinner.value:
print("StackTrace")
code = []
code.append("========== Stacktrace of active Python threads ===========")
log = []
log.append("[StackTrace] ========== Stacktrace of active Python threads ===========")
for threadId, stack in list(_current_frames().items()):
if (threadId != current_thread().ident):
if (threadId == self.MainThread_ident):
code.append("========== MainThread 0x%08x =========================" % threadId)
log.append("[StackTrace] ========== MainThread 0x%08x =========================" % threadId)
else:
code.append("========== Thread ID 0x%08x =========================" % threadId)
log.append("[StackTrace] ========== Thread ID 0x%08x =========================" % threadId)
for filename, lineno, name, line in extract_stack(stack):
code.append('File: "%s", line %d, in %s' % (filename, lineno, name))
log.append('[StackTrace] File: "%s", line %d, in %s' % (filename, lineno, name))
if line:
code.append(" %s" % (line.strip()))
log.append("[StackTrace] %s" % (line.strip()))
del stack
code.append("========== Stacktrace end ================================")
for line in code:
log.append("[StackTrace] ========== Stacktrace end ================================")
for line in log: # This is done so that each line of the output is timestamped.
print(line)
sleep(1)
Thread.__init__(self)

def deactivate(self):
print("deactivating StackTracePrinter")
print("[StackTrace] Deactivating StackTracePrinter.")
self.__running = False

0 comments on commit c6ca0e5

Please sign in to comment.