Skip to content

Commit

Permalink
Refactor error framework C++ interface (#141)
Browse files Browse the repository at this point in the history
Implementation Base Template:
* Add error_manager
* Add error_state_monitor
* Add error_factory
* Update raise_error interface
* Update clear_error interface

Export Template
* Add error_manager
* Add error_state_monitor
* Update subscribe_error interface

ld-ev template
* Update subscribe_global_all_errors interface

Signed-off-by: Andreas Heinrich <[email protected]>
  • Loading branch information
andistorm authored May 16, 2024
1 parent 38f6033 commit 425bec9
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 51 deletions.
2 changes: 1 addition & 1 deletion ev-dev-tools/src/ev_cli/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
"""EVerest command line utility."""
__version__ = '0.0.24'
__version__ = '0.1.0'
67 changes: 43 additions & 24 deletions ev-dev-tools/src/ev_cli/templates/interface-Base.hpp.j2
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,39 @@
#include <framework/ModuleAdapter.hpp>
#include <utils/types.hpp>
#include <utils/error.hpp>
#include <utils/error/error_state_monitor.hpp>
#include <utils/error/error_manager_impl.hpp>
#include <utils/error/error_factory.hpp>

#include "Types.hpp"

class {{ info.class_name }} : public Everest::ImplementationBase {
public:
{{ info.class_name }}(Everest::ModuleAdapter* ev, const std::string& name) : Everest::ImplementationBase(), _ev(ev), _name(name) {};
{{ info.class_name }}(Everest::ModuleAdapter* ev, const std::string& name)
: Everest::ImplementationBase(),
_ev(ev),
_name(name) {
if (ev == nullptr) {
EVLOG_error << "ev is nullptr, please check the initialization of the module";
error_manager = nullptr;
error_state_monitor = nullptr;
error_factory = nullptr;
EVLOG_error << "error_manager, error_state_monitor and error_factory are nullptr";
} else {
error_manager = ev->get_error_manager_impl(name);
if (error_manager == nullptr) {
EVLOG_error << "error_manager is nullptr";
}
error_state_monitor = ev->get_error_state_monitor_impl(name);
if (error_state_monitor == nullptr) {
EVLOG_error << "error_state_monitor is nullptr";
}
error_factory = ev->get_error_factory(name);
if (error_factory == nullptr) {
EVLOG_error << "error_factory is nullptr";
}
}
}

{% if not vars %}
// no variables defined for this interface
Expand Down Expand Up @@ -43,33 +70,24 @@ public:
{% endfor %}
{% endif %}

{% if not errors %}
// no errors defined for this interface
{% else %}
// raise functions for errors
{% for error in errors %}
Everest::error::ErrorHandle raise_{{ error.namespace }}_{{ error.name }}(const std::string& message, const Everest::error::Severity& severity=Everest::error::Severity::Low) {
return _ev->raise_error(
_name,
"{{ error.namespace }}/{{ error.name }}",
message,
severity
);
void raise_error(const Everest::error::Error& error) {
error_manager->raise_error(error);
}
{% endfor %}
// request clear functions for errors
{% for error in errors %}
void request_clear_all_{{ error.namespace }}_{{ error.name }}() {
_ev->request_clear_all_errors_of_type_of_module(_name, "{{ error.namespace }}/{{ error.name }}");

void clear_error(const Everest::error::ErrorType& type, const bool clear_all = false) {
error_manager->clear_error(type, clear_all);
}
{% endfor %}
void request_clear_error(const Everest::error::ErrorHandle& handle) {
_ev->request_clear_error_uuid(_name, handle);

void clear_error(const Everest::error::ErrorType& type, const Everest::error::ErrorSubType& sub_type) {
error_manager->clear_error(type, sub_type);
}
void request_clear_all_errors() {
_ev->request_clear_all_errors_of_module(_name);

void clear_all_errors_of_impl() {
error_manager->clear_all_errors();
}
{% endif %}

std::shared_ptr<Everest::error::ErrorStateMonitor> error_state_monitor;
std::shared_ptr<Everest::error::ErrorFactory> error_factory;

protected:
{% if not cmds %}
Expand All @@ -84,6 +102,7 @@ protected:
private:
Everest::ModuleAdapter* const _ev;
const std::string _name;
std::shared_ptr<Everest::error::ErrorManagerImpl> error_manager;

// helper function for getting all commands
void _gather_cmds(std::vector<Everest::cmd>& cmds) override {
Expand Down
55 changes: 35 additions & 20 deletions ev-dev-tools/src/ev_cli/templates/interface-Exports.hpp.j2
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,35 @@
{{ print_template_info('3') }}

#include <framework/ModuleAdapter.hpp>
#include <utils/types.hpp>
#include <utils/error.hpp>
#include <utils/error/error_state_monitor.hpp>
#include <utils/error/error_manager_req.hpp>

#include "Types.hpp"

class {{ info.class_name }} {
public:
{{ info.class_name }}(Everest::ModuleAdapter* adapter, Requirement req, const std::string& module_id) : module_id(module_id), _adapter(adapter), _req(req){};
{{ info.class_name }}(Everest::ModuleAdapter* adapter, Requirement req, const std::string& module_id)
: module_id(module_id),
_adapter(adapter),
_req(req) {
if (adapter == nullptr) {
EVLOG_error << "adapter is nullptr, please check the initialization of the module";
error_manager = nullptr;
error_state_monitor = nullptr;
EVLOG_error << "error_manager and error_state_monitor are nullptr";
} else {
error_manager = adapter->get_error_manager_req(req);
if (error_manager == nullptr) {
EVLOG_error << "error_manager is nullptr";
}
error_state_monitor = adapter->get_error_state_monitor_req(req);
if (error_state_monitor == nullptr) {
EVLOG_error << "error_state_monitor is nullptr";
}
}
}

const std::string module_id;

Expand Down Expand Up @@ -44,31 +67,20 @@ public:
{% endfor %}
{% endif %}

{% if not errors %}
// This interface does not export any errors to subscribe to
{% else %}
// errors available for subscription
{% for error in errors %}
void subscribe_error_{{ error.namespace }}_{{ error.name }}(
const Everest::error::ErrorCallback& error_listener,
const Everest::error::ErrorCallback& error_cleared_listener
void subscribe_error(
const Everest::error::ErrorType& type,
const Everest::error::ErrorCallback& callback,
const Everest::error::ErrorCallback& clear_callback
) {
_adapter->subscribe_error(_req, "{{ error.namespace }}/{{ error.name }}", error_listener);
_adapter->subscribe_error_cleared(_req, "{{ error.namespace }}/{{ error.name }}", error_cleared_listener);
error_manager->subscribe_error(type, callback, clear_callback);
}
{% if not loop.last %}

{% endif %}
{% endfor %}
void subscribe_all_errors(
const Everest::error::ErrorCallback& error_listener,
const Everest::error::ErrorCallback& error_cleared_listener
const Everest::error::ErrorCallback& callback,
const Everest::error::ErrorCallback& clear_callback
) {
{% for error in errors %}
subscribe_error_{{ error.namespace }}_{{ error.name }}(error_listener, error_cleared_listener);
{% endfor %}
error_manager->subscribe_all_errors(callback, clear_callback);
}
{% endif %}

{% if not cmds %}
// this interface does not export any commands to call
Expand Down Expand Up @@ -121,7 +133,10 @@ public:
{% endfor %}
{% endif %}

std::shared_ptr<Everest::error::ErrorStateMonitor> error_state_monitor;

private:
std::shared_ptr<Everest::error::ErrorManagerReq> error_manager;
Everest::ModuleAdapter* const _adapter;
Requirement _req;
};
Expand Down
7 changes: 3 additions & 4 deletions ev-dev-tools/src/ev_cli/templates/ld-ev.cpp.j2
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,10 @@ std::vector<Everest::cmd> everest_register(const json& connections) {

{% if info.enable_global_errors %}
void subscribe_global_all_errors(
const Everest::error::ErrorCallback& error_listener,
const Everest::error::ErrorCallback& error_cleared_listener
const Everest::error::ErrorCallback& callback,
const Everest::error::ErrorCallback& clear_callback
) {
adapter.subscribe_all_errors(error_listener);
adapter.subscribe_all_errors_cleared(error_cleared_listener);
adapter.subscribe_global_all_errors(callback, clear_callback);
}
{% endif %}
} // namespace module
Expand Down
4 changes: 2 additions & 2 deletions ev-dev-tools/src/ev_cli/templates/ld-ev.hpp.j2
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ std::vector<Everest::cmd> everest_register(const json& connections);

{% if info.enable_global_errors %}
void subscribe_global_all_errors(
const Everest::error::ErrorCallback& error_listener,
const Everest::error::ErrorCallback& error_cleared_listener
const Everest::error::ErrorCallback& callback,
const Everest::error::ErrorCallback& clear_callback
);
{% endif %}

Expand Down

0 comments on commit 425bec9

Please sign in to comment.