From 045302cc9653c9839fa9b1e27b2f86c97199e198 Mon Sep 17 00:00:00 2001 From: kalipso Date: Sun, 19 Feb 2023 22:47:24 +0100 Subject: [PATCH] add loglevel commands --- logging.hpp | 22 +++++++++++++++++----- main.cpp | 31 +++++++++++++++++++------------ 2 files changed, 36 insertions(+), 17 deletions(-) diff --git a/logging.hpp b/logging.hpp index 45b6ce5..0b5910a 100644 --- a/logging.hpp +++ b/logging.hpp @@ -11,7 +11,7 @@ enum class LogLevel { TRACE, }; -inline std::string_view get_loglevel_string(LogLevel level) { +inline constexpr std::string_view get_loglevel_string(LogLevel level) { switch (level) { case (LogLevel::ERROR): { return "[Error]"; @@ -61,23 +61,35 @@ class log { template static void error(Args&&... args) { - log_impl_begin(LogLevel::ERROR); + if (!log_impl_begin(LogLevel::ERROR)) { + return; + } + log_impl(std::forward(args)...); } template static void debug(Args&&... args) { - log_impl_begin(LogLevel::DEBUG); + if (!log_impl_begin(LogLevel::DEBUG)) { + return; + } + log_impl(std::forward(args)...); } template static void info(Args&&... args) { - log_impl_begin(LogLevel::INFO); + if (!log_impl_begin(LogLevel::INFO)) { + return; + } + log_impl(std::forward(args)...); } - static void set_loglevel(LogLevel level) { log_level = level; } + static void set_loglevel(LogLevel level) { + info("New LogLevel: ", get_loglevel_string(level)); + log_level = level; + } private: static bool log_impl_begin(LogLevel level) { diff --git a/main.cpp b/main.cpp index 3002c35..e3a09ca 100644 --- a/main.cpp +++ b/main.cpp @@ -28,6 +28,10 @@ extern SPI_HandleTypeDef hspi1; void print_version(void) { log::info("running PentaTrack v0.1.3"); } void print_help(void); +void loglevel_error(void) { log::set_loglevel(LogLevel::ERROR); } +void loglevel_info(void) { log::set_loglevel(LogLevel::INFO); } +void loglevel_debug(void) { log::set_loglevel(LogLevel::DEBUG); } + template class cmd_holder { public: @@ -63,6 +67,7 @@ class cmd_holder { std::array commands; }; +template class cmd_handler { public: static constexpr auto MaxCmdLength = 24; @@ -70,11 +75,9 @@ class cmd_handler { using iterator_t = array_t::iterator; using const_iterator_t = array_t::const_iterator; - consteval cmd_handler() - : commands{std::make_tuple("ver", "Prints current version.", - &print_version), - std::make_tuple("help", "Prints available commands", - &print_help)}, + template + consteval cmd_handler(Args &&...command_list) + : commands{std::forward(command_list)...}, symbols{}, iterator{symbols.begin()} {} @@ -121,19 +124,21 @@ class cmd_handler { void print_help_() const { commands.print_help(); } private: - cmd_holder<2> commands; + cmd_holder commands; array_t symbols; iterator_t iterator; bool ShouldExecute = false; }; -static cmd_handler commands{}; +static cmd_handler<5> commands{ + std::make_tuple("ver", "Prints current version.", &print_version), + std::make_tuple("error", "Set LogLevel to Error.", &loglevel_error), + std::make_tuple("info", "Set LogLevel to Info.", &loglevel_info), + std::make_tuple("debug", "Set LogLevel to Debug.", &loglevel_debug), + std::make_tuple("help", "Prints available commands", &print_help)}; void print_help(void) { commands.print_help_(); } -// cmd_handler::array_t cmd_handler::symbols = cmd_handler::array_t{}; -// cmd_handler::iterator_t cmd_handler::iterator = cmd_handler::iterator_t{}; - // This prevent name mangling for functions used in C/assembly files. extern "C" { void SysTick_Handler(void) { @@ -183,7 +188,8 @@ void initGPIO() { GPIO_ConfigSPI.Pin = LoRa_CS_Pin | LoRa_RESET_Pin; // bare metal init of led1: - // volatile uint32_t* CRH = reinterpret_cast(0x40011000 + 0x04); + // volatile uint32_t* CRH = reinterpret_cast(0x40011000 + + // 0x04); //*CRH |= 0x3; //*CRH &= (~0xC); @@ -191,7 +197,8 @@ void initGPIO() { HAL_GPIO_Init(BTN_PORT, &GPIO_Config2); HAL_GPIO_Init(LoRa_CS_GPIO_Port, &GPIO_ConfigSPI); // HAL_GPIO_WritePin(LoRa_CS_GPIO_Port, LoRa_CS_Pin, GPIO_PIN_SET); - // HAL_GPIO_WritePin(LoRa_RESET_GPIO_Port, LoRa_RESET_Pin, GPIO_PIN_SET); + // HAL_GPIO_WritePin(LoRa_RESET_GPIO_Port, LoRa_RESET_Pin, + // GPIO_PIN_SET); } extern "C" {