forked from eclipse-iceoryx/iceoryx
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
iox-eclipse-iceoryx#1032 Implement minimal error handling
Signed-off-by: Matthias Killat <[email protected]>
- Loading branch information
1 parent
d72c6e1
commit bb4fc25
Showing
7 changed files
with
141 additions
and
0 deletions.
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
iceoryx_hoofs/include/iceoryx_hoofs/error_handling_2/api.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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__); \ | ||
} |
4 changes: 4 additions & 0 deletions
4
iceoryx_hoofs/include/iceoryx_hoofs/error_handling_2/config.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#pragma once | ||
|
||
#include "error_codes.hpp" | ||
#include "error_levels.hpp" |
22 changes: 22 additions & 0 deletions
22
iceoryx_hoofs/include/iceoryx_hoofs/error_handling_2/error_codes.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} |
31 changes: 31 additions & 0 deletions
31
iceoryx_hoofs/include/iceoryx_hoofs/error_handling_2/error_levels.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
25 changes: 25 additions & 0 deletions
25
iceoryx_hoofs/include/iceoryx_hoofs/error_handling_2/error_reporting.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
18
iceoryx_hoofs/include/iceoryx_hoofs/error_handling_2/location.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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__ \ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |