Implementation of a Working Error handler

master
Kerem Yollu 4 years ago
commit 3baf93742c

@ -0,0 +1,21 @@
cpp_src = $(wildcard *.cpp)
#c_src = $(wildcard ../c/algorithms/*.c) $(wildcard driver/ma120x0/*.c)
cpp_obj = $(cpp_src:.cpp=.o)
c_obj = $(c_src:.c=.o)
CC = g++
CFLAGS = -Wall -pedantic
LDFLAGS =
EXEC = runtest
all : $(EXEC)
$(EXEC): $(cpp_obj) $(c_obj)
$(CC) -o $@ $^ $(LDFLAGS)
clean:
rm -rf $(c_obj) $(cpp_obj) $(EXEC)
cleanall:
rm -rf $(c_obj) $(cpp_obj) $(EXEC)

@ -0,0 +1,54 @@
#include "errorHandling.h"
ErrorHandler::ErrorHandler(): emptyIndex(0)
{
}
int ErrorHandler::getLookUpIndex(int errNo)
{
int i = 0;
for(i = 0; i < emptyIndex; i++)
{
if(errNo == errorLookup[i].errNo)
{
return i;
}
}
return -1;
}
void ErrorHandler::handleError(int errNo)
{
int index = 0;
index = getLookUpIndex(errNo);
if(index < 0)
{
std::cout << "The given error number does not exist\n" << "Tipp: use \"addNewError(Integer, String)\""<< std::endl;
exit(1);
}
std::cout << "Error | " << errNo << " | \"" << errorLookup[index].errMessage << "\""<<std::endl;
exit(1);
}
void ErrorHandler::addNewError(const int& no, const std::string& message)
{
if(emptyIndex >= MAX_NUMBER_OF_ERRORS) // Check if list is full
{
std::cout << "Error | Ultimate | List is full" << std::endl;
exit(1);
}
if(getLookUpIndex(no) >= 0) // Check if Error No already exists
{
std::cout << "Error | Intern | Error No: "<< no << " Exists ! PLease choose a different number !" << std::endl;
exit(1);
}
errorLookup[emptyIndex].errNo = no;
errorLookup[emptyIndex].errMessage = message;
emptyIndex++;
}

@ -0,0 +1,50 @@
/*
*
*
*
*
*
*
*
*
*
*
* TODO : IMplement it as singleton
*/
#ifndef _ERRORHANDLIG_H_
#define _ERRORHANDLIG_H_
#include <iostream>
#include <array>
#include <string>
#define MAX_NUMBER_OF_ERRORS 255
/*
* Singleton Pattern based on : https://stackoverflow.com/questions/1008019/c-singleton-design-pattern/1008289
*
*/
class ErrorHandler
{
public:
ErrorHandler();
void addNewError(const int& no, const std::string& message);
void handleError(int no);
private:
struct error_t {
int errNo;
std::string errMessage;
};
unsigned int emptyIndex;
std::array <error_t,MAX_NUMBER_OF_ERRORS> errorLookup;
int getLookUpIndex(int errNo); // If error number exists returns the index otherwise -1
};
#endif // _ERRORHANDLIG_H_

@ -0,0 +1,47 @@
/*
*
*
*
*
*
*
*
*
*
*
*
*/
#include "errorHandling.h"
#include <iostream>
ErrorHandler errorHandle;
int initPlatform()
{
// Dev. Module initialisation "Ex i2c_init() ...."
return 0;
}
int main(int argc, char *argv[])
{
errorHandle.addNewError(-34,"Test eroor 1");
errorHandle.addNewError(-33,"Test eroor 2");
errorHandle.addNewError(-23,"Test eroor 3");
errorHandle.addNewError(-1,"Test eroor 4");
errorHandle.addNewError(-34, "Test eroor 5");
errorHandle.addNewError(-34,"Test eroor 6");
// errorHandle.handleError(-34);
errorHandle.addNewError(-34,"Test eroor");
// Init
std::cout << "Main" << std::endl;
return 1;
}

Binary file not shown.
Loading…
Cancel
Save