From 313f744cf1570d97b844e79047bb57852a8f1e6a Mon Sep 17 00:00:00 2001 From: PonomarevDA Date: Thu, 26 Sep 2024 19:21:21 +0300 Subject: [PATCH] refactor logging: use a general abstract logger for both cyphal and dronecan: - update dronecan submodule from v0.5.0 to v0.5.1 - cyphal does not support logging yet, so do nothing --- Libs/Dronecan | 2 +- Src/common/logging.cpp | 54 ++++++++++++++++++++++++++ Src/common/logging.hpp | 27 +++++++++++++ Src/modules/dronecan/arming/arming.cpp | 2 +- Src/modules/dronecan/pwm/PWMModule.cpp | 3 -- Src/modules/dronecan/pwm/PWMModule.hpp | 4 +- Src/modules/system/main.cpp | 4 +- Src/platform/stm32f103/CMakeLists.txt | 1 + Src/platform/stm32g0b1/CMakeLists.txt | 1 + Src/platform/ubuntu/CMakeLists.txt | 1 + 10 files changed, 90 insertions(+), 9 deletions(-) create mode 100644 Src/common/logging.cpp create mode 100644 Src/common/logging.hpp diff --git a/Libs/Dronecan b/Libs/Dronecan index caa015b..8cf4a58 160000 --- a/Libs/Dronecan +++ b/Libs/Dronecan @@ -1 +1 @@ -Subproject commit caa015bd8ad5721a9595b401f26b643cd52b3e2c +Subproject commit 8cf4a5807276951edf2a67e152c7360c2bdeb278 diff --git a/Src/common/logging.cpp b/Src/common/logging.cpp new file mode 100644 index 0000000..04390a3 --- /dev/null +++ b/Src/common/logging.cpp @@ -0,0 +1,54 @@ +/** + * This program is free software under the GNU General Public License v3. + * See for details. + * Author: Dmitry Ponomarev + * Author: Anastasiia Stepanova + */ + +#include "common/logging.hpp" +#include "common/module.hpp" + +#ifndef CONFIG_USE_DRONECAN +#define CONFIG_USE_DRONECAN 0 +#elif defined(CONFIG_USE_DRONECAN) && CONFIG_USE_DRONECAN == 1 +#include "logger.hpp" +#endif + +#ifndef CONFIG_USE_CYPHAL +#define CONFIG_USE_CYPHAL 0 +#elif defined(CONFIG_USE_CYPHAL) && CONFIG_USE_CYPHAL == 1 +#endif + + +Logging::Logging(const char* source_) : source(source_) { +} + +void Logging::log([[maybe_unused]] uint8_t severity, [[maybe_unused]] const char* text) const { +#if CONFIG_USE_DRONECAN == 1 + if (ModuleManager::get_active_protocol() == Module::Protocol::DRONECAN) { + DronecanLogger::log_global(severity, source, text); + } +#endif + +#if CONFIG_USE_CYPHAL == 1 + if (ModuleManager::get_active_protocol() == Module::Protocol::CYPHAL) { + // CyphalLogger::log_global(severity, source, text); + } +#endif +} + +void Logging::log_debug(const char* text) const { + log(0, text); +} + +void Logging::log_info(const char* text) const { + log(1, text); +} + +void Logging::log_warn(const char* text) const { + log(2, text); +} + +void Logging::log_error(const char* text) const { + log(3, text); +} diff --git a/Src/common/logging.hpp b/Src/common/logging.hpp new file mode 100644 index 0000000..16fa462 --- /dev/null +++ b/Src/common/logging.hpp @@ -0,0 +1,27 @@ +/** + * This program is free software under the GNU General Public License v3. + * See for details. + * Author: Dmitry Ponomarev + * Author: Anastasiia Stepanova + */ + +#ifndef SRC_COMMON_LOGGING_HPP_ +#define SRC_COMMON_LOGGING_HPP_ + +#include + +class Logging { +public: + explicit Logging(const char* source_); + + void log_debug(const char* text) const; + void log_info(const char* text) const; + void log_warn(const char* text) const; + void log_error(const char* text) const; + + void log(uint8_t severity, const char* text) const; +private: + const char* source{nullptr}; +}; + +#endif // SRC_COMMON_LOGGING_HPP_ diff --git a/Src/modules/dronecan/arming/arming.cpp b/Src/modules/dronecan/arming/arming.cpp index e509b93..e50f84d 100644 --- a/Src/modules/dronecan/arming/arming.cpp +++ b/Src/modules/dronecan/arming/arming.cpp @@ -11,7 +11,7 @@ #include #include -Logger logger = Logger("ArmingModule"); +DronecanLogger logger = DronecanLogger("ArmingModule"); REGISTER_MODULE(ArmingModule) diff --git a/Src/modules/dronecan/pwm/PWMModule.cpp b/Src/modules/dronecan/pwm/PWMModule.cpp index 77194f0..4694f2f 100644 --- a/Src/modules/dronecan/pwm/PWMModule.cpp +++ b/Src/modules/dronecan/pwm/PWMModule.cpp @@ -23,8 +23,6 @@ #define DEF(channel) IntParamsIndexes::PARAM_PWM_##channel##_DEF -Logger PWMModule::logger = Logger("PWMModule"); - uint16_t PWMModule::ttl_cmd = 500; uint16_t PWMModule::pwm_freq = 50; CommandType PWMModule::pwm_cmd_type = CommandType::RAW_COMMAND; @@ -43,7 +41,6 @@ void PWMModule::init() { update_params(); - logger.init("PWMModule"); for (auto param : params) { HAL::Pwm::init(param.pin); } diff --git a/Src/modules/dronecan/pwm/PWMModule.hpp b/Src/modules/dronecan/pwm/PWMModule.hpp index 9dce0e9..b1b5c12 100644 --- a/Src/modules/dronecan/pwm/PWMModule.hpp +++ b/Src/modules/dronecan/pwm/PWMModule.hpp @@ -12,7 +12,7 @@ #include "algorithms.hpp" #include "dronecan.h" #include "params.hpp" -#include "logger.hpp" +#include "common/logging.hpp" #include "peripheral/pwm/pwm.hpp" #include "common/module.hpp" #include "publisher.hpp" @@ -81,7 +81,7 @@ class PWMModule : public Module { bool verbose = false; static bool publish_error; - static Logger logger; + static inline Logging logger{"PWM"}; }; #endif // SRC_MODULES_PWM_PWMMODULE_HPP_ diff --git a/Src/modules/system/main.cpp b/Src/modules/system/main.cpp index 56c2607..9d821a3 100644 --- a/Src/modules/system/main.cpp +++ b/Src/modules/system/main.cpp @@ -8,10 +8,10 @@ #include #include "can_driver.h" #include "common/algorithms.hpp" +#include "common/logging.hpp" #include "params.hpp" -#include "logger.hpp" -static Logger logger = Logger("SYS"); +static Logging logger("SYS"); REGISTER_MODULE(SystemModule) diff --git a/Src/platform/stm32f103/CMakeLists.txt b/Src/platform/stm32f103/CMakeLists.txt index 3988f6f..466f5fe 100644 --- a/Src/platform/stm32f103/CMakeLists.txt +++ b/Src/platform/stm32f103/CMakeLists.txt @@ -6,6 +6,7 @@ add_executable(${EXECUTABLE} ${APPLICATION_SOURCES} ${BUILD_SRC_DIR}/params.cpp ${ROOT_DIR}/Src/common/algorithms.cpp + ${ROOT_DIR}/Src/common/logging.cpp ${ROOT_DIR}/Src/common/application.cpp ${ROOT_DIR}/Src/common/module.cpp diff --git a/Src/platform/stm32g0b1/CMakeLists.txt b/Src/platform/stm32g0b1/CMakeLists.txt index c8e821a..c6f5c3f 100644 --- a/Src/platform/stm32g0b1/CMakeLists.txt +++ b/Src/platform/stm32g0b1/CMakeLists.txt @@ -11,6 +11,7 @@ add_executable(${EXECUTABLE} ${APPLICATION_SOURCES} ${BUILD_SRC_DIR}/params.cpp ${ROOT_DIR}/Src/common/algorithms.cpp + ${ROOT_DIR}/Src/common/logging.cpp ${ROOT_DIR}/Src/common/application.cpp ${ROOT_DIR}/Src/common/module.cpp diff --git a/Src/platform/ubuntu/CMakeLists.txt b/Src/platform/ubuntu/CMakeLists.txt index 0034ab1..2e8578e 100644 --- a/Src/platform/ubuntu/CMakeLists.txt +++ b/Src/platform/ubuntu/CMakeLists.txt @@ -6,6 +6,7 @@ add_executable(${EXECUTABLE} ${APPLICATION_SOURCES} ${BUILD_SRC_DIR}/params.cpp ${ROOT_DIR}/Src/common/algorithms.cpp + ${ROOT_DIR}/Src/common/logging.cpp ${ROOT_DIR}/Src/common/application.cpp ${ROOT_DIR}/Src/common/module.cpp