diff options
-rw-r--r-- | apps/CMakeLists.txt | 2 | ||||
-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 | ||||
-rw-r--r-- | pcitool/cli.c | 38 |
7 files changed, 78 insertions, 35 deletions
diff --git a/apps/CMakeLists.txt b/apps/CMakeLists.txt index 32aac62..aa9f8bb 100644 --- a/apps/CMakeLists.txt +++ b/apps/CMakeLists.txt @@ -4,7 +4,7 @@ include_directories( ) link_directories( - ${CMAKE_SOURCE_DIR}/pcilib + ${CMAKE_BINARY_DIR}/pcilib ) add_executable(xilinx xilinx.c) 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); diff --git a/pcitool/cli.c b/pcitool/cli.c index 79ec9ce..5538ffa 100644 --- a/pcitool/cli.c +++ b/pcitool/cli.c @@ -340,23 +340,31 @@ static void signal_exit_handler(int signo) { exit(-1); } - -void Error(const char *format, ...) { - va_list ap; - - va_start(ap, format); - printf("Error %i: ", errno); +void LogError(void *arg, const char *file, int line, pcilib_log_priority_t prio, const char *format, va_list ap) { vprintf(format, ap); - if (errno) printf("\n errno: %s", strerror(errno)); + + if (prio == PCILIB_LOG_ERROR) { + if (errno) printf("\nerrno: %i (%s)", errno, strerror(errno)); + } + printf("\n\n"); - va_end(ap); - - exit(-1); + + if (prio == PCILIB_LOG_ERROR) { + printf("Exiting at [%s:%u]\n\n", file, line); + exit(-1); + } } -void Silence(const char *format, ...) { +void ErrorInternal(void *arg, const char *file, int line, pcilib_log_priority_t prio, const char *format, ...) { + va_list ap; + va_start(ap, format); + LogError(arg, file, line, prio, format, ap); + va_end(ap); } +#define Error(...) ErrorInternal(NULL, __FILE__, __LINE__, PCILIB_LOG_ERROR, __VA_ARGS__) + + void List(pcilib_t *handle, const pcilib_model_description_t *model_info, const char *bank, int details) { int i,j; const pcilib_register_bank_description_t *banks; @@ -2419,6 +2427,8 @@ int main(int argc, char **argv) { int force = 0; int verify = 0; + pcilib_log_priority_t log_priority; + const char *model = NULL; const pcilib_model_description_t *model_info; const pcilib_dma_description_t *dma_info; @@ -2893,7 +2903,11 @@ int main(int argc, char **argv) { else Usage(argc, argv, NULL); } - pcilib_set_error_handler(&Error, quiete?Silence:NULL); + if (verbose) log_priority = PCILIB_LOG_INFO; + else if (quiete) log_priority = PCILIB_LOG_ERROR; + else log_priority = PCILIB_LOG_WARNING; + + pcilib_set_logger(log_priority, &LogError, NULL); handle = pcilib_open(fpga_device, model); if (handle < 0) Error("Failed to open FPGA device: %s", fpga_device); |