Skip to content

Commit

Permalink
iox-eclipse-iceoryx#1032 Implement minimal error handling
Browse files Browse the repository at this point in the history
Signed-off-by: Matthias Killat <[email protected]>
  • Loading branch information
MatthiasKillat committed Jun 16, 2022
1 parent d72c6e1 commit bb4fc25
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 0 deletions.
18 changes: 18 additions & 0 deletions iceoryx_hoofs/include/iceoryx_hoofs/error_handling_2/api.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include "error_reporting.hpp"

using namespace eh;

// macros are required for SOURCE_LOCATION
// macros start with IOX_ but constants do not (avoids some clashes)

#define IOX_RAISE(...) \
{ \
raise(SOURCE_LOCATION, ##__VA_ARGS__); \
}

#define IOX_FATAL(...) \
{ \
raise(SOURCE_LOCATION, FATAL, ##__VA_ARGS__); \
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#pragma once

#include "error_codes.hpp"
#include "error_levels.hpp"
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once

#pragma once

#include <stdint.h>


using error_code_t = uint32_t;

enum class ErrorCode : error_code_t
{
Default = 0,
OutOfMemory = 1
};

static const char* errorCodes[] = {"Default", "OutOfMemory"};

const char* error_code_to_name(ErrorCode code)

{
return errorCodes[(error_code_t)code];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#pragma once

#include <stdint.h>

namespace eh
{
using error_level_t = uint32_t;

enum class ErrorLevel : error_level_t
{
Fatal = 0,
Error = 1,
Warning = 2
};

static const char* errorLevels[] = {"Fatal", "Error", "Warning"};


// note that the array apporach is by design (instead of switch) to allow a fast lookup
const char* error_level_to_name(ErrorLevel level)

{
return errorLevels[(error_level_t)level];
}

} // namespace eh

// convenience
#define FATAL ErrorLevel::Fatal // mandatory, should always be 0 (create a test)
#define ERROR ErrorLevel::Error
#define WARNING ErrorLevel::Warning
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#pragma once

#include "error_codes.hpp" // this must be provided by the module
#include "location.hpp"

#include <iostream>

namespace eh
{
template <typename Level>
void raise(const SourceLocation& location, Level level)
{
auto name = error_level_to_name(level);
std::cout << name << "@" << location.file << " " << location.line << " " << location.function << std::endl;
}

template <typename Level, typename Code>
void raise(const SourceLocation& location, Level level, Code code)
{
auto levelName = error_level_to_name(level);
auto codeName = error_code_to_name(code);
std::cout << levelName << "@" << location.file << " " << location.line << " " << location.function << " : "
<< codeName << std::endl;
}
} // namespace eh
18 changes: 18 additions & 0 deletions iceoryx_hoofs/include/iceoryx_hoofs/error_handling_2/location.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

namespace eh
{
struct SourceLocation
{
const char* file;
unsigned line;
const char* function;
};

} // namespace eh

#define SOURCE_LOCATION \
eh::SourceLocation \
{ \
__FILE__, __LINE__, __func__ \
}
23 changes: 23 additions & 0 deletions iceoryx_hoofs/test/moduletests/test_error_handling_2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "iceoryx_hoofs/error_handling_2/api.hpp"
#include "iceoryx_hoofs/error_handling_2/config.hpp"

#include "test.hpp"

#include <iostream>

namespace
{
using namespace ::testing;
using std::cout;
using std::endl;

TEST(EH_test, raise)
{
IOX_RAISE(WARNING);
IOX_RAISE(WARNING, ErrorCode::OutOfMemory);

IOX_FATAL();
IOX_FATAL(ErrorCode::OutOfMemory);
}

} // namespace

0 comments on commit bb4fc25

Please sign in to comment.