From ac8aed99e3fcc503c57c764acffaec6168c4b10a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=5B=20Kristjan=20Komlo=C5=A1i=20HomePC=20=5D?= <[ kristjan.komlosi@gmail.com ]> Date: Wed, 11 Jan 2017 17:56:37 +0100 Subject: [PATCH] updated error handling stuff --- tinio/main.cpp | 118 ++++++++++++++++++++++++++----------------------- 1 file changed, 62 insertions(+), 56 deletions(-) diff --git a/tinio/main.cpp b/tinio/main.cpp index 9ba945e..566fdf4 100644 --- a/tinio/main.cpp +++ b/tinio/main.cpp @@ -1,63 +1,59 @@ // Tinio - A tool to connect to Tinio USB IO controllers. #include "CyUSBCommon.h" +#include +#include #include #include -#include using namespace std; namespace errorEngine { - enum errorLevel_t { debug = 0, warning, error, crash }; +enum errorLevel_t { debug = 0, warning, error, crash }; - errorLevel_t globalErrLvl = error; +errorLevel_t globalErrLvl = error; - struct errorEngineCreds { - string errorLocation; - string errorName; - int8_t errorValue; - errorLevel_t errorLevel; - }; +struct errorEngineCreds { + string errorLocation; + string errorName; + int8_t errorValue; + errorLevel_t errorLevel; +}; - void handleErrors(errorEngineCreds eecToEval) { - switch (eecToEval.errorLevel) { - case debug: - if (globalErrLvl <= debug) { - cerr << "Debug: " << eecToEval.errorLocation << ": " - << eecToEval.errorName << endl; - } +void controlledCrash() { + cerr << "Triggering a controlled crash..." << endl; + exit(-177); +} - case warning: - if (globalErrLvl <= warning) { - cerr << "Warning: " << eecToEval.errorLocation << ": " - << eecToEval.errorName << endl; - } - case error: - if (globalErrLvl <= error) { - cerr << "ERROR: " << eecToEval.errorLocation << ": " - << eecToEval.errorName << endl; - } - case crash: - if (globalErrLvl <= crash) { - cerr << "FATAL ERROR: " << eecToEval.errorLocation << ": " - << eecToEval.errorName << endl; - cerr << "REQUESTING CRASH!" << endl; - } + +void handleErrors(errorEngineCreds eecToEval) { + switch (eecToEval.errorLevel) { + case debug: + if (globalErrLvl <= debug) { + cerr << "Debug: " << eecToEval.errorLocation << ": " + << eecToEval.errorName << endl; + } + + case warning: + if (globalErrLvl <= warning) { + cerr << "Warning: " << eecToEval.errorLocation << ": " + << eecToEval.errorName << endl; + } + case error: + if (globalErrLvl <= error) { + cerr << "ERROR: " << eecToEval.errorLocation << ": " + << eecToEval.errorName << endl; + } + case crash: + if (globalErrLvl <= crash) { + cerr << "FATAL ERROR: " << eecToEval.errorLocation << ": " + << eecToEval.errorName << endl; + cerr << "REQUESTING CRASH!" << endl; + controlledCrash(); } } +} - void controlledCrash() { - cerr << "Triggering a controlled crash..." << endl; - 3 / 0; // triggers a floating point error - return; - } - int callExternFun(CY_RETURN_STATUS function()) { - int retval=function(); - if (retval != CY_SUCCESS) { - this.localErrorCreds.errorValue = retval; - - } - } } struct accessNums_t { @@ -67,21 +63,31 @@ struct accessNums_t { class Device { private: - errorEngine::errorEngineCreds localErrorCreds { - "Device handler class", "", 0, errorEngine::errorLevel_t::debug - }; + errorEngine::errorEngineCreds localErrorCreds{ + "Device handler class", "", 0, errorEngine::errorLevel_t::debug}; CY_HANDLE localDeviceHandle; accessNums_t localAccessNums; -public: - Device(CY_HANDLE deviceHandle, accessNums_t accessNums) { - localDeviceHandle = deviceHandle; - localAccessNums = accessNums; + int callExternFun(CY_RETURN_STATUS function()) { + int retval = function(); + if (retval != CY_SUCCESS) { + this->localErrorCreds.errorValue = retval; + localErrorCreds.errorName = "error number "; + localErrorCreds.errorName.append(itoa(localErrorCreds.errorValue)); + errorEngine::handleErrors(localErrorCreds); + return -177; + } } - Device(void); - void deviceSet(accessNums_t deviceAccessNums) { - CyOpen(deviceAccessNums.deviceNumber, deviceAccessNums.deviceInterfaceNumber, &localDeviceHandle); - } - void deviceDestroy(); + public: + Device(CY_HANDLE deviceHandle, accessNums_t accessNums) { + localDeviceHandle = deviceHandle; + localAccessNums = accessNums; + } + Device(void); + void deviceSet(accessNums_t deviceAccessNums) { + CyOpen(deviceAccessNums.deviceNumber, + deviceAccessNums.deviceInterfaceNumber, &localDeviceHandle); + } + void deviceDestroy(); };