From 0b720dc8bc9124f4746164395ea7627b8f1f9af9 Mon Sep 17 00:00:00 2001 From: Pacman Ghost Date: Mon, 12 Sep 2022 05:10:46 +1000 Subject: [PATCH] Automatically set up basic logging for the desktop app. --- vasl_templates/main.py | 11 +++++++++++ vasl_templates/main_window.py | 10 ++++++++-- vasl_templates/utils.py | 13 +++++++++++++ vasl_templates/webapp/static/images/menu/log.png | Bin 0 -> 762 bytes vasl_templates/webapp/user_profile.py | 4 ++++ 5 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 vasl_templates/webapp/static/images/menu/log.png diff --git a/vasl_templates/main.py b/vasl_templates/main.py index fb0b741..a909e3b 100755 --- a/vasl_templates/main.py +++ b/vasl_templates/main.py @@ -102,6 +102,17 @@ def _do_main( template_pack, default_scenario, remote_debugging, debug ): #pylin from vasl_templates.webapp import globvars, load_debug_config from vasl_templates.webapp import main as webapp_main, snippets as webapp_snippets + # initialize logging + # NOTE: We set up basic logging for people using the desktop app (if they are running from source, + # or using Docker, there is an expectation they can do this themselves). If logging has already + # been set up, this config is in *addition* to what's already been configured. + handler = logging.FileHandler( globvars.user_profile.default_log_fname, mode="w" ) + handler.setLevel( logging.WARNING ) + handler.setFormatter( + logging.Formatter( "%(asctime)s | %(message)s" ) + ) + logging.getLogger().addHandler( handler ) + # configure the default template pack if template_pack: if template_pack.lower().endswith( ".zip" ): diff --git a/vasl_templates/main_window.py b/vasl_templates/main_window.py index 033dca6..36ad806 100644 --- a/vasl_templates/main_window.py +++ b/vasl_templates/main_window.py @@ -14,9 +14,10 @@ from PyQt5.QtGui import QDesktopServices, QIcon from PyQt5.QtCore import Qt, QUrl, QMargins, pyqtSlot, QVariant from vasl_templates.webapp.config.constants import APP_NAME, APP_VERSION, IS_FROZEN +from vasl_templates.webapp import globvars from vasl_templates.main import app_settings from vasl_templates.web_channel import WebChannelHandler -from vasl_templates.utils import catch_exceptions +from vasl_templates.utils import catch_exceptions, launch_file _CONSOLE_SOURCE_REGEX = re.compile( r"^http://.+?/static/(.*)$" ) @@ -44,7 +45,7 @@ class AppWebPage( QWebEnginePage ): mo = _CONSOLE_SOURCE_REGEX.search( source_id ) source = mo.group(1) if mo else source_id logger = logging.getLogger( "javascript" ) - logger.info( "%s:%d - %s", source, line_no, msg ) + logger.warning( "%s:%d - %s", source, line_no, msg ) # --------------------------------------------------------------------- @@ -80,6 +81,7 @@ class MainWindow( QWidget ): action.triggered.connect( handler ) file_menu.addAction( action ) add_action( "&Settings", "settings.png", self.on_settings ) + add_action( "&Log file", "log.png", self.on_log_file ) add_action( "&About", "info.png", self.on_about ) file_menu.addSeparator() add_action( "E&xit", "exit.png", self.on_exit ) @@ -219,6 +221,10 @@ class MainWindow( QWidget ): dlg = ServerSettingsDialog( self ) dlg.exec_() + def on_log_file( self ): + """Menu action handler.""" + launch_file( globvars.user_profile.default_log_fname ) + def on_about( self ): """Menu action handler.""" from vasl_templates.about import AboutDialog #pylint: disable=cyclic-import diff --git a/vasl_templates/utils.py b/vasl_templates/utils.py index 14a1c2c..e9d2e78 100644 --- a/vasl_templates/utils.py +++ b/vasl_templates/utils.py @@ -1,6 +1,8 @@ """ Miscellaneous utilities. """ import os +import platform +import subprocess import functools import logging import traceback @@ -78,3 +80,14 @@ def show_msg_store( msg_store ): for msg_type in ("error","warning"): for msg in msg_store.get_msgs( msg_type ): MainWindow.showErrorMsg( msg ) + +# --------------------------------------------------------------------- + +def launch_file( fname ): + """Launch the specified file.""" + if platform.system() == "Windows": + os.startfile( fname ) #pylint: disable=no-member + elif platform.system() == "Darwin": + subprocess.call( ("open", fname) ) + else: + subprocess.call( ("xdg-open", fname) ) diff --git a/vasl_templates/webapp/static/images/menu/log.png b/vasl_templates/webapp/static/images/menu/log.png new file mode 100644 index 0000000000000000000000000000000000000000..12c50838728e51d8b6dd1801a0ef9e8a90707758 GIT binary patch literal 762 zcmeAS@N?(olHy`uVBq!ia0vp@KrFz)1|-ie{%Q%NI14-?iy0WWg+Z8+Vb&Z8pdfpR zr>`sfGfqAZec{UO`DI`s*N76w(vpn)B8HXg&UgTYcuIgmE~&-IMVSR9nfZAP!kmiA zKyfaRxO09%Wl?5&MhU|j{xeoUQ9iI}QEDPcsX|F+ZYqQ6cYaQw3@1n?I6tkVJh3R1 zA-3Q@NI5sy@XX@moJ63~B8Ke;w)6vqIY7eUiA8ytdFc!Xq@T3|#n>VGON)|I8NU8+ zehpLRoS&PUnpeW`?aZ|OKruFuvXIP@oK%Jt?=Gn`FfbnVba4#Pm^gLv-t6E&iDUK8 zpDp`-&t zdZ@*m^V7X9CU?3#e)uG%Ue)MVa&6Y1FQ2DQRb}}6_>fasnX;vMfZlu+=JwfVqrQF; z&VM7spu)FyxmNvybfcB?_ujm~@aWlPS6+izT^pBwKfN{Y?&|z+M!&AE&i_&(ry%?w zE33CNN9=lOrim1X;x4werpq#wmZoH8<%KkB>7?E?<4WvgW-` zkknCBW4NP3h7My$bOlM)h3h56v{T1~x4OwT)IW{NH(!TvgRgcYMb#!*jWvSDf zzB#WA<8IJ?t`k$P>CiQ=Q>n#4^QrR8x2Kp((q;VmNO?hjupztpZv@q;V+q! zFK)=gyw-GEu5kC!7t&KW6jxk-{d-|R`)1glHHT z+MHRuJGt@dlpu|z8**nq`eS3zapGx_xSh4R9m}2n0Z*?s-%fg;{ipvQQ)JAN5@yxE R-M}bj@O1TaS?83{1OQ+8K8XMT literal 0 HcmV?d00001 diff --git a/vasl_templates/webapp/user_profile.py b/vasl_templates/webapp/user_profile.py index 730ea66..f914f12 100644 --- a/vasl_templates/webapp/user_profile.py +++ b/vasl_templates/webapp/user_profile.py @@ -40,6 +40,8 @@ class UserProfile: # configure the location of the log files self.logs_dname = "/tmp" if is_container \ else self._check_dir( appdirs.user_log_dir( _APP_NAME, _APP_AUTHOR ) ) + if is_desktop_app: + self.default_log_fname = os.path.join( self.logs_dname, "app.log" ) self.webdriver_log_fname = app_config.get( "WEBDRIVER_LOG", os.path.join( self.logs_dname, "webdriver.log" ) ) @@ -61,6 +63,8 @@ class UserProfile: _logger.info( "- local data = %s", self.local_data_dname ) _logger.debug( " - Flask lock file = %s", self.flask_lock_fname ) _logger.info( "- logs = %s", self.logs_dname ) + if is_desktop_app: + _logger.debug( "- app = %s", self.default_log_fname ) _logger.debug( " - webdriver = %s", self.webdriver_log_fname ) _logger.info( "- cache = %s", self.cache_dname ) for key, val in self.downloaded_files.items():