diff --git a/Utility/CMakeLists.txt b/Utility/CMakeLists.txt index 03bac34..8504bd4 100644 --- a/Utility/CMakeLists.txt +++ b/Utility/CMakeLists.txt @@ -1,7 +1,7 @@ cmake_minimum_required(VERSION 3.14 FATAL_ERROR) add_library(utility STATIC - Src/utility_logger.cpp + Src/utility_itmlogger.cpp ) add_library(skullc::utility ALIAS utility) diff --git a/Utility/Inc/utility_ilogger.hpp b/Utility/Inc/utility_ilogger.hpp new file mode 100644 index 0000000..a8f8bdb --- /dev/null +++ b/Utility/Inc/utility_ilogger.hpp @@ -0,0 +1,49 @@ +/* + * utility_ringbuffer.hpp + * + * Created on: Mar 12, 2021 + * Author: erki + */ + +#ifndef SKULLC_UTILITY_ILOGGER_HPP_ +#define SKULLC_UTILITY_ILOGGER_HPP_ + +#include + +namespace Utility +{ + +class ILogger +{ +public: + enum class LogLevel : std::uint8_t + { + LOG_DEBUG = 0, + LOG_INFO, + LOG_NOTICE, + LOG_WARNING, + LOG_ERROR, + LOG_FATAL + }; + + constexpr static const char* level_strs[] = { + "DEBUG", + "INFO", + "NOTICE", + "WARNING", + "ERROR", + "FATAL" + }; + + ILogger() = default; + ILogger(const ILogger&) = delete; + ILogger(ILogger&&) = delete; + + virtual ~ILogger() = default; + + virtual void log(const char* format, ...) = 0; +}; + +} + +#endif //SKULLC_UTILITY_ILOGGER_HPP_ diff --git a/Utility/Inc/utility_itmlogger.hpp b/Utility/Inc/utility_itmlogger.hpp new file mode 100644 index 0000000..563e5ce --- /dev/null +++ b/Utility/Inc/utility_itmlogger.hpp @@ -0,0 +1,30 @@ +// +// Created by erki on 14.03.21. +// + +#ifndef SKULLC_UTILITY_ITMLOGGER_HPP_ +#define SKULLC_UTILITY_ITMLOGGER_HPP_ + +#include "utility_logging.hpp" + +#include + +namespace Utility +{ + +class ITMLogger : public ILogger +{ +public: + ITMLogger() = default; + ITMLogger(const ITMLogger&) = delete; + ITMLogger(ITMLogger&&) = delete; + + void log(const char* format, ...) override; + +private: + std::array _buffer; +}; + +} + +#endif //SKULLC_UTILITY_ITMLOGGER_HPP_ diff --git a/Utility/Inc/utility_logging.hpp b/Utility/Inc/utility_logging.hpp new file mode 100644 index 0000000..2c11754 --- /dev/null +++ b/Utility/Inc/utility_logging.hpp @@ -0,0 +1,28 @@ +// +// Created by erki on 14.03.21. +// + +#ifndef UTILITY_LOGGING_HPP_ +#define UTILITY_LOGGING_HPP_ + +#include "utility_ilogger.hpp" + +#define SKULLC_LOG(sev, msg, ...) Utility::skullc_logger->log("%s: " msg "\n\r", Utility::ILogger::level_strs[std::uint8_t(sev)], ## __VA_ARGS__) +#define SKULLC_LOG_DEBUG(msg, ...) SKULLC_LOG(Utility::ILogger::LogLevel::LOG_DEBUG, msg, ## __VA_ARGS__) +#define SKULLC_LOG_INFO(msg, ...) SKULLC_LOG(Utility::ILogger::LogLevel::LOG_INFO, msg, ## __VA_ARGS__) +#define SKULLC_LOG_NOTICE(msg, ...) SKULLC_LOG(Utility::ILogger::LogLevel::LOG_NOTICE, msg, ## __VA_ARGS__) +#define SKULLC_LOG_WARNING(msg, ...) SKULLC_LOG(Utility::ILogger::LogLevel::LOG_WARNING, msg, ## __VA_ARGS__) +#define SKULLC_LOG_ERROR(msg, ...) SKULLC_LOG(Utility::ILogger::LogLevel::LOG_ERROR, msg, ## __VA_ARGS__) +#define SKULLC_LOG_FATAL(msg, ...) SKULLC_LOG(Utility::ILogger::LogLevel::LOG_FATAL, msg, ## __VA_ARGS__) + +namespace Utility +{ + +extern ILogger* skullc_logger; + +void setLogger(ILogger* log); +void setLogger(ILogger& log); + +} + +#endif //UTILITY_LOGGING_HPP_ diff --git a/Utility/Inc/utility_uartlogger.hpp b/Utility/Inc/utility_uartlogger.hpp new file mode 100644 index 0000000..fcad76f --- /dev/null +++ b/Utility/Inc/utility_uartlogger.hpp @@ -0,0 +1,39 @@ +/* + * utility_uartlogger.hpp + * + * Created on: Mar 20, 2021 + * Author: erki + */ + +#ifndef SKULLC_UTILITY_UARTLOGGER_HPP_ +#define SKULLC_UTILITY_UARTLOGGER_HPP_ + +#include "utility_logging.hpp" + +#include "usart.h" + +#include + +namespace Utility +{ + +class UARTLogger : public ILogger +{ +public: + explicit UARTLogger(UART_HandleTypeDef* huart); + + UARTLogger() = delete; + UARTLogger(const UARTLogger&) = delete; + UARTLogger(UARTLogger&&) = delete; + + void log(const char* format, ...) override; + +private: + UART_HandleTypeDef* _huart; + std::array _buffer; +}; + +} + + +#endif /* SKULLC_UTILITY_UARTLOGGER_HPP_ */ diff --git a/Utility/Src/utility_itmlogger.cpp b/Utility/Src/utility_itmlogger.cpp new file mode 100644 index 0000000..7d6a5e3 --- /dev/null +++ b/Utility/Src/utility_itmlogger.cpp @@ -0,0 +1,33 @@ +/* + * utility_logger.cpp + * + * Created on: Mar 13, 2021 + * Author: erki + */ + +#include "utility_itmlogger.hpp" + +#include +#include + +#include "main.h" + +namespace Utility +{ + +void ITMLogger::log(const char* format, ...) +{ + std::va_list args; + va_start(args, format); + + const std::int32_t len = vsnprintf(_buffer.data(), _buffer.size(), format, args); + + for (std::int32_t i = 0; i < len; i++) + { + ITM_SendChar(_buffer[i]); + } + + va_end(args); +} + +} diff --git a/Utility/Src/utility_logger.cpp b/Utility/Src/utility_logger.cpp deleted file mode 100644 index 942f9ec..0000000 --- a/Utility/Src/utility_logger.cpp +++ /dev/null @@ -1,10 +0,0 @@ -/* - * utility_logger.cpp - * - * Created on: Mar 13, 2021 - * Author: erki - */ - - - - diff --git a/Utility/Src/utility_logging.cpp b/Utility/Src/utility_logging.cpp new file mode 100644 index 0000000..eec5266 --- /dev/null +++ b/Utility/Src/utility_logging.cpp @@ -0,0 +1,25 @@ +/* + * utility_logging.cpp + * + * Created on: Mar 20, 2021 + * Author: erki + */ + +#include "utility_logging.hpp" + +namespace Utility +{ + +ILogger* skullc_logger = nullptr; + +void setLogger(ILogger* log) +{ + skullc_logger = log; +} + +void setLogger(ILogger& log) +{ + skullc_logger = &log; +} + +} diff --git a/Utility/Src/utility_uartlogger.cpp b/Utility/Src/utility_uartlogger.cpp new file mode 100644 index 0000000..3418a20 --- /dev/null +++ b/Utility/Src/utility_uartlogger.cpp @@ -0,0 +1,32 @@ +/* + * utility_uartlogger.cpp + * + * Created on: Mar 20, 2021 + * Author: erki + */ + +#include "utility_uartlogger.hpp" + +#include +#include + +namespace Utility +{ + +UARTLogger::UARTLogger(UART_HandleTypeDef* huart) + : _huart(huart) +{ } + +void UARTLogger::log(const char* format, ...) +{ + std::va_list args; + va_start(args, format); + + const std::int32_t len = vsnprintf(_buffer.data(), _buffer.size(), format, args); + + HAL_UART_Transmit(_huart, reinterpret_cast(_buffer.data()), len, 10); + + va_end(args); +} + +}