diff --git a/logFileModel.py b/logFileModel.py index b3ae4fe..d0b72af 100644 --- a/logFileModel.py +++ b/logFileModel.py @@ -42,6 +42,7 @@ class LogFileModel: with self._lock: # TODO handle lines longer than 4096 bytes + # TODO abort file open after a few secons: https://docs.python.org/3/library/signal.html#example with open(self._file, 'rb') as f: offset = min(byte_offset, self.byte_count()) offset = max(0, offset - self.settings.max_line_length()) diff --git a/main.py b/main.py index 610a025..66ff1d3 100644 --- a/main.py +++ b/main.py @@ -1,5 +1,7 @@ +import logging import os import re +import signal from PyQt6 import QtCore from PyQt6.lupdate import lupdate @@ -15,10 +17,13 @@ from tabs import Tabs MAX_LINE_LENGTH = 4096 +logging.basicConfig(level=logging.INFO) +log = logging.getLogger("main") class MainWindow(QMainWindow): def __init__(self, *args, **kwargs): super(MainWindow, self).__init__(*args, **kwargs) + self.settings = Settings() self.setWindowTitle(self.tr("RavenLog")) self.setGeometry(0, 0, 640, 480) @@ -84,6 +89,17 @@ class MainWindow(QMainWindow): self.close() +def stop_signal(signum, _stackframe): + """ Handle terminate signal """ + try: + log.info("Terminate signal received. %s", signum) + QtCore.QCoreApplication.quit() + except Exception: + log.exception("Exception occured while terminating") + sys.exit(1) + sys.exit(0) + + if __name__ == "__main__": app = QApplication(sys.argv) @@ -91,7 +107,19 @@ if __name__ == "__main__": #if translator.load(QLocale("de"), "messages_de.ts"): # app.installTranslator(translator) + # workaround to make signals work in QT apps. + # They do not work out of the box, because the main thread + # is running in C++ code once app.exec() is executed + # Forcing an empty lambda to be executed periodically gives + # control back to python and allows python to react to signals + timer = QTimer() + timer.timeout.connect(lambda: None) + timer.start(500) + window = MainWindow() window.show() + signal.signal(signal.SIGINT, stop_signal) + signal.signal(signal.SIGTERM, stop_signal) + app.exec()