Skip to content

Commit

Permalink
Take a screenshot in case of exception in CodeExecutor
Browse files Browse the repository at this point in the history
  • Loading branch information
kozlovsky committed Dec 16, 2021
1 parent 54c7e69 commit 50176d0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
10 changes: 9 additions & 1 deletion src/tribler-gui/tribler_gui/code_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from PyQt5.QtNetwork import QTcpServer

from tribler_gui.utilities import connect
from tribler_gui.utilities import connect, take_screenshot


class CodeExecutor:
Expand Down Expand Up @@ -58,6 +58,7 @@ def run_code(self, code, task_id):
pass

if self.shell.last_traceback:
self.take_screenshot()
self.on_crash(self.shell.last_traceback)
return

Expand All @@ -67,6 +68,13 @@ def run_code(self, code, task_id):
for socket in self.sockets:
socket.write(b"result %s %s\n" % (return_value, task_id))

def take_screenshot(self):
window = self.shell.locals.get('window')
if not window:
self.logger.warning("Cannot take screenshot, window is not found in locals")
else:
take_screenshot(window)

def on_crash(self, exception_text):
self.logger.error(f"Crash in CodeExecutor:\n{exception_text}")

Expand Down
14 changes: 13 additions & 1 deletion src/tribler-gui/tribler_gui/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import math
import os
import sys
import time
import traceback
import types
from datetime import datetime, timedelta
Expand All @@ -11,7 +12,8 @@
from urllib.parse import quote_plus
from uuid import uuid4

from PyQt5.QtCore import QCoreApplication, QLocale, QTranslator, pyqtSignal
from PyQt5.QtCore import QCoreApplication, QLocale, QPoint, QTranslator, pyqtSignal
from PyQt5.QtGui import QPixmap, QRegion
from PyQt5.QtWidgets import QApplication

import tribler_gui
Expand Down Expand Up @@ -434,3 +436,13 @@ def get_translator(language=None):
filename = ""
translator.load(locale, filename, directory=TRANSLATIONS_DIR)
return translator


def take_screenshot(window):
timestamp = int(time.time())
pixmap = QPixmap(window.rect().size())
window.render(pixmap, QPoint(), QRegion(window.rect()))
screenshots_dir = Path.cwd() / "screenshots"
screenshots_dir.mkdir(exist_ok=True)
img_name = 'screenshot_%d.jpg' % timestamp
pixmap.save(str(screenshots_dir / img_name))

0 comments on commit 50176d0

Please sign in to comment.