updated error handling stuff

This commit is contained in:
[ Kristjan Komloši HomePC ]
2017-01-11 17:56:37 +01:00
parent dbc551eeb6
commit ac8aed99e3

View File

@@ -1,24 +1,31 @@
// Tinio - A tool to connect to Tinio USB IO controllers. // Tinio - A tool to connect to Tinio USB IO controllers.
#include "CyUSBCommon.h" #include "CyUSBCommon.h"
#include <cctype>
#include <cstdlib>
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <cstdlib>
using namespace std; using namespace std;
namespace errorEngine { 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 { struct errorEngineCreds {
string errorLocation; string errorLocation;
string errorName; string errorName;
int8_t errorValue; int8_t errorValue;
errorLevel_t errorLevel; errorLevel_t errorLevel;
}; };
void handleErrors(errorEngineCreds eecToEval) { void controlledCrash() {
cerr << "Triggering a controlled crash..." << endl;
exit(-177);
}
void handleErrors(errorEngineCreds eecToEval) {
switch (eecToEval.errorLevel) { switch (eecToEval.errorLevel) {
case debug: case debug:
if (globalErrLvl <= debug) { if (globalErrLvl <= debug) {
@@ -41,23 +48,12 @@ namespace errorEngine {
cerr << "FATAL ERROR: " << eecToEval.errorLocation << ": " cerr << "FATAL ERROR: " << eecToEval.errorLocation << ": "
<< eecToEval.errorName << endl; << eecToEval.errorName << endl;
cerr << "REQUESTING CRASH!" << 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 { struct accessNums_t {
@@ -67,21 +63,31 @@ struct accessNums_t {
class Device { class Device {
private: private:
errorEngine::errorEngineCreds localErrorCreds { errorEngine::errorEngineCreds localErrorCreds{
"Device handler class", "", 0, errorEngine::errorLevel_t::debug "Device handler class", "", 0, errorEngine::errorLevel_t::debug};
};
CY_HANDLE localDeviceHandle; CY_HANDLE localDeviceHandle;
accessNums_t localAccessNums; accessNums_t localAccessNums;
public: 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;
}
}
public:
Device(CY_HANDLE deviceHandle, accessNums_t accessNums) { Device(CY_HANDLE deviceHandle, accessNums_t accessNums) {
localDeviceHandle = deviceHandle; localDeviceHandle = deviceHandle;
localAccessNums = accessNums; localAccessNums = accessNums;
} }
Device(void); Device(void);
void deviceSet(accessNums_t deviceAccessNums) { void deviceSet(accessNums_t deviceAccessNums) {
CyOpen(deviceAccessNums.deviceNumber, deviceAccessNums.deviceInterfaceNumber, &localDeviceHandle); CyOpen(deviceAccessNums.deviceNumber,
deviceAccessNums.deviceInterfaceNumber, &localDeviceHandle);
} }
void deviceDestroy(); void deviceDestroy();
}; };