Skip to content

Commit

Permalink
refactor logging: use a general abstract logger for both cyphal and d…
Browse files Browse the repository at this point in the history
…ronecan:

- update dronecan submodule from v0.5.0 to v0.5.1
- cyphal does not support logging yet, so do nothing
  • Loading branch information
PonomarevDA committed Sep 28, 2024
1 parent 57c3890 commit 313f744
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 9 deletions.
54 changes: 54 additions & 0 deletions Src/common/logging.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* This program is free software under the GNU General Public License v3.
* See <https://www.gnu.org/licenses/> for details.
* Author: Dmitry Ponomarev <[email protected]>
* Author: Anastasiia Stepanova <[email protected]>
*/

#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);
}
27 changes: 27 additions & 0 deletions Src/common/logging.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* This program is free software under the GNU General Public License v3.
* See <https://www.gnu.org/licenses/> for details.
* Author: Dmitry Ponomarev <[email protected]>
* Author: Anastasiia Stepanova <[email protected]>
*/

#ifndef SRC_COMMON_LOGGING_HPP_
#define SRC_COMMON_LOGGING_HPP_

#include <stdint.h>

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_
2 changes: 1 addition & 1 deletion Src/modules/dronecan/arming/arming.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include <params.hpp>
#include <logger.hpp>

Logger logger = Logger("ArmingModule");
DronecanLogger logger = DronecanLogger("ArmingModule");


REGISTER_MODULE(ArmingModule)
Expand Down
3 changes: 0 additions & 3 deletions Src/modules/dronecan/pwm/PWMModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -43,7 +41,6 @@ void PWMModule::init() {

update_params();

logger.init("PWMModule");
for (auto param : params) {
HAL::Pwm::init(param.pin);
}
Expand Down
4 changes: 2 additions & 2 deletions Src/modules/dronecan/pwm/PWMModule.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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_
4 changes: 2 additions & 2 deletions Src/modules/system/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
#include <storage.h>
#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)

Expand Down
1 change: 1 addition & 0 deletions Src/platform/stm32f103/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions Src/platform/stm32g0b1/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions Src/platform/ubuntu/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 313f744

Please sign in to comment.