From 6c8ec8a8400978bf6c2290080ef34ff78fababeb Mon Sep 17 00:00:00 2001 From: Tim Bentley Date: Sat, 3 Feb 2018 11:24:39 +0000 Subject: [PATCH] Add logging to mainwindow and clean up logging code --- openlp/core/common/mixins.py | 12 ++++++- openlp/core/ui/mainwindow.py | 36 +++++++++----------- openlp/core/ui/servicemanager.py | 56 ++++++++++++++++---------------- 3 files changed, 55 insertions(+), 49 deletions(-) diff --git a/openlp/core/common/mixins.py b/openlp/core/common/mixins.py index 28c478b93..4ff8f798d 100644 --- a/openlp/core/common/mixins.py +++ b/openlp/core/common/mixins.py @@ -56,7 +56,10 @@ class LogMixin(object): def wrapped(*args, **kwargs): parent.logger.debug("Entering {function}".format(function=func.__name__)) try: - return func(*args, **kwargs) + if len(inspect.signature(func).parameters.values()): + return func(*args, **kwargs) + else: + return func(*args) except Exception as e: if parent.logger.getEffectiveLevel() <= logging.ERROR: parent.logger.error('Exception in {function} : {error}'.format(function=func.__name__, @@ -89,6 +92,13 @@ class LogMixin(object): trace_error_handler(self.logger) self.logger.error(message) + def log_critical(self, message): + """ + Common log critical handler which prints the calling path + """ + trace_error_handler(self.logger) + self.logger.critical(message) + def log_exception(self, message): """ Common log exception handler which prints the calling path diff --git a/openlp/core/ui/mainwindow.py b/openlp/core/ui/mainwindow.py index 8ea09be2f..1b923a9fb 100644 --- a/openlp/core/ui/mainwindow.py +++ b/openlp/core/ui/mainwindow.py @@ -56,7 +56,7 @@ from openlp.core.version import get_version from openlp.core.widgets.dialogs import FileDialog from openlp.core.widgets.docks import OpenLPDockWidget, MediaDockManager -log = logging.getLogger(__name__) +#log = logging.getLogger(__name__) class Ui_MainWindow(object): @@ -465,12 +465,10 @@ class Ui_MainWindow(object): self.mode_live_item.setStatusTip(translate('OpenLP.MainWindow', 'Use layout that focuses on Live.')) -class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow, RegistryProperties, LogMixin): +class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow, LogMixin, RegistryProperties): """ The main window. """ - log.info('MainWindow loaded') - def __init__(self): """ This constructor sets up the interface, the various managers, and the plugins. @@ -557,7 +555,7 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow, RegistryProperties, LogMi wait_dialog.setCancelButton(None) wait_dialog.show() for thread_name in self.application.worker_threads.keys(): - log.debug('Waiting for thread %s', thread_name) + self.log_debug('Waiting for thread %s' % thread_name) self.application.processEvents() thread = self.application.worker_threads[thread_name]['thread'] worker = self.application.worker_threads[thread_name]['worker'] @@ -595,7 +593,7 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow, RegistryProperties, LogMi """ Called on start up to restore the last active media plugin. """ - log.info('Load data from Settings') + self.log_info('Load data from Settings') if Settings().value('advanced/save current plugin'): saved_plugin_id = Settings().value('advanced/current media plugin') if saved_plugin_id != -1: @@ -627,7 +625,6 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow, RegistryProperties, LogMi :param version: The Version to be displayed. """ - log.debug('version_notice') version_text = translate('OpenLP.MainWindow', 'Version {new} of OpenLP is now available for download (you are ' 'currently running version {current}). \n\nYou can download the latest version from ' 'http://openlp.org/.').format(new=version, current=get_version()[u'full']) @@ -774,7 +771,8 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow, RegistryProperties, LogMi self.application.splash.close() QtWidgets.QMessageBox.information(self, title, message) - def on_help_web_site_clicked(self): + @staticmethod + def on_help_web_site_clicked(): """ Load the OpenLP website """ @@ -891,7 +889,7 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow, RegistryProperties, LogMi settings = Settings() import_settings = Settings(str(temp_config_path), Settings.IniFormat) - log.info('hook upgrade_plugin_settings') + self.log_info('hook upgrade_plugin_settings') self.plugin_manager.hook_upgrade_plugin_settings(import_settings) # Upgrade settings to prepare the import. if import_settings.can_upgrade(): @@ -929,7 +927,8 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow, RegistryProperties, LogMi value = import_settings.value(section_key) except KeyError: value = None - log.warning('The key "{key}" does not exist (anymore), so it will be skipped.'.format(key=section_key)) + self.log_warning('The key "{key}" does not exist (anymore), so it will be skipped.'. + format(key=section_key)) if value is not None: settings.setValue('{key}'.format(key=section_key), value) now = datetime.now() @@ -1013,7 +1012,6 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow, RegistryProperties, LogMi """ The screen has changed so we have to update components such as the renderer. """ - log.debug('screen_changed') self.application.set_busy_cursor() self.image_manager.update_display() self.renderer.update_display() @@ -1076,7 +1074,7 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow, RegistryProperties, LogMi if Settings().value('advanced/save current plugin'): Settings().setValue('advanced/current media plugin', self.media_tool_box.currentIndex()) # Call the cleanup method to shutdown plugins. - log.info('cleanup plugins') + self.log_info('cleanup plugins') self.plugin_manager.finalise_plugins() if save_settings: # Save settings @@ -1216,7 +1214,6 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow, RegistryProperties, LogMi """ Load the main window settings. """ - log.debug('Loading QSettings') settings = Settings() # Remove obsolete entries. settings.remove('custom slide') @@ -1243,7 +1240,6 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow, RegistryProperties, LogMi # Exit if we just did a settings import. if self.settings_imported: return - log.debug('Saving QSettings') settings = Settings() settings.beginGroup(self.general_settings_section) settings.setValue('recent files', self.recent_files) @@ -1268,7 +1264,7 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow, RegistryProperties, LogMi if not recent_path.is_file(): continue count += 1 - log.debug('Recent file name: {name}'.format(name=recent_path)) + self.log_debug('Recent file name: {name}'.format(name=recent_path)) action = create_action(self, '', text='&{n} {name}'.format(n=count, name=recent_path.name), data=recent_path, triggers=self.service_manager_contents.on_recent_service_clicked) @@ -1348,21 +1344,21 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow, RegistryProperties, LogMi """ Change the data directory. """ - log.info('Changing data path to {newpath}'.format(newpath=self.new_data_path)) + self.log_info('Changing data path to {newpath}'.format(newpath=self.new_data_path)) old_data_path = AppLocation.get_data_path() # Copy OpenLP data to new location if requested. self.application.set_busy_cursor() if self.copy_data: - log.info('Copying data to new path') + self.log_info('Copying data to new path') try: self.show_status_message( translate('OpenLP.MainWindow', 'Copying OpenLP data to new data directory location - {path} ' '- Please wait for copy to finish').format(path=self.new_data_path)) dir_util.copy_tree(str(old_data_path), str(self.new_data_path)) - log.info('Copy successful') + self.log_info('Copy successful') except (OSError, DistutilsFileError) as why: self.application.set_normal_cursor() - log.exception('Data copy failed {err}'.format(err=str(why))) + self.log_exception('Data copy failed {err}'.format(err=str(why))) err_text = translate('OpenLP.MainWindow', 'OpenLP Data directory copy failed\n\n{err}').format(err=str(why)), QtWidgets.QMessageBox.critical(self, translate('OpenLP.MainWindow', 'New Data Directory Error'), @@ -1370,7 +1366,7 @@ class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow, RegistryProperties, LogMi QtWidgets.QMessageBox.StandardButtons(QtWidgets.QMessageBox.Ok)) return False else: - log.info('No data copy requested') + self.log_info('No data copy requested') # Change the location of data directory in config file. settings = QtCore.QSettings() settings.setValue('advanced/data path', self.new_data_path) diff --git a/openlp/core/ui/servicemanager.py b/openlp/core/ui/servicemanager.py index 5d355f88a..2b079cdf7 100644 --- a/openlp/core/ui/servicemanager.py +++ b/openlp/core/ui/servicemanager.py @@ -299,7 +299,7 @@ class Ui_ServiceManager(object): class ServiceManager(QtWidgets.QWidget, RegistryBase, Ui_ServiceManager, LogMixin, RegistryProperties): """ Manages the services. This involves taking text strings from plugins and adding them to the service. This service - can then be zipped up with all the resources used into one OSZ or oszl file for use on any OpenLP v2 installation. + can then be zipped up with all the resources used into one OSZ or OSZL file for use on any OpenLP installation. Also handles the UI tasks of moving things up and down etc. """ servicemanager_set_item = QtCore.pyqtSignal(int) @@ -412,7 +412,7 @@ class ServiceManager(QtWidgets.QWidget, RegistryBase, Ui_ServiceManager, LogMixi if suffix not in self.suffixes: self.suffixes.append(suffix) - def on_new_service_clicked(self, field=None): + def on_new_service_clicked(self): """ Create a new service. :param field: @@ -463,7 +463,7 @@ class ServiceManager(QtWidgets.QWidget, RegistryBase, Ui_ServiceManager, LogMixi QtWidgets.QMessageBox.Save | QtWidgets.QMessageBox.Discard | QtWidgets.QMessageBox.Cancel, QtWidgets.QMessageBox.Save) - def on_recent_service_clicked(self, field=None): + def on_recent_service_clicked(self): """ Load a recent file as the service triggered by mainwindow recent service list. :param field: @@ -503,7 +503,7 @@ class ServiceManager(QtWidgets.QWidget, RegistryBase, Ui_ServiceManager, LogMixi service.append({'openlp_core': core}) return service - def save_file(self, field=None): + def save_file(self): """ Save the current service file. @@ -684,7 +684,7 @@ class ServiceManager(QtWidgets.QWidget, RegistryBase, Ui_ServiceManager, LogMixi delete_file(Path(temp_file_name)) return success - def save_file_as(self, field=None): + def save_file_as(self): """ Get a file name and then call :func:`ServiceManager.save_file` to save the file. """ @@ -736,7 +736,7 @@ class ServiceManager(QtWidgets.QWidget, RegistryBase, Ui_ServiceManager, LogMixi self.set_file_name(file_path) self.decide_save_method() - def decide_save_method(self, field=None): + def decide_save_method(self): """ Determine which type of save method to use. :param field: @@ -937,7 +937,7 @@ class ServiceManager(QtWidgets.QWidget, RegistryBase, Ui_ServiceManager, LogMixi theme_action.setChecked(True) self.menu.exec(self.service_manager_list.mapToGlobal(point)) - def on_service_item_note_form(self, field=None): + def on_service_item_note_form(self): """ Allow the service note to be edited :param field: @@ -949,7 +949,7 @@ class ServiceManager(QtWidgets.QWidget, RegistryBase, Ui_ServiceManager, LogMixi self.repaint_service_list(item, -1) self.set_modified() - def on_start_time_form(self, field=None): + def on_start_time_form(self): """ Opens a dialog to type in service item notes. :param field: @@ -959,7 +959,7 @@ class ServiceManager(QtWidgets.QWidget, RegistryBase, Ui_ServiceManager, LogMixi if self.start_time_form.exec(): self.repaint_service_list(item, -1) - def toggle_auto_play_slides_once(self, field=None): + def toggle_auto_play_slides_once(self): """ Toggle Auto play slide once. Inverts auto play once option for the item @@ -976,7 +976,7 @@ class ServiceManager(QtWidgets.QWidget, RegistryBase, Ui_ServiceManager, LogMixi self.main_window.general_settings_section + '/loop delay') self.set_modified() - def toggle_auto_play_slides_loop(self, field=None): + def toggle_auto_play_slides_loop(self): """ Toggle Auto play slide loop. @@ -993,7 +993,7 @@ class ServiceManager(QtWidgets.QWidget, RegistryBase, Ui_ServiceManager, LogMixi self.main_window.general_settings_section + '/loop delay') self.set_modified() - def on_timed_slide_interval(self, field=None): + def on_timed_slide_interval(self): """ Shows input dialog for enter interval in seconds for delay :param field: @@ -1019,7 +1019,7 @@ class ServiceManager(QtWidgets.QWidget, RegistryBase, Ui_ServiceManager, LogMixi service_item.auto_play_slides_once = False self.set_modified() - def on_auto_start(self, field=None): + def on_auto_start(self): """ Toggles to Auto Start Setting. """ @@ -1027,7 +1027,7 @@ class ServiceManager(QtWidgets.QWidget, RegistryBase, Ui_ServiceManager, LogMixi self.service_items[item]['service_item'].will_auto_start = \ not self.service_items[item]['service_item'].will_auto_start - def on_service_item_edit_form(self, field=None): + def on_service_item_edit_form(self): """ Opens a dialog to edit the service item and update the service display if changes are saved. :param field: @@ -1103,7 +1103,7 @@ class ServiceManager(QtWidgets.QWidget, RegistryBase, Ui_ServiceManager, LogMixi prev_item_last_slide = service_iterator.value() service_iterator += 1 - def on_set_item(self, message, field=None): + def on_set_item(self, message): """ Called by a signal to select a specific item and make it live usually from remote. @@ -1173,7 +1173,7 @@ class ServiceManager(QtWidgets.QWidget, RegistryBase, Ui_ServiceManager, LogMixi self.service_manager.collapsed(item.parent()) self.service_manager_list.setCurrentItem(item.parent()) - def on_collapse_all(self, field=None): + def on_collapse_all(self): """ Collapse all the service items. :param field: @@ -1193,7 +1193,7 @@ class ServiceManager(QtWidgets.QWidget, RegistryBase, Ui_ServiceManager, LogMixi if item.childCount(): self.service_items[pos - 1]['expanded'] = False - def on_expand_all(self, field=None): + def on_expand_all(self): """ Collapse all the service items. :param field: @@ -1213,7 +1213,7 @@ class ServiceManager(QtWidgets.QWidget, RegistryBase, Ui_ServiceManager, LogMixi if item.childCount(): self.service_items[pos - 1]['expanded'] = True - def on_service_top(self, field=None): + def on_service_top(self): """ Move the current ServiceItem to the top of the list. :param field: @@ -1226,7 +1226,7 @@ class ServiceManager(QtWidgets.QWidget, RegistryBase, Ui_ServiceManager, LogMixi self.repaint_service_list(0, child) self.set_modified() - def on_service_up(self, field=None): + def on_service_up(self): """ Move the current ServiceItem one position up in the list. :param field: @@ -1239,7 +1239,7 @@ class ServiceManager(QtWidgets.QWidget, RegistryBase, Ui_ServiceManager, LogMixi self.repaint_service_list(item - 1, child) self.set_modified() - def on_service_down(self, field=None): + def on_service_down(self): """ Move the current ServiceItem one position down in the list. :param field: @@ -1252,7 +1252,7 @@ class ServiceManager(QtWidgets.QWidget, RegistryBase, Ui_ServiceManager, LogMixi self.repaint_service_list(item + 1, child) self.set_modified() - def on_service_end(self, field=None): + def on_service_end(self): """ Move the current ServiceItem to the bottom of the list. :param field: @@ -1265,7 +1265,7 @@ class ServiceManager(QtWidgets.QWidget, RegistryBase, Ui_ServiceManager, LogMixi self.repaint_service_list(len(self.service_items) - 1, child) self.set_modified() - def on_delete_from_service(self, field=None): + def on_delete_from_service(self): """ Remove the current ServiceItem from the list. :param field: @@ -1491,7 +1491,7 @@ class ServiceManager(QtWidgets.QWidget, RegistryBase, Ui_ServiceManager, LogMixi self.drop_position = -1 self.set_modified() - def make_preview(self, field=None): + def make_preview(self): """ Send the current item to the Preview slide controller :param field: @@ -1516,7 +1516,7 @@ class ServiceManager(QtWidgets.QWidget, RegistryBase, Ui_ServiceManager, LogMixi else: return self.service_items[item]['service_item'] - def on_double_click_live(self, field=None): + def on_double_click_live(self): """ Send the current item to the Live slide controller but triggered by a tablewidget click event. :param field: @@ -1524,7 +1524,7 @@ class ServiceManager(QtWidgets.QWidget, RegistryBase, Ui_ServiceManager, LogMixi self.list_double_clicked = True self.make_live() - def on_single_click_preview(self, field=None): + def on_single_click_preview(self): """ If single click previewing is enabled, and triggered by a tablewidget click event, start a timeout to verify a double-click hasn't triggered. @@ -1576,7 +1576,7 @@ class ServiceManager(QtWidgets.QWidget, RegistryBase, Ui_ServiceManager, LogMixi 'is missing or inactive')) self.application.set_normal_cursor() - def remote_edit(self, field=None): + def remote_edit(self): """ Triggers a remote edit to a plugin to allow item to be edited. :param field: @@ -1588,7 +1588,7 @@ class ServiceManager(QtWidgets.QWidget, RegistryBase, Ui_ServiceManager, LogMixi if new_item: self.add_service_item(new_item, replace=True) - def on_service_item_rename(self, field=None): + def on_service_item_rename(self): """ Opens a dialog to rename the service item. @@ -1606,7 +1606,7 @@ class ServiceManager(QtWidgets.QWidget, RegistryBase, Ui_ServiceManager, LogMixi self.repaint_service_list(item, -1) self.set_modified() - def create_custom(self, field=None): + def create_custom(self): """ Saves the current text item as a custom slide :param field: @@ -1726,7 +1726,7 @@ class ServiceManager(QtWidgets.QWidget, RegistryBase, Ui_ServiceManager, LogMixi self.renderer.set_service_theme(self.service_theme) self.regenerate_service_items() - def on_theme_change_action(self, field=None): + def on_theme_change_action(self): """ Handles theme change events