From 2f6625a363d40352f78e33aef1db5aafb59e91dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Szymaniak?= Date: Fri, 24 Jul 2020 00:06:48 +0200 Subject: [PATCH] gizmo logging setup moved to c++ --- gizmo/python/general.cpp | 19 +++++++++++++ subsync/__init__.py | 6 ----- subsync/__main__.py | 58 +++++++++++++++++++--------------------- subsync/loggercfg.py | 12 +-------- 4 files changed, 47 insertions(+), 48 deletions(-) diff --git a/gizmo/python/general.cpp b/gizmo/python/general.cpp index 94ff9c4..24a8888 100644 --- a/gizmo/python/general.cpp +++ b/gizmo/python/general.cpp @@ -41,4 +41,23 @@ void initGeneralWrapper(py::module &m) /*** logger configuration ***/ m.def("setLoggerCallback", &logger::setLoggerCallback); m.def("setDebugLevel", &logger::setDebugLevel); + + + /*** default logger callback ***/ + auto logging = py::module::import("logging"); + auto getLogger = logging.attr("getLogger"); + logger::setLoggerCallback([getLogger](int level, const char* mod, const char* msg) + { + pybind11::gil_scoped_acquire acquire; + auto logger = getLogger(mod); + auto message = py::cast(msg).attr("strip")().attr("replace")("\n", "; "); + logger.attr("log") (level, message); + }); + + /*** unregister logger callback at exit ***/ + auto atexit = py::module::import("atexit"); + atexit.attr("register")(py::cpp_function([]() + { + logger::setLoggerCallback(nullptr); + })); } diff --git a/subsync/__init__.py b/subsync/__init__.py index 0b5635a..7c5255a 100644 --- a/subsync/__init__.py +++ b/subsync/__init__.py @@ -4,12 +4,6 @@ """ import gizmo -import logging -import atexit - -gizmo.setLoggerCallback(lambda lvl, m, msg: logging.getLogger(m).log(lvl, msg.strip().replace('\n', '; '))) -atexit.register(gizmo.setLoggerCallback, None) - from .synchro import SyncController, SyncTask, InputFile, SubFile, RefFile, OutputFile from .assets import assetManager, AssetList diff --git a/subsync/__main__.py b/subsync/__main__.py index f5beba7..ca5ef94 100644 --- a/subsync/__main__.py +++ b/subsync/__main__.py @@ -12,44 +12,40 @@ def subsync(argv=None): - try: - args = cmdargs.parseCmdArgs(argv) - options = args.get('options', {}) + args = cmdargs.parseCmdArgs(argv) + options = args.get('options', {}) - if options.get('logLevel') or options.get('logFile'): - loggercfg.init(level=options.get('logLevel'), path=options.get('logFile')) - if options.get('language'): - translations.setLanguage(options.get('language')) + if options.get('logLevel') or options.get('logFile'): + loggercfg.init(level=options.get('logLevel'), path=options.get('logFile')) + if options.get('language'): + translations.setLanguage(options.get('language')) - if args: - logger.debug('running with arguments: %r', args) + if args: + logger.debug('running with arguments: %r', args) - if args.get('help'): - cmdargs.printHelp() - return 0 + if args.get('help'): + cmdargs.printHelp() + return 0 - if args.get('version'): - print('subsync version {} on {}'.format(version()[0], sys.platform)) - return 0 + if args.get('version'): + print('subsync version {} on {}'.format(version()[0], sys.platform)) + return 0 - if not args.get('cli'): - if os.path.basename(os.path.splitext(sys.argv[0])[0]) == 'subsync-cmd': - logger.info("running command 'subsync-cmd', starting in headless mode") - args['cli'] = True - else: - try: - import wx - except Exception as e: - logger.warning("couldn't start wx, falling back to headless mode, %r", e) - args['cli'] = True - - if args.get('cli'): - return cli(**args) + if not args.get('cli'): + if os.path.basename(os.path.splitext(sys.argv[0])[0]) == 'subsync-cmd': + logger.info("running command 'subsync-cmd', starting in headless mode") + args['cli'] = True else: - return gui(**args) + try: + import wx + except Exception as e: + logger.warning("couldn't start wx, falling back to headless mode, %r", e) + args['cli'] = True - finally: - loggercfg.terminate() + if args.get('cli'): + return cli(**args) + else: + return gui(**args) def gui(sync=None, fromFile=None, batch=False, options={}, **args): diff --git a/subsync/loggercfg.py b/subsync/loggercfg.py index 5a2ba56..1b118fa 100644 --- a/subsync/loggercfg.py +++ b/subsync/loggercfg.py @@ -46,16 +46,12 @@ def init(level=None, path=None): logging.getLogger().error("invalid log file path '%s', ignoring", path, exc_info=True) def excepthook(type, exc, tb): - logging.getLogger('RUNTIME').critical("Unhandled exception", exc_info=(type, exc, tb)) + logging.getLogger().critical("Unhandled exception", exc_info=(type, exc, tb)) sys.__excepthook__(type, exc, tb) sys.excepthook = excepthook setup_thread_excepthook() - def print_log(level, m, msg): - logging.getLogger(m).log(level, msg.strip().replace('\n', '; ')) - - gizmo.setLoggerCallback(print_log) gizmo.setDebugLevel(numLevel) global initialized @@ -79,11 +75,6 @@ def run_with_except_hook(*args2, **kwargs2): threading.Thread.__init__ = init -def terminate(): - if initialized: - gizmo.setLoggerCallback(None) - - def setLevel(level): numLevel = parseLevel(level) logger = logging.getLogger() @@ -117,4 +108,3 @@ def setBlacklistFilters(filters): for handler in logging.root.handlers: handler.addFilter(_activeFilter) -