From 425bec953e1fe61a5260fbf83d4bfadf90412996 Mon Sep 17 00:00:00 2001 From: Andreas Heinrich Date: Thu, 16 May 2024 13:08:36 +0200 Subject: [PATCH] Refactor error framework C++ interface (#141) 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 --- ev-dev-tools/src/ev_cli/__init__.py | 2 +- .../ev_cli/templates/interface-Base.hpp.j2 | 67 ++++++++++++------- .../ev_cli/templates/interface-Exports.hpp.j2 | 55 +++++++++------ .../src/ev_cli/templates/ld-ev.cpp.j2 | 7 +- .../src/ev_cli/templates/ld-ev.hpp.j2 | 4 +- 5 files changed, 84 insertions(+), 51 deletions(-) diff --git a/ev-dev-tools/src/ev_cli/__init__.py b/ev-dev-tools/src/ev_cli/__init__.py index 928b2e04..8d9bfa83 100644 --- a/ev-dev-tools/src/ev_cli/__init__.py +++ b/ev-dev-tools/src/ev_cli/__init__.py @@ -1,2 +1,2 @@ """EVerest command line utility.""" -__version__ = '0.0.24' +__version__ = '0.1.0' diff --git a/ev-dev-tools/src/ev_cli/templates/interface-Base.hpp.j2 b/ev-dev-tools/src/ev_cli/templates/interface-Base.hpp.j2 index a9b69c50..367f9df5 100644 --- a/ev-dev-tools/src/ev_cli/templates/interface-Base.hpp.j2 +++ b/ev-dev-tools/src/ev_cli/templates/interface-Base.hpp.j2 @@ -8,12 +8,39 @@ #include #include #include +#include +#include +#include #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 @@ -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 error_state_monitor; + std::shared_ptr error_factory; protected: {% if not cmds %} @@ -84,6 +102,7 @@ protected: private: Everest::ModuleAdapter* const _ev; const std::string _name; + std::shared_ptr error_manager; // helper function for getting all commands void _gather_cmds(std::vector& cmds) override { diff --git a/ev-dev-tools/src/ev_cli/templates/interface-Exports.hpp.j2 b/ev-dev-tools/src/ev_cli/templates/interface-Exports.hpp.j2 index 94364268..a288705c 100644 --- a/ev-dev-tools/src/ev_cli/templates/interface-Exports.hpp.j2 +++ b/ev-dev-tools/src/ev_cli/templates/interface-Exports.hpp.j2 @@ -6,12 +6,35 @@ {{ print_template_info('3') }} #include +#include +#include +#include +#include #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; @@ -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 @@ -121,7 +133,10 @@ public: {% endfor %} {% endif %} + std::shared_ptr error_state_monitor; + private: + std::shared_ptr error_manager; Everest::ModuleAdapter* const _adapter; Requirement _req; }; diff --git a/ev-dev-tools/src/ev_cli/templates/ld-ev.cpp.j2 b/ev-dev-tools/src/ev_cli/templates/ld-ev.cpp.j2 index 8f827bf2..a8b87206 100644 --- a/ev-dev-tools/src/ev_cli/templates/ld-ev.cpp.j2 +++ b/ev-dev-tools/src/ev_cli/templates/ld-ev.cpp.j2 @@ -117,11 +117,10 @@ std::vector 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 diff --git a/ev-dev-tools/src/ev_cli/templates/ld-ev.hpp.j2 b/ev-dev-tools/src/ev_cli/templates/ld-ev.hpp.j2 index dac58397..fcb7e5b6 100644 --- a/ev-dev-tools/src/ev_cli/templates/ld-ev.hpp.j2 +++ b/ev-dev-tools/src/ev_cli/templates/ld-ev.hpp.j2 @@ -24,8 +24,8 @@ std::vector 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 %}