diff options
author | Kevin O'Connor <kevin@koconnor.net> | 2016-11-30 12:04:28 -0500 |
---|---|---|
committer | Kevin O'Connor <kevin@koconnor.net> | 2016-11-30 21:19:43 -0500 |
commit | 4f07ee4d92732d0b619553d366b50b4b758c3d87 (patch) | |
tree | e4bf83d205001cafba04725aa6b1da9254620575 /klippy/pyhelper.c | |
parent | b14db404b55424ef783a163a02da8868120b36c9 (diff) | |
download | kutter-4f07ee4d92732d0b619553d366b50b4b758c3d87.tar.gz kutter-4f07ee4d92732d0b619553d366b50b4b758c3d87.tar.xz kutter-4f07ee4d92732d0b619553d366b50b4b758c3d87.zip |
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 <kevin@koconnor.net>
Diffstat (limited to 'klippy/pyhelper.c')
-rw-r--r-- | klippy/pyhelper.c | 30 |
1 files changed, 29 insertions, 1 deletions
diff --git a/klippy/pyhelper.c b/klippy/pyhelper.c index 3de9c2e8..2716b9b7 100644 --- a/klippy/pyhelper.c +++ b/klippy/pyhelper.c @@ -5,6 +5,7 @@ // This file may be distributed under the terms of the GNU GPLv3 license. #include <errno.h> // errno +#include <stdarg.h> // va_start #include <stdint.h> // uint8_t #include <stdio.h> // fprintf #include <string.h> // strerror @@ -29,12 +30,39 @@ fill_time(double time) return (struct timespec) {t, (time - t)*1000000000. }; } +static void +default_logger(const char *msg) +{ + fprintf(stderr, "%s\n", msg); +} + +static void (*python_logging_callback)(const char *msg) = default_logger; + +void +set_python_logging_callback(void (*func)(const char *)) +{ + python_logging_callback = func; +} + +// Log an error message +void +errorf(const char *fmt, ...) +{ + char buf[512]; + va_list args; + va_start(args, fmt); + vsnprintf(buf, sizeof(buf), fmt, args); + va_end(args); + buf[sizeof(buf)-1] = '\0'; + python_logging_callback(buf); +} + // Report 'errno' in a message written to stderr void report_errno(char *where, int rc) { int e = errno; - fprintf(stderr, "Got error %d in %s: (%d)%s\n", rc, where, e, strerror(e)); + errorf("Got error %d in %s: (%d)%s", rc, where, e, strerror(e)); } // Return a hex character for a given number |