1
0
mirror of git://sigrok.org/libserialport synced 2023-08-10 21:13:24 +03:00

Add API for and default handler for debug messages.

This commit is contained in:
Martin Ling 2013-11-22 22:41:03 +00:00
parent 71c8a9b906
commit 863b35e69c
2 changed files with 43 additions and 0 deletions

View File

@ -615,6 +615,29 @@ char *sp_last_error_message(void);
*/
void sp_free_error_message(char *message);
/**
* Set the handler function for library debugging messages.
*
* Debugging messages are generated by the library during each operation,
* to help in diagnosing problems. The handler will be called for each
* message. The handler can be set to NULL to ignore all debug messages.
*
* The handler function should accept a format string and variable length
* argument list, in the same manner as e.g. printf().
*
* The default handler is sp_default_debug_handler().
*/
void sp_set_debug_handler(void (*handler)(const char *format, ...));
/**
* Default handler function for library debugging messages.
*
* This function prints debug messages to the standard error stream if the
* environment variable LIBSERIALPORT_DEBUG is set. Otherwise, they are
* ignored.
*/
void sp_default_debug_handler(const char *format, ...);
/** @} */
#ifdef __cplusplus

View 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);
}