From 485d9730341972baf0c3e028ffe547ed5fb165ed Mon Sep 17 00:00:00 2001 From: Ing-Dom Date: Thu, 28 Dec 2023 17:28:32 +0100 Subject: [PATCH] wip of a new logging approach in the knx stack --- src/knx/logger.h | 69 +++++++++++++++++++++++++++++++++ src/knx/memory.cpp | 5 ++- src/rp2040_arduino_platform.cpp | 1 + 3 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 src/knx/logger.h diff --git a/src/knx/logger.h b/src/knx/logger.h new file mode 100644 index 0000000..81edd9c --- /dev/null +++ b/src/knx/logger.h @@ -0,0 +1,69 @@ +/* this is just a draft for the logging API, it is not complete and mssing all the backend + +ToDo: +- define all areas +- complete the logging functions for all levels +- define the backend logging class that can be inherited and overriden +- define an interface to redirect the logging stream + +Usage: +- define KNX_LOG_LVL and KNX_LOG_AREAS globally to your desired value +- KNX_LOG_TRACE("Unhandled service identifier: %02x", code); + + +*/ + + +constexpr auto KNX_LOG_LVL_ERROR = 1; +constexpr auto KNX_LOG_LVL_INFO = 2; +constexpr auto KNX_LOG_LVL_DEBUG = 3; +constexpr auto KNX_LOG_LVL_TRACE = 4; + + +constexpr auto KNX_LOG_LL = 0x0001; +constexpr auto KNX_LOG_NL = 0x0002; +constexpr auto KNX_LOG_TL = 0x0004; +constexpr auto KNX_LOG_AL = 0x0008; +constexpr auto KNX_LOG_TPUART = 0x0010; +constexpr auto KNX_LOG_IP = 0x0011; +constexpr auto KNX_LOG_MEM = 0x0012; + + +#ifndef KNX_LOG_AREAS + #define KNX_LOG_AREAS 0 +#endif + +#ifndef KNX_LOG_LVL + #define KNX_LOG_LVL 0 +#endif + +constexpr auto LOGLEVEL = KNX_LOG_LVL; +constexpr auto LOGAREAS = KNX_LOG_AREAS; + +template + __attribute__((always_inline)) constexpr void KNX_LOG_TRACE(Args&&... args) +{ + if constexpr((LOGLEVEL >= KNX_LOG_LVL_TRACE) && (x & LOGAREAS)) + Serial.printf(std::forward(args)...); +} + +template + __attribute__((always_inline)) constexpr void KNX_LOG_DEBUG(Args&&... args) +{ + if constexpr((LOGLEVEL >= KNX_LOG_LVL_DEBUG) && (x & LOGAREAS)) + Serial.printf(std::forward(args)...); +} + +template + __attribute__((always_inline)) constexpr void KNX_LOG_INFO(Args&&... args) +{ + if constexpr((LOGLEVEL >= KNX_LOG_LVL_INFO) && (x & LOGAREAS)) + Serial.printf(std::forward(args)...); +} + +template + __attribute__((always_inline)) constexpr void KNX_LOG_ERROR(Args&&... args) +{ + if constexpr((LOGLEVEL >= KNX_LOG_LVL_ERROR) && (x & LOGAREAS)) + Serial.printf(std::forward(args)...); +} \ No newline at end of file diff --git a/src/knx/memory.cpp b/src/knx/memory.cpp index c7bdd00..5631c2f 100644 --- a/src/knx/memory.cpp +++ b/src/knx/memory.cpp @@ -3,6 +3,7 @@ #include #include "bits.h" +#include "logger.h" Memory::Memory(Platform& platform, DeviceObject& deviceObject) : _platform(platform), _deviceObject(deviceObject) @@ -13,13 +14,13 @@ Memory::~Memory() void Memory::readMemory() { - println("readMemory"); + KNX_LOG_INFO("readMemory"); uint8_t* flashStart = _platform.getNonVolatileMemoryStart(); size_t flashSize = _platform.getNonVolatileMemorySize(); if (flashStart == nullptr) { - println("no user flash available;"); + KNX_LOG_ERROR("no user flash available;"); return; } diff --git a/src/rp2040_arduino_platform.cpp b/src/rp2040_arduino_platform.cpp index dd8dfb9..751901a 100644 --- a/src/rp2040_arduino_platform.cpp +++ b/src/rp2040_arduino_platform.cpp @@ -28,6 +28,7 @@ For usage of KNX-IP you have to define either #ifdef ARDUINO_ARCH_RP2040 #include "knx/bits.h" +#include "knx/logger.h" #include