]> sigrok.org Git - libserialport.git/blobdiff - serialport.c
Add API for and default handler for debug messages.
[libserialport.git] / serialport.c
index fb2e973e60cb146bfba21b55df66c5c58c5a60ff..2bb53b4f56edb9c14301bec2304892500447689b 100644 (file)
@@ -27,6 +27,8 @@
 #include <unistd.h>
 #include <stdlib.h>
 #include <errno.h>
+#include <stdio.h>
+#include <stdarg.h>
 #ifdef _WIN32
 #include <windows.h>
 #include <tchar.h>
@@ -101,6 +103,8 @@ const struct std_baudrate std_baudrates[] = {
 #endif
 };
 
+void (*sp_debug_handler)(const char *format, ...) = sp_default_debug_handler;
+
 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
 #define NUM_STD_BAUDRATES ARRAY_SIZE(std_baudrates)
 
@@ -1424,3 +1428,19 @@ void sp_free_error_message(char *message)
        (void)message;
 #endif
 }
+
+void sp_set_debug_handler(void (*handler)(const char *format, ...))
+{
+       sp_debug_handler = handler;
+}
+
+void sp_default_debug_handler(const char *format, ...)
+{
+       va_list args;
+       va_start(args, format);
+       if (getenv("LIBSERIALPORT_DEBUG")) {
+               fputs("libserialport: ", stderr);
+               vfprintf(stderr, format, args);
+       }
+       va_end(args);
+}