From: Jens Steinhauser Date: Fri, 16 Oct 2015 11:41:07 +0000 (+0200) Subject: Allow saving the log messages to a file. X-Git-Url: https://sigrok.org/gitaction?a=commitdiff_plain;h=c46081ed1105799f5e0d7c789a2cfbeb4a8186d8;p=sigrok-meter.git Allow saving the log messages to a file. --- diff --git a/mainwindow.py b/mainwindow.py index a28e15c..be39a4e 100644 --- a/mainwindow.py +++ b/mainwindow.py @@ -273,15 +273,19 @@ class MainWindow(QtGui.QMainWindow): layout.addWidget(label) def _setup_logPage(self): + self.logPage = QtGui.QWidget(self) + layout = QtGui.QVBoxLayout(self.logPage) + self.logView = QtGui.QListView(self) self.logView.setModel(self.logModel) self.logView.setEditTriggers(QtGui.QAbstractItemView.NoEditTriggers) self.logView.setSelectionMode(QtGui.QAbstractItemView.NoSelection) - - self.logPage = QtGui.QWidget(self) - layout = QtGui.QVBoxLayout(self.logPage) layout.addWidget(self.logView) + btn = QtGui.QPushButton('Save to file...', self) + btn.clicked.connect(self.on_save_log_clicked) + layout.addWidget(btn) + def _setup_preferencesPage(self): self.preferencesPage = QtGui.QWidget(self) layout = QtGui.QGridLayout(self.preferencesPage) @@ -498,6 +502,26 @@ class MainWindow(QtGui.QMainWindow): self.actionStartStop.setText('Stop Acquisition') self.actionStartStop.setIcon(icons.stop) + @QtCore.Slot() + def on_save_log_clicked(self): + filename = QtGui.QFileDialog.getSaveFileName(self, + 'Save Log File', settings.logging.filename.value()) + + if not filename: + # User pressed 'cancel'. + return + + try: + with open(filename, 'w') as f: + for line in self.logModel.stringList(): + f.write(line) + f.write('\n') + except Exception as e: + QtGui.QMessageBox.critical(self, 'Error saving log file', + 'Unable to save the log messages:\n{}'.format(e)) + + settings.logging.filename.setValue(filename) + @QtCore.Slot() def show_about(self): text = textwrap.dedent('''\ diff --git a/settings.py b/settings.py index c779ce4..a04b0eb 100644 --- a/settings.py +++ b/settings.py @@ -114,4 +114,5 @@ def init(): logging.level = Setting('logging/level', _default_loglevel, s=_s_loglevel, d=_d_loglevel) logging.lines = Setting('logging/lines', 1000, d=int) + logging.filename = Setting('logging/filename', '') globals()['logging'] = logging