From 9169bc10b9b6982b9a3ee156b65a3fdcd980989f Mon Sep 17 00:00:00 2001 From: Andrew Dona-Couch -- GitHub drop ICE Date: Mon, 31 Jan 2022 15:48:10 -0500 Subject: [PATCH] Make Librador logger configurable (#210) * Refactor librador logging * Make librador logger configurable --- .../librador_shared_library/librador.cpp | 32 +++++++- .../librador_shared_library/librador.h | 8 ++ .../librador_shared_library/logging.h | 11 +++ .../logging_internal.h | 13 +++ .../librador_shared_library/o1buffer.cpp | 5 +- .../usbcallhandler.cpp | 81 ++++++++++--------- 6 files changed, 107 insertions(+), 43 deletions(-) create mode 100644 Librador_API/___librador/librador_shared_library/logging.h create mode 100644 Librador_API/___librador/librador_shared_library/logging_internal.h diff --git a/Librador_API/___librador/librador_shared_library/librador.cpp b/Librador_API/___librador/librador_shared_library/librador.cpp index 25dc2931..f748a164 100644 --- a/Librador_API/___librador/librador_shared_library/librador.cpp +++ b/Librador_API/___librador/librador_shared_library/librador.cpp @@ -1,6 +1,7 @@ #include "librador.h" #include "librador_internal.h" #include "usbcallhandler.h" +#include "logging_internal.h" #include #include @@ -94,7 +95,7 @@ std::vector * librador_get_digital_data(int channel, double timeWindow_ int delay_subsamples = round(delay_seconds * subsamples_per_second); int numToGet = round(timeWindow_seconds * subsamples_per_second)/interval_subsamples; - printf("interval_subsamples = %d\ndelay_subsamples = %d\nnumToGet=%d\n", interval_subsamples, delay_subsamples, numToGet); + LIBRADOR_LOG(LOG_DEBUG, "interval_subsamples = %d\ndelay_subsamples = %d\nnumToGet=%d\n", interval_subsamples, delay_subsamples, numToGet); return internal_librador_object->usb_driver->getMany_singleBit(channel, numToGet, interval_subsamples, delay_subsamples); } @@ -295,3 +296,32 @@ int librador_synchronise_end(){ return internal_librador_object->usb_driver->set_synchronous_pause_state(false); } */ + +static void std_logger(void * userdata, const int level, const char * format, va_list ap); +static librador_logger_p _librador_global_logger = std_logger; +static void * _librador_global_userdata = NULL; + +void librador_global_logger(const int level, const char * format, ...){ + va_list args; + va_start(args, format); + if (_librador_global_logger) + _librador_global_logger(_librador_global_userdata, level, format, args); + va_end(args); +} + +void librador_logger_set(void * userdata, librador_logger_p logger){ + _librador_global_logger = logger ? logger : std_logger; + _librador_global_userdata = userdata; +} + +librador_logger_p librador_logger_get(void){ + return _librador_global_logger; +} + +void * librador_logger_get_userdata(void){ + return _librador_global_userdata; +} + +static void std_logger(void * userdata, const int level, const char * format, va_list ap){ + vfprintf((level > LOG_ERROR) ? stdout : stderr , format, ap); +} diff --git a/Librador_API/___librador/librador_shared_library/librador.h b/Librador_API/___librador/librador_shared_library/librador.h index 657eb645..67f0d0d7 100644 --- a/Librador_API/___librador/librador_shared_library/librador.h +++ b/Librador_API/___librador/librador_shared_library/librador.h @@ -2,7 +2,9 @@ #define LIBRADOR_H #include "librador_global.h" +#include "logging.h" #include +#include #include int LIBRADORSHARED_EXPORT librador_init(); @@ -52,5 +54,11 @@ int LIBRADORSHARED_EXPORT librador_synchronise_begin(); int LIBRADORSHARED_EXPORT librador_synchronise_end(); */ +typedef void (*librador_logger_p)(void * userdata, const int level, const char * format, va_list); + +void LIBRADORSHARED_EXPORT librador_logger_set(void * userdata, librador_logger_p logger); +librador_logger_p LIBRADORSHARED_EXPORT librador_logger_get(void); +void * LIBRADORSHARED_EXPORT librador_logger_get_userdata(void); + #endif // LIBRADOR_H diff --git a/Librador_API/___librador/librador_shared_library/logging.h b/Librador_API/___librador/librador_shared_library/logging.h new file mode 100644 index 00000000..972cfc17 --- /dev/null +++ b/Librador_API/___librador/librador_shared_library/logging.h @@ -0,0 +1,11 @@ +#ifndef LOGGING_H +#define LOGGING_H + +enum { + LOG_NONE = 0, + LOG_ERROR, + LOG_WARNING, + LOG_DEBUG, +}; + +#endif // LOGGING_H diff --git a/Librador_API/___librador/librador_shared_library/logging_internal.h b/Librador_API/___librador/librador_shared_library/logging_internal.h new file mode 100644 index 00000000..4ac61902 --- /dev/null +++ b/Librador_API/___librador/librador_shared_library/logging_internal.h @@ -0,0 +1,13 @@ +#ifndef LOGGING_INTERNAL_H +#define LOGGING_INTERNAL_H + +#include "logging.h" + +#define LIBRADOR_LOG(level, ...) \ + do { \ + librador_global_logger(level, __VA_ARGS__); \ + } while (0) + +void librador_global_logger(const int level, const char * format, ...); + +#endif // LOGGING_INTERNAL_H diff --git a/Librador_API/___librador/librador_shared_library/o1buffer.cpp b/Librador_API/___librador/librador_shared_library/o1buffer.cpp index 92e80f7b..8aa37fb0 100644 --- a/Librador_API/___librador/librador_shared_library/o1buffer.cpp +++ b/Librador_API/___librador/librador_shared_library/o1buffer.cpp @@ -1,4 +1,5 @@ #include "o1buffer.h" +#include "logging_internal.h" #include #include #include @@ -34,7 +35,7 @@ void o1buffer::add(int value, int address){ address = address % NUM_SAMPLES_PER_CHANNEL; } if(address<0){ - fprintf(stderr, "ERROR: o1buffer::add was given a negative address\n"); + LIBRADOR_LOG(LOG_ERROR, "ERROR: o1buffer::add was given a negative address\n"); } //Assign the value buffer[address] = value; @@ -88,7 +89,7 @@ int o1buffer::get(int address){ address = address % NUM_SAMPLES_PER_CHANNEL; } if(address<0){ - fprintf(stderr, "ERROR: o1buffer::get was given a negative address\n"); + LIBRADOR_LOG(LOG_ERROR, "ERROR: o1buffer::get was given a negative address\n"); } //Return the value return buffer[address]; diff --git a/Librador_API/___librador/librador_shared_library/usbcallhandler.cpp b/Librador_API/___librador/librador_shared_library/usbcallhandler.cpp index 965ceb7c..43e02491 100644 --- a/Librador_API/___librador/librador_shared_library/usbcallhandler.cpp +++ b/Librador_API/___librador/librador_shared_library/usbcallhandler.cpp @@ -2,6 +2,7 @@ #include #include "o1buffer.h" +#include "logging_internal.h" #include #include #include @@ -88,20 +89,20 @@ static void LIBUSB_CALL isoCallback(struct libusb_transfer * transfer){ if(usb_iso_needs_rearming()){ int error = libusb_submit_transfer(transfer); if(error){ - printf("Error re-arming the endpoint!\n"); + LIBRADOR_LOG(LOG_DEBUG, "Error re-arming the endpoint!\n"); begin_usb_thread_shutdown(); decrement_remaining_transfers(); - printf("Transfer not being rearmed! %d armed transfers remaining\n", usb_shutdown_remaining_transfers); + LIBRADOR_LOG(LOG_WARNING, "Transfer not being rearmed! %d armed transfers remaining\n", usb_shutdown_remaining_transfers); } } else { decrement_remaining_transfers(); - printf("Transfer not being rearmed! %d armed transfers remaining\n", usb_shutdown_remaining_transfers); + LIBRADOR_LOG(LOG_WARNING, "Transfer not being rearmed! %d armed transfers remaining\n", usb_shutdown_remaining_transfers); } return; } void usb_polling_function(libusb_context *ctx){ - printf("usb_polling_function thread spawned\n"); + LIBRADOR_LOG(LOG_DEBUG, "usb_polling_function thread spawned\n"); struct timeval tv; tv.tv_sec = 1; tv.tv_usec = 0;//ISO_PACKETS_PER_CTX*4000; @@ -120,7 +121,7 @@ usbCallHandler::usbCallHandler(unsigned short VID_in, unsigned short PID_in) for(int k=0; kjoin(); - printf("USB polling thread stopped.\n"); + LIBRADOR_LOG(LOG_DEBUG, "USB polling thread stopped.\n"); delete usb_polling_thread; for (int i=0; itrfcntH0 << 8) + udsPtr->trfcntL0; @@ -281,18 +282,18 @@ int usbCallHandler::avrDebug(void){ uint16_t dma_ch0_cnt = (udsPtr->dma_ch0_cntH << 8) + udsPtr->dma_ch0_cntL; uint16_t dma_ch1_cnt = (udsPtr->dma_ch1_cntH << 8) + udsPtr->dma_ch1_cntL; - printf("%s", udsPtr->header); - printf("trfcnt0 = %d\n", trfcnt0); - printf("trfcnt1 = %d\n", trfcnt1); - printf("medianTrfcnt = %d\n", medianTrfcnt); - printf("outOfRange = %d\n", outOfRange); - printf("counter = %d\n", counter); - printf("calValNeg = %d\n", udsPtr->calValNeg); - printf("calValPos = %d\n", udsPtr->calValPos); - printf("CALA = %d\n", udsPtr->CALA); - printf("CALB = %d\n", udsPtr->CALB); - printf("dma_ch0_cnt = %d\n", dma_ch0_cnt); - printf("dma_ch1_cnt = %d\n", dma_ch1_cnt); + LIBRADOR_LOG(LOG_DEBUG, "%s", udsPtr->header); + LIBRADOR_LOG(LOG_DEBUG, "trfcnt0 = %d\n", trfcnt0); + LIBRADOR_LOG(LOG_DEBUG, "trfcnt1 = %d\n", trfcnt1); + LIBRADOR_LOG(LOG_DEBUG, "medianTrfcnt = %d\n", medianTrfcnt); + LIBRADOR_LOG(LOG_DEBUG, "outOfRange = %d\n", outOfRange); + LIBRADOR_LOG(LOG_DEBUG, "counter = %d\n", counter); + LIBRADOR_LOG(LOG_DEBUG, "calValNeg = %d\n", udsPtr->calValNeg); + LIBRADOR_LOG(LOG_DEBUG, "calValPos = %d\n", udsPtr->calValPos); + LIBRADOR_LOG(LOG_DEBUG, "CALA = %d\n", udsPtr->CALA); + LIBRADOR_LOG(LOG_DEBUG, "CALB = %d\n", udsPtr->CALB); + LIBRADOR_LOG(LOG_DEBUG, "dma_ch0_cnt = %d\n", dma_ch0_cnt); + LIBRADOR_LOG(LOG_DEBUG, "dma_ch1_cnt = %d\n", dma_ch1_cnt); return 0; } @@ -404,7 +405,7 @@ int usbCallHandler::set_gain(double newGain){ else if (newGain == 32) gainMask = 0x05; else if (newGain == 64) gainMask = 0x06; else{ - printf("Invalid gain value. Valid values are 0.1, 1, 2, 4, 8, 16, 32, 64\n"); + LIBRADOR_LOG(LOG_ERROR, "Invalid gain value. Valid values are 0.1, 1, 2, 4, 8, 16, 32, 64\n"); return -1; }