From 4f07ee4d92732d0b619553d366b50b4b758c3d87 Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Wed, 30 Nov 2016 12:04:28 -0500 Subject: pyhelper: Add ability to route error messages to python logging Instead of writing error messages to stderr, route them into the python code and use the standard python logging system. Signed-off-by: Kevin O'Connor --- klippy/chelper.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) (limited to 'klippy/chelper.py') diff --git a/klippy/chelper.py b/klippy/chelper.py index 7fb8fd98..58049582 100644 --- a/klippy/chelper.py +++ b/klippy/chelper.py @@ -71,6 +71,10 @@ defs_serialqueue = """ , struct pull_queue_message *q, int max); """ +defs_pyhelper = """ + void set_python_logging_callback(void (*func)(const char *)); +""" + # Return the list of file modification times def get_mtimes(srcdir, filelist): out = [] @@ -95,15 +99,23 @@ def check_build_code(srcdir): FFI_main = None FFI_lib = None +pyhelper_logging_callback = None # Return the Foreign Function Interface api to the caller def get_ffi(): - global FFI_main, FFI_lib + global FFI_main, FFI_lib, pyhelper_logging_callback if FFI_lib is None: srcdir = os.path.dirname(os.path.realpath(__file__)) check_build_code(srcdir) FFI_main = cffi.FFI() FFI_main.cdef(defs_stepcompress) FFI_main.cdef(defs_serialqueue) + FFI_main.cdef(defs_pyhelper) FFI_lib = FFI_main.dlopen(os.path.join(srcdir, DEST_LIB)) + # Setup error logging + def logging_callback(msg): + logging.error(FFI_main.string(msg)) + pyhelper_logging_callback = FFI_main.callback( + "void(const char *)", logging_callback) + FFI_lib.set_python_logging_callback(pyhelper_logging_callback) return FFI_main, FFI_lib -- cgit v1.2.3-70-g09d2