diff options
author | Suren A. Chilingaryan <csa@suren.me> | 2015-04-29 03:42:43 +0200 |
---|---|---|
committer | Suren A. Chilingaryan <csa@suren.me> | 2015-04-29 03:42:43 +0200 |
commit | bf466a54a10293067765bf767c572cbf09bcd170 (patch) | |
tree | 7b285a15aa71949dceb17b8726717ca3e003bee1 /pcilib | |
parent | c95fc0fb80a5e72cf198e77c9bf64fd17286dfde (diff) | |
download | pcitool-bf466a54a10293067765bf767c572cbf09bcd170.tar.gz pcitool-bf466a54a10293067765bf767c572cbf09bcd170.tar.bz2 pcitool-bf466a54a10293067765bf767c572cbf09bcd170.tar.xz pcitool-bf466a54a10293067765bf767c572cbf09bcd170.zip |
New error reporting public interface
Diffstat (limited to 'pcilib')
-rw-r--r-- | pcilib/error.c | 36 | ||||
-rw-r--r-- | pcilib/error.h | 15 | ||||
-rw-r--r-- | pcilib/event.c | 4 | ||||
-rw-r--r-- | pcilib/export.c | 5 | ||||
-rw-r--r-- | pcilib/pcilib.h | 13 |
5 files changed, 51 insertions, 22 deletions
diff --git a/pcilib/error.c b/pcilib/error.c index 9df7d9d..538534b 100644 --- a/pcilib/error.c +++ b/pcilib/error.c @@ -1,24 +1,38 @@ #include <stdio.h> +#include <stdlib.h> #include <stdarg.h> #include "export.h" #include "error.h" -void pcilib_print_error(const char *msg, ...) { - va_list va; - - va_start(va, msg); +void pcilib_print_error(void *arg, const char *file, int line, pcilib_log_priority_t prio, const char *msg, va_list va) { vprintf(msg, va); - va_end(va); - printf("\n"); + printf(" [%s:%d]\n", file, line); } +static void *pcilib_logger_argument = NULL; +static pcilib_log_priority_t pcilib_logger_min_prio = PCILIB_LOG_WARNING; +static pcilib_logger_t pcilib_logger = pcilib_print_error; + + + +void pcilib_log_message(const char *file, int line, pcilib_log_priority_t prio, const char *msg, ...) { + va_list va; + + if (prio >= pcilib_logger_min_prio) { + va_start(va, msg); + pcilib_logger(pcilib_logger_argument, file, line, prio, msg, va); + va_end(va); + } +} + + +int pcilib_set_logger(pcilib_log_priority_t min_prio, pcilib_logger_t logger, void *arg) { + pcilib_logger_min_prio = min_prio; + pcilib_logger_argument = arg; -int pcilib_set_error_handler(void (*err)(const char *msg, ...), void (*warn)(const char *msg, ...)) { - if (err) pcilib_error = err; - else pcilib_error = pcilib_print_error; - if (warn) pcilib_warning = warn; - else pcilib_warning = pcilib_print_error; + if (logger) pcilib_logger = logger; + else logger = pcilib_print_error; return 0; } diff --git a/pcilib/error.h b/pcilib/error.h index d923d3f..5b5f8b1 100644 --- a/pcilib/error.h +++ b/pcilib/error.h @@ -2,7 +2,8 @@ #define _PCILIB_ERROR_H #include <errno.h> - +#include <pcilib.h> + enum { PCILIB_ERROR_SUCCESS = 0, PCILIB_ERROR_MEMORY = ENOMEM, @@ -25,8 +26,14 @@ enum { PCILIB_ERROR_BUSY = EBUSY } pcilib_errot_t; -void pcilib_print_error(const char *msg, ...); -extern void (*pcilib_error)(const char *msg, ...); -extern void (*pcilib_warning)(const char *msg, ...); +void pcilib_log_message(const char *file, int line, pcilib_log_priority_t prio, const char *msg, ...); + +#define pcilib_log(prio, ...) \ + pcilib_log_message(__FILE__, __LINE__, prio, __VA_ARGS__) + +#define pcilib_error(...) pcilib_log(PCILIB_LOG_ERROR, __VA_ARGS__) +#define pcilib_warning(...) pcilib_log(PCILIB_LOG_WARNING, __VA_ARGS__) +#define pcilib_info(...) pcilib_log(PCILIB_LOG_INFO, __VA_ARGS__) + #endif /* _PCILIB_ERROR_H */ diff --git a/pcilib/event.c b/pcilib/event.c index 43bbf01..2fc77ab 100644 --- a/pcilib/event.c +++ b/pcilib/event.c @@ -69,6 +69,10 @@ int pcilib_init_event_engine(pcilib_t *ctx) { return 0; } +pcilib_context_t *pcilib_get_event_engine(pcilib_t *ctx) { + return ctx->event_ctx; +} + int pcilib_reset(pcilib_t *ctx) { const pcilib_event_api_description_t *api; diff --git a/pcilib/export.c b/pcilib/export.c index 590a8ff..e442fc7 100644 --- a/pcilib/export.c +++ b/pcilib/export.c @@ -2,16 +2,11 @@ #include <stdio.h> -#include "error.h" #include "export.h" #include "protocols/default.h" -void (*pcilib_error)(const char *msg, ...) = pcilib_print_error; -void (*pcilib_warning)(const char *msg, ...) = pcilib_print_error; - - const pcilib_register_protocol_description_t pcilib_protocols[] = { { PCILIB_REGISTER_PROTOCOL_DEFAULT, &pcilib_default_protocol_api, NULL, NULL, "default", "" }, { 0 } diff --git a/pcilib/pcilib.h b/pcilib/pcilib.h index 3518f75..232daf8 100644 --- a/pcilib/pcilib.h +++ b/pcilib/pcilib.h @@ -4,6 +4,7 @@ #include <sys/time.h> #include <stddef.h> #include <stdint.h> +#include <stdarg.h> typedef struct pcilib_s pcilib_t; typedef struct pcilib_event_context_s pcilib_context_t; @@ -22,6 +23,12 @@ typedef unsigned int pcilib_irq_hw_source_t; typedef uint32_t pcilib_irq_source_t; typedef enum { + PCILIB_LOG_INFO, + PCILIB_LOG_WARNING, + PCILIB_LOG_ERROR +} pcilib_log_priority_t; + +typedef enum { PCILIB_HOST_ENDIAN = 0, PCILIB_LITTLE_ENDIAN, PCILIB_BIG_ENDIAN @@ -97,6 +104,8 @@ typedef struct { #define PCILIB_MODEL_DETECT NULL +typedef void (*pcilib_logger_t)(void *arg, const char *file, int line, pcilib_log_priority_t prio, const char *msg, va_list va); + /**< * Callback function called when new data is read by DMA streaming function * @ctx - DMA Engine context @@ -117,8 +126,7 @@ typedef int (*pcilib_event_callback_t)(pcilib_event_id_t event_id, pcilib_event_ typedef int (*pcilib_event_rawdata_callback_t)(pcilib_event_id_t event_id, pcilib_event_info_t *info, pcilib_event_flags_t flags, size_t size, void *data, void *user); - -int pcilib_set_error_handler(void (*err)(const char *msg, ...), void (*warn)(const char *msg, ...)); +int pcilib_set_logger(pcilib_log_priority_t min_prio, pcilib_logger_t logger, void *arg); pcilib_t *pcilib_open(const char *device, const char *model); void pcilib_close(pcilib_t *ctx); @@ -198,6 +206,7 @@ int pcilib_configure_rawdata_callback(pcilib_t *ctx, pcilib_event_rawdata_callba */ int pcilib_configure_preprocessing_threads(pcilib_t *ctx, size_t max_threads); +pcilib_context_t *pcilib_get_event_engine(pcilib_t *ctx); int pcilib_start(pcilib_t *ctx, pcilib_event_t event_mask, pcilib_event_flags_t flags); int pcilib_stop(pcilib_t *ctx, pcilib_event_flags_t flags); |