From b6d9ef4c7ec48a8f6cf4aaf6c3ab74bea5a77d6b Mon Sep 17 00:00:00 2001 From: Kyle Schwarz Date: Thu, 27 Apr 2023 18:52:17 +0000 Subject: [PATCH] EventManager: Add optional debug printing --- README.md | 4 ++++ api/icsneocpp/eventmanager.cpp | 22 ++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/README.md b/README.md index 6220806..a503bde 100644 --- a/README.md +++ b/README.md @@ -110,6 +110,10 @@ for(size_t i = 0; i < messageCount; i++) { icsneo_closeDevice(myDevice); ``` +### Debugging + +To enable debug printing set the `LIBICSNEO_PRINT_EVENTS` environmental variable to the desired `APIEvent::Severity` level, all `Event`s greater than or equal to that level will be printed to stderr. For example, to print all warnings and errors: `LIBICSNEO_PRINT_EVENTS=32`. + ## Building from Source ### FTD3XX Some devices require FTD3XX for USB communication so the [FTDI D3XX library](https://ftdichip.com/drivers/d3xx-drivers/) will be automatically downloaded and included. If you would like to use a system copy of D3XX instead you can set `FTD3XX_ROOT` to the path containing `f3d3xx.h` (`-DFTD3XX_ROOT=`). diff --git a/api/icsneocpp/eventmanager.cpp b/api/icsneocpp/eventmanager.cpp index 2871c44..6e6465e 100644 --- a/api/icsneocpp/eventmanager.cpp +++ b/api/icsneocpp/eventmanager.cpp @@ -1,5 +1,8 @@ #include "icsneo/api/eventmanager.h" #include +#include +#include +#include using namespace icsneo; @@ -33,6 +36,25 @@ void EventManager::cancelErrorDowngradingOnCurrentThread() { void EventManager::add(APIEvent event) { if(destructing) return; + static const auto printLevel = []() -> std::optional { + #ifdef _MSC_VER + #pragma warning(push) + #pragma warning(disable : 4996) + #endif + const auto level = std::getenv("LIBICSNEO_PRINT_EVENTS"); + #ifdef _MSC_VER + #pragma warning(pop) + #endif + if(!level) + return std::nullopt; + try { + return (uint8_t)std::stoi(level); + } catch (std::invalid_argument const&) { + return std::nullopt; + } + }(); + if(printLevel && (uint8_t)event.getSeverity() >= *printLevel) + std::cerr << event.describe() << std::endl; if(event.getSeverity() == APIEvent::Severity::Error) { // if the error was added on a thread that downgrades errors (non-user thread) std::lock_guard lk(downgradedThreadsMutex);