Philipp Hörist pushed to branch master at gajim / gajim
Commits: ae2d2f10 by lovetox at 2021-02-14T13:06:09+01:00 Windows: Set debug env var
This allows us to discover if Gajim was started with Gajim-Debug.exe
- - - - - a68337a4 by lovetox at 2021-02-14T13:31:40+01:00 Logging: Move logging setup methods to logging_helpers
Also check for GAJIM_DEBUG
- - - - -
3 changed files:
- gajim/application.py - gajim/common/logging_helpers.py - win/misc/create-launcher.py
Changes:
===================================== gajim/application.py ===================================== @@ -34,9 +34,7 @@ # along with Gajim. If not, see http://www.gnu.org/licenses/.
import os -import time import sys -from datetime import datetime from urllib.parse import unquote
from nbxmpp.namespaces import Namespace @@ -358,14 +356,7 @@ def _handle_local_options(self, if options.contains('gdebug'): os.environ['G_MESSAGES_DEBUG'] = 'all'
- if app.get_debug_mode(): - # Redirect has to happen before logging init - self._cleanup_debug_logs() - self._redirect_output() - logging_helpers.init() - logging_helpers.set_verbose() - else: - logging_helpers.init() + logging_helpers.init()
if options.contains('quiet'): logging_helpers.set_quiet() @@ -393,24 +384,6 @@ def warn_with_traceback(message, category, filename, lineno, warnings.showwarning = warn_with_traceback warnings.filterwarnings(action="always")
- @staticmethod - def _redirect_output(): - debug_folder = configpaths.get('DEBUG') - date = datetime.today().strftime('%d%m%Y-%H%M%S') - filename = '%s-debug.log' % date - fd = open(debug_folder / filename, 'a') - sys.stderr = sys.stdout = fd - - @staticmethod - def _cleanup_debug_logs(): - debug_folder = configpaths.get('DEBUG') - debug_files = list(debug_folder.glob('*-debug.log*')) - now = time.time() - for file in debug_files: - # Delete everything older than 3 days - if file.stat().st_ctime < now - 259200: - file.unlink() - def add_actions(self): ''' Build Application Actions ''' from gajim import app_actions
===================================== gajim/common/logging_helpers.py ===================================== @@ -17,7 +17,11 @@ import logging import os import sys +import time +from datetime import datetime
+from gajim.common import app +from gajim.common import configpaths from gajim.common.i18n import _
def parseLogLevel(arg): @@ -136,6 +140,11 @@ def init(): """ Iinitialize the logging system """ + + if app.get_debug_mode(): + _cleanup_debug_logs() + _redirect_output() + use_color = False if os.name != 'nt': use_color = sys.stderr.isatty() @@ -164,6 +173,12 @@ def init(): root_log.addHandler(consoleloghandler) root_log.propagate = False
+ # GAJIM_DEBUG is set only on Windows when using Gajim-Debug.exe + # Gajim-Debug.exe shows a command line prompt and we want to redirect + # log output to it + if app.get_debug_mode() or os.environ.get('GAJIM_DEBUG', False): + set_verbose() + def set_loglevels(loglevels_string): parseAndSetLogLevels(loglevels_string)
@@ -176,6 +191,25 @@ def set_quiet(): parseAndSetLogLevels('.nbxmpp=CRITICAL')
+def _redirect_output(): + debug_folder = configpaths.get('DEBUG') + date = datetime.today().strftime('%d%m%Y-%H%M%S') + filename = '%s-debug.log' % date + fd = open(debug_folder / filename, 'a') + sys.stderr = sys.stdout = fd + + +def _cleanup_debug_logs(): + debug_folder = configpaths.get('DEBUG') + debug_files = list(debug_folder.glob('*-debug.log*')) + now = time.time() + for file in debug_files: + # Delete everything older than 3 days + if file.stat().st_ctime < now - 259200: + file.unlink() + + + # tests if __name__ == '__main__': init()
===================================== win/misc/create-launcher.py ===================================== @@ -58,7 +58,7 @@ def build_exe(source_path, resource_path, is_gui, out_path): subprocess.check_call(args)
-def get_launcher_code(): +def get_launcher_code(debug): template = """\ #include "Python.h" #define WIN32_LEAN_AND_MEAN @@ -89,6 +89,7 @@ def get_launcher_code(): PySys_SetArgvEx(__argc, szArglist, 0); result = PyRun_SimpleString( "import sys; import os;" + "os.environ['GAJIM_DEBUG'] = %s;" "sys.frozen=True;" "from pathlib import Path;" "root_path = Path(sys.executable).parents[1];" @@ -99,7 +100,7 @@ def get_launcher_code(): Py_Finalize(); return result; } - """ + """ % int(debug)
return template
@@ -153,7 +154,7 @@ def to_ver_list(v):
def build_launcher(out_path, icon_path, file_desc, product_name, product_version, - company_name, is_gui): + company_name, is_gui, debug=False):
src_ico = os.path.abspath(icon_path) target = os.path.abspath(out_path) @@ -165,7 +166,7 @@ def build_launcher(out_path, icon_path, file_desc, product_name, product_version try: os.chdir(temp) with open("launcher.c", "w") as h: - h.write(get_launcher_code()) + h.write(get_launcher_code(debug)) shutil.copyfile(src_ico, "launcher.ico") with open("launcher.rc", "w") as h: h.write(get_resouce_code( @@ -196,7 +197,7 @@ def main(): build_launcher( os.path.join(target, "Gajim-Debug.exe"), os.path.join(misc, "gajim.ico"), "Gajim", "Gajim", - version, company_name, False) + version, company_name, False, debug=True)
# build_launcher( # os.path.join(target, "history_manager.exe"),
View it on GitLab: https://dev.gajim.org/gajim/gajim/-/compare/94b6b0c2523ac3562b78031f6f0f0d42...