Skip to content
This repository has been archived by the owner on Oct 1, 2024. It is now read-only.

Commit

Permalink
gizmo logging setup moved to c++
Browse files Browse the repository at this point in the history
  • Loading branch information
Michał Szymaniak committed Jul 23, 2020
1 parent fc83484 commit 2f6625a
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 48 deletions.
19 changes: 19 additions & 0 deletions gizmo/python/general.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}));
}
6 changes: 0 additions & 6 deletions subsync/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
58 changes: 27 additions & 31 deletions subsync/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
12 changes: 1 addition & 11 deletions subsync/loggercfg.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand Down Expand Up @@ -117,4 +108,3 @@ def setBlacklistFilters(filters):

for handler in logging.root.handlers:
handler.addFilter(_activeFilter)

0 comments on commit 2f6625a

Please sign in to comment.