From 6017069c61f3f549edcf9e462c9eea2931411133 Mon Sep 17 00:00:00 2001 From: kongr45gpen <electrovesta@gmail.com> Date: Thu, 8 Aug 2019 14:06:30 +0300 Subject: [PATCH] Use the Logger to send all the messages Fix a bug with error names not showing up in the logs --- CMakeLists.txt | 2 ++ inc/Logger.hpp | 1 + src/Logger.cpp | 8 +++++++- src/Platform/x86/ErrorHandler.cpp | 10 +++++----- src/Platform/x86/Logger.cpp | 9 ++++++++- src/Platform/x86/Service.cpp | 23 +++++++++++++---------- src/Platform/x86/main.cpp | 2 -- 7 files changed, 36 insertions(+), 19 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index ab4a0c23..1be913a6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -43,6 +43,8 @@ add_executable(ecss_services $<TARGET_OBJECTS:common> ${x86_main_SRC} ) +set_target_properties(ecss_services + PROPERTIES COMPILE_DEFINITIONS LOGLEVEL_TRACE) IF (EXISTS "${PROJECT_SOURCE_DIR}/lib/Catch2/CMakeLists.txt") # Gather all the .cpp files corresponding to tests diff --git a/inc/Logger.hpp b/inc/Logger.hpp index 894461ea..48a3ab19 100644 --- a/inc/Logger.hpp +++ b/inc/Logger.hpp @@ -169,5 +169,6 @@ Logger::LogEntry& operator<<(Logger::LogEntry& entry, const T value) { return entry; } +Logger::LogEntry& operator<<(Logger::LogEntry& entry, const std::string & value); #endif //ECSS_SERVICES_LOGGER_HPP diff --git a/src/Logger.cpp b/src/Logger.cpp index 990c49df..bb3e40d8 100644 --- a/src/Logger.cpp +++ b/src/Logger.cpp @@ -1,10 +1,16 @@ #include <Logger.hpp> +// Reimplementation of the function for variable C strings +template <> +Logger::LogEntry& operator<<(Logger::LogEntry& entry, char* value) { + entry.message.append(value); + return entry; +} + // Reimplementation of the function for C strings template <> Logger::LogEntry& operator<<(Logger::LogEntry& entry, const char* value) { entry.message.append(value); - return entry; } diff --git a/src/Platform/x86/ErrorHandler.cpp b/src/Platform/x86/ErrorHandler.cpp index a59e3651..ffddffd2 100644 --- a/src/Platform/x86/ErrorHandler.cpp +++ b/src/Platform/x86/ErrorHandler.cpp @@ -7,6 +7,7 @@ #include <cxxabi.h> #include <ErrorHandler.hpp> #include <Message.hpp> +#include <Logger.hpp> // TODO: Find a way to reduce the number of copies of this chunk template void ErrorHandler::logError(const Message&, ErrorHandler::AcceptanceErrorType); @@ -18,23 +19,22 @@ template void ErrorHandler::logError(ErrorHandler::InternalErrorType); template <typename ErrorType> void ErrorHandler::logError(const Message& message, ErrorType errorType) { - std::cerr + LOG_ERROR /* * Gets the error class name from the template * Note: This is g++-dependent code and should only be used for debugging. */ << abi::__cxa_demangle(typeid(ErrorType).name(), nullptr, nullptr, nullptr) << " Error " << "[" << static_cast<uint16_t>(message.serviceType) << "," << static_cast<uint16_t>(message.messageType) - << "]: " << errorType << std::endl; + << "]: " << errorType; } template <typename ErrorType> void ErrorHandler::logError(ErrorType errorType) { - std::cerr + LOG_ERROR /* * Gets the error class name from the template * Note: This is g++-dependent code and should only be used for debugging. */ - << abi::__cxa_demangle(typeid(ErrorType).name(), nullptr, nullptr, nullptr) << " Error: " << errorType - << std::endl; + << abi::__cxa_demangle(typeid(ErrorType).name(), nullptr, nullptr, nullptr) << " Error: " << errorType; } diff --git a/src/Platform/x86/Logger.cpp b/src/Platform/x86/Logger.cpp index 9ee1b8b0..ed4fe3a0 100644 --- a/src/Platform/x86/Logger.cpp +++ b/src/Platform/x86/Logger.cpp @@ -24,7 +24,6 @@ void Logger::log(Logger::LogLevel level, String<LOGGER_MAX_MESSAGE_SIZE> & messa } else if (level <= Logger::debug) { name = "debug"; colour = "90"; // bright black - keepColour = true; } else if (level <= Logger::info) { name = "info"; colour = "32"; // green @@ -59,3 +58,11 @@ void Logger::log(Logger::LogLevel level, String<LOGGER_MAX_MESSAGE_SIZE> & messa ss << "\n"; std::cerr << ss.str(); } + +// Reimplementation of the log function for C++ strings +// This is kept in the Platform files, since we don't want to mess with std::strings in the microcontroller +Logger::LogEntry& operator<<(Logger::LogEntry& entry, const std::string & value) { + entry.message.append(value.c_str()); + + return entry; +} diff --git a/src/Platform/x86/Service.cpp b/src/Platform/x86/Service.cpp index 7c20d2f9..c44acb96 100644 --- a/src/Platform/x86/Service.cpp +++ b/src/Platform/x86/Service.cpp @@ -1,21 +1,24 @@ #include <iostream> #include <iomanip> +#include <Logger.hpp> #include "Service.hpp" void Service::storeMessage(Message& message) { // appends the remaining bits to complete a byte message.finalize(); + // Create a new stream to display the packet + std::ostringstream ss; + // Just print it to the screen - std::cout << "New " << ((message.packetType == Message::TM) ? "TM" : "TC") << "[" - << std::hex - // << std::dec - << static_cast<int>(message.serviceType) << "," << static_cast<int>(message.messageType) - << "] message!\n"; - // std::cout << std::hex << std::setfill('0') << std::setw(2); - for (int i = 0; i < message.dataSize; i++) { - std::cout << static_cast<int>(message.data[i]); - std::cout << " "; + ss << "New " << ((message.packetType == Message::TM) ? "TM" : "TC") << "[" + << std::hex + << static_cast<int>(message.serviceType) << "," << static_cast<int>(message.messageType) + << "] message! "; + + for (unsigned int i = 0; i < message.dataSize; i++) { + ss << static_cast<int>(message.data[i]) << " "; // Ignore-MISRA } - std::cout << std::endl; + + LOG_DEBUG << ss.str(); } diff --git a/src/Platform/x86/main.cpp b/src/Platform/x86/main.cpp index f53c4ae3..2e15a3af 100644 --- a/src/Platform/x86/main.cpp +++ b/src/Platform/x86/main.cpp @@ -1,5 +1,3 @@ -#define LOGLEVEL_TRACE - #include <iostream> #include <ServicePool.hpp> #include <Logger.hpp> -- GitLab