Logging in memory and linuxplatform

This commit is contained in:
Thomas Kunze 2024-08-19 16:25:30 +02:00
parent 774a6b1d71
commit 546300d705
7 changed files with 133 additions and 91 deletions

View File

@ -66,7 +66,7 @@ void measureTemp()
currentValue -= 50; currentValue -= 50;
// currentValue *= (670433.28 + 273); // currentValue *= (670433.28 + 273);
// currentValue -= 273; // currentValue -= 273;
println(currentValue); LOGGER.info("current value: %f",currentValue);
GO_CURR.value(currentValue); GO_CURR.value(currentValue);
double max = GO_MAX.value(); double max = GO_MAX.value();
@ -97,6 +97,9 @@ void appLoop()
void setup() void setup()
{ {
srand((unsigned int)time(NULL)); srand((unsigned int)time(NULL));
Logger::logLevel("App", Logger::Info);
Logger::logLevel("ApplicationLayer", Logger::Info);
knx.readMemory(); knx.readMemory();
if (knx.individualAddress() == 0xFFFF) if (knx.individualAddress() == 0xFFFF)

View File

@ -9,7 +9,7 @@ const uint8_t* popByte(uint8_t& b, const uint8_t* data)
} }
std::string byte2hex(uint8_t byte) std::string byte2hex(const uint8_t byte)
{ {
const char* hex = "0123456789ABCDEF"; const char* hex = "0123456789ABCDEF";
char out[3] = {0}; char out[3] = {0};
@ -18,11 +18,21 @@ std::string byte2hex(uint8_t byte)
return std::string(out); return std::string(out);
} }
std::string word2hex(uint16_t value) std::string word2hex(const uint16_t value)
{ {
return byte2hex((uint8_t) (value & 0xFF00) >> 8) + byte2hex((uint8_t) (value & 0xFF)); return byte2hex((uint8_t) (value & 0xFF00) >> 8) + byte2hex((uint8_t) (value & 0xFF));
} }
std::string array2hex(const uint8_t* value, size_t length)
{
std::string result("");
for (size_t i = 0; i < length; i++)
result += byte2hex(value[i]) + " ";
return result;
}
#ifndef KNX_NO_PRINT #ifndef KNX_NO_PRINT
void printHex(const char* suffix, const uint8_t* data, size_t length, bool newline) void printHex(const char* suffix, const uint8_t* data, size_t length, bool newline)
{ {

View File

@ -70,8 +70,9 @@
void attachInterrupt(uint32_t pin, voidFuncPtr callback, uint32_t mode); void attachInterrupt(uint32_t pin, voidFuncPtr callback, uint32_t mode);
#endif #endif
std::string byte2hex(uint8_t byte); std::string byte2hex(const uint8_t byte);
std::string word2hex(uint16_t value); std::string word2hex(const uint16_t value);
std::string array2hex(const uint8_t* value, size_t length);
#ifndef KNX_NO_PRINT #ifndef KNX_NO_PRINT
void print(const char[]); void print(const char[]);

View File

@ -1,4 +1,7 @@
#include "memory.h" #include "memory.h"
#include "util/logger.h"
#define LOGGER Logger::logger("Memory")
#include <string.h> #include <string.h>
@ -13,18 +16,18 @@ Memory::~Memory()
void Memory::readMemory() void Memory::readMemory()
{ {
println("readMemory"); LOGGER.info("restoring persistant Memory");
uint8_t* flashStart = _platform.getNonVolatileMemoryStart(); uint8_t* flashStart = _platform.getNonVolatileMemoryStart();
size_t flashSize = _platform.getNonVolatileMemorySize(); size_t flashSize = _platform.getNonVolatileMemorySize();
if (flashStart == nullptr) if (flashStart == nullptr)
{ {
println("no user flash available;"); LOGGER.error("no user flash available;");
return; return;
} }
printHex("RESTORED ", flashStart, _metadataSize); LOGGER.info("content %S", array2hex(flashStart, _metadataSize));
uint16_t metadataBlockSize = alignToPageSize(_metadataSize); uint16_t metadataBlockSize = alignToPageSize(_metadataSize);
@ -66,64 +69,51 @@ void Memory::readMemory()
} }
else else
{ {
println("manufacturerId or hardwareType are different"); LOGGER.warning("manufacturerId or hardwareType are different");
print("expexted manufacturerId: "); LOGGER.warning("expexted manufacturerId: %S , stored manufacturerId: %S",
print(_deviceObject.manufacturerId(), HEX); word2hex(_deviceObject.manufacturerId()), manufacturerId);
print(", stored manufacturerId: "); LOGGER.warning("expexted hardwareType: %S, stored hardwareType: %S",
println(manufacturerId, HEX); array2hex(_deviceObject.hardwareType(), LEN_HARDWARE_TYPE),
print("expexted hardwareType: "); array2hex(hardwareType, LEN_HARDWARE_TYPE));
printHex("", _deviceObject.hardwareType(), LEN_HARDWARE_TYPE);
print(", stored hardwareType: ");
printHex("", hardwareType, LEN_HARDWARE_TYPE);
println("");
} }
} }
else else
{ {
println("DataObject api changed, any data stored in flash is invalid."); LOGGER.warning("DataObject api changed, any data stored in flash is invalid.");
print("expexted DataObject api version: "); LOGGER.warning("expexted DataObject api version: %S, stored api version: %S", word2hex(_deviceObject.apiVersion), word2hex(apiVersion));
print(_deviceObject.apiVersion, HEX);
print(", stored api version: ");
println(apiVersion, HEX);
} }
if (versionCheck == FlashAllInvalid) if (versionCheck == FlashAllInvalid)
{ {
println("ETS has to reprogram PA and application!"); LOGGER.warning("You need to reprogram PA and application with ETS!");
return; return;
} }
println("restoring data from flash..."); LOGGER.info("saverestores %d", _saveCount);
print("saverestores ");
println(_saveCount);
for (int i = 0; i < _saveCount; i++) for (int i = 0; i < _saveCount; i++)
{ {
println(flashStart - buffer); LOGGER.info("Offset %d", buffer - flashStart);
println(".");
buffer = _saveRestores[i]->restore(buffer); buffer = _saveRestores[i]->restore(buffer);
} }
println("restored saveRestores"); LOGGER.info("restored saveRestores");
if (versionCheck == FlashTablesInvalid) if (versionCheck == FlashTablesInvalid)
{ {
println("TableObjects are referring to an older firmware version and are not loaded"); LOGGER.warning("TableObjects are referring to an older firmware version and are not loaded");
return; return;
} }
print("tableObjs "); LOGGER.info("tableObjs %d", _tableObjCount);
println(_tableObjCount);
for (int i = 0; i < _tableObjCount; i++) for (int i = 0; i < _tableObjCount; i++)
{ {
println(flashStart - buffer); ptrdiff_t offset = (buffer - flashStart);
println(".");
buffer = _tableObjects[i]->restore(buffer); buffer = _tableObjects[i]->restore(buffer);
uint16_t memorySize = 0; uint16_t memorySize = 0;
buffer = popWord(memorySize, buffer); buffer = popWord(memorySize, buffer);
println(memorySize); LOGGER.info("Offset %d, Size %d", offset, memorySize);
if (memorySize == 0) if (memorySize == 0)
continue; continue;
@ -131,11 +121,12 @@ void Memory::readMemory()
addNewUsedBlock(_tableObjects[i]->_data, memorySize); addNewUsedBlock(_tableObjects[i]->_data, memorySize);
} }
println("restored Tableobjects"); LOGGER.info("restored Tableobjects");
} }
void Memory::writeMemory() void Memory::writeMemory()
{ {
LOGGER.info("writing persistang memory");
// first get the necessary size of the writeBuffer // first get the necessary size of the writeBuffer
uint16_t writeBufferSize = _metadataSize; uint16_t writeBufferSize = _metadataSize;
@ -156,8 +147,7 @@ void Memory::writeMemory()
flashPos = _platform.writeNonVolatileMemory(flashPos, buffer, bufferPos - buffer); flashPos = _platform.writeNonVolatileMemory(flashPos, buffer, bufferPos - buffer);
print("save saveRestores "); LOGGER.info("write saveRestores %d", _saveCount);
println(_saveCount);
for (int i = 0; i < _saveCount; i++) for (int i = 0; i < _saveCount; i++)
{ {
@ -165,8 +155,7 @@ void Memory::writeMemory()
flashPos = _platform.writeNonVolatileMemory(flashPos, buffer, bufferPos - buffer); flashPos = _platform.writeNonVolatileMemory(flashPos, buffer, bufferPos - buffer);
} }
print("save tableobjs "); LOGGER.info("save tableobjs %d", _tableObjCount);
println(_tableObjCount);
for (int i = 0; i < _tableObjCount; i++) for (int i = 0; i < _tableObjCount; i++)
{ {
@ -179,7 +168,7 @@ void Memory::writeMemory()
if (block == nullptr) if (block == nullptr)
{ {
println("_data of TableObject not in _usedList"); LOGGER.error("_data of TableObject not in _usedList");
_platform.fatalError(); _platform.fatalError();
} }
@ -244,7 +233,7 @@ uint8_t* Memory::allocMemory(size_t size)
if (!blockToUse) if (!blockToUse)
{ {
println("No available non volatile memory!"); LOGGER.error("No available non volatile memory!");
_platform.fatalError(); _platform.fatalError();
} }
@ -287,7 +276,7 @@ void Memory::freeMemory(uint8_t* ptr)
if (!found) if (!found)
{ {
println("freeMemory for not used pointer called"); LOGGER.error("freeMemory for not used pointer called");
_platform.fatalError(); _platform.fatalError();
} }
@ -328,7 +317,7 @@ MemoryBlock* Memory::removeFromList(MemoryBlock* head, MemoryBlock* item)
if (!head || !item) if (!head || !item)
{ {
println("invalid parameters of Memory::removeFromList"); LOGGER.critical("invalid parameters of Memory::removeFromList");
_platform.fatalError(); _platform.fatalError();
} }
@ -349,7 +338,7 @@ MemoryBlock* Memory::removeFromList(MemoryBlock* head, MemoryBlock* item)
if (!found) if (!found)
{ {
println("tried to remove block from list not in it"); LOGGER.critical("tried to remove block from list not in it");
_platform.fatalError(); _platform.fatalError();
} }
@ -475,13 +464,13 @@ void Memory::addNewUsedBlock(uint8_t* address, size_t size)
if (smallerFreeBlock == nullptr) if (smallerFreeBlock == nullptr)
{ {
println("addNewUsedBlock: no smallerBlock found"); LOGGER.critical("addNewUsedBlock: no smallerBlock found");
_platform.fatalError(); _platform.fatalError();
} }
if ((smallerFreeBlock->address + smallerFreeBlock->size) < (address + size)) if ((smallerFreeBlock->address + smallerFreeBlock->size) < (address + size))
{ {
println("addNewUsedBlock: found block can't contain new block"); LOGGER.critical("addNewUsedBlock: found block can't contain new block");
_platform.fatalError(); _platform.fatalError();
} }

View File

@ -37,6 +37,9 @@
#include "../ip/ip_parameter_object.h" #include "../ip/ip_parameter_object.h"
#include "../bits.h" #include "../bits.h"
#include "../ip/ip_host_protocol_address_information.h" #include "../ip/ip_host_protocol_address_information.h"
#include "../util/logger.h"
#define LOGGER Logger::logger("LinuxPlatform")
#define MAX_MEM 4096 #define MAX_MEM 4096
@ -46,7 +49,7 @@ LinuxPlatform::LinuxPlatform()
if (socketMac < 0) if (socketMac < 0)
{ {
printf("Lookup socket creation failed"); LOGGER.critical("Lookup socket creation failed");
return; return;
} }
@ -158,7 +161,7 @@ void LinuxPlatform::restart()
void LinuxPlatform::fatalError() void LinuxPlatform::fatalError()
{ {
printf("A fatal error occured. Stopping.\n"); LOGGER.critical("A fatal error occured. Stopping.\n");
while (true) while (true)
sleep(1); sleep(1);
@ -185,7 +188,7 @@ void LinuxPlatform::setupMultiCast(uint32_t addr, uint16_t port)
if (_multicastSocketFd == -1) if (_multicastSocketFd == -1)
{ {
perror("socket()"); LOGGER.critical("socket() %s", strerror(errno));
fatalError(); fatalError();
} }
@ -194,13 +197,13 @@ void LinuxPlatform::setupMultiCast(uint32_t addr, uint16_t port)
if (setsockopt(_multicastSocketFd, SOL_SOCKET, SO_REUSEADDR, &loop, sizeof(loop)) < 0) if (setsockopt(_multicastSocketFd, SOL_SOCKET, SO_REUSEADDR, &loop, sizeof(loop)) < 0)
{ {
perror("setsockopt:SO_REUSEADDR"); LOGGER.critical("setsockopt:SO_REUSEADDR %s", strerror(errno));
fatalError(); fatalError();
} }
if (bind(_multicastSocketFd, (struct sockaddr*)&sin, sizeof(sin)) < 0) if (bind(_multicastSocketFd, (struct sockaddr*)&sin, sizeof(sin)) < 0)
{ {
perror("bind"); LOGGER.critical("bind %s", strerror(errno));
fatalError(); fatalError();
} }
@ -209,7 +212,7 @@ void LinuxPlatform::setupMultiCast(uint32_t addr, uint16_t port)
if (setsockopt(_multicastSocketFd, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop)) < 0) if (setsockopt(_multicastSocketFd, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop)) < 0)
{ {
perror("setsockopt:IP_MULTICAST_LOOP"); LOGGER.critical("setsockopt:IP_MULTICAST_LOOP %s", strerror(errno));
fatalError(); fatalError();
} }
@ -219,7 +222,7 @@ void LinuxPlatform::setupMultiCast(uint32_t addr, uint16_t port)
if (setsockopt(_multicastSocketFd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &command, sizeof(command)) < 0) if (setsockopt(_multicastSocketFd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &command, sizeof(command)) < 0)
{ {
perror("setsockopt:IP_ADD_MEMBERSHIP"); LOGGER.critical("setsockopt:IP_ADD_MEMBERSHIP %s", strerror(errno));
fatalError(); fatalError();
} }
@ -239,7 +242,7 @@ void LinuxPlatform::closeMultiCast()
IP_DROP_MEMBERSHIP, IP_DROP_MEMBERSHIP,
&command, sizeof(command)) < 0) &command, sizeof(command)) < 0)
{ {
perror("setsockopt:IP_DROP_MEMBERSHIP"); LOGGER.error("setsockopt:IP_DROP_MEMBERSHIP %s", strerror(errno));
} }
close(_multicastSocketFd); close(_multicastSocketFd);
@ -306,7 +309,7 @@ void LinuxPlatform::doMemoryMapping()
if (_fd < 0) if (_fd < 0)
{ {
puts("Error in file opening"); LOGGER.critical("Error in file opening");
//exit(-1); //exit(-1);
} }
@ -316,7 +319,7 @@ void LinuxPlatform::doMemoryMapping()
if (ret < 0) if (ret < 0)
{ {
puts("Error in fstat"); LOGGER.critical("Error in fstat");
//exit(-1); //exit(-1);
} }
@ -326,7 +329,7 @@ void LinuxPlatform::doMemoryMapping()
{ {
if (ftruncate(_fd, FLASHSIZE) != 0) if (ftruncate(_fd, FLASHSIZE) != 0)
{ {
puts("Error extending file"); LOGGER.critical("Error extending file");
//exit(-1); //exit(-1);
} }
@ -344,7 +347,7 @@ void LinuxPlatform::doMemoryMapping()
if (addr == MAP_FAILED) if (addr == MAP_FAILED)
{ {
puts("Error in mmap"); LOGGER.critical("Error in mmap");
//exit(-1); //exit(-1);
} }
@ -354,7 +357,7 @@ void LinuxPlatform::doMemoryMapping()
void LinuxPlatform::closeSpi() void LinuxPlatform::closeSpi()
{ {
close(_spiFd); close(_spiFd);
printf("SPI device closed.\r\n"); LOGGER.info("SPI device closed.");
} }
int LinuxPlatform::readWriteSpi(uint8_t* data, size_t len) int LinuxPlatform::readWriteSpi(uint8_t* data, size_t len)
@ -384,7 +387,7 @@ void LinuxPlatform::setupSpi()
{ {
if ((_spiFd = open("/dev/spidev0.0", O_RDWR)) < 0) if ((_spiFd = open("/dev/spidev0.0", O_RDWR)) < 0)
{ {
printf("ERROR: SPI setup failed! Could not open SPI device!\r\n"); LOGGER.error("ERROR: SPI setup failed! Could not open SPI device!");
return; return;
} }
@ -395,26 +398,26 @@ void LinuxPlatform::setupSpi()
if (ioctl(_spiFd, SPI_IOC_WR_MODE, &mode) < 0) if (ioctl(_spiFd, SPI_IOC_WR_MODE, &mode) < 0)
{ {
printf("ERROR: SPI Mode Change failure: %s\n", strerror(errno)); LOGGER.error("ERROR: SPI Mode Change failure: %s", strerror(errno));
close(_spiFd); close(_spiFd);
return; return;
} }
if (ioctl(_spiFd, SPI_IOC_WR_BITS_PER_WORD, &spiBPW) < 0) if (ioctl(_spiFd, SPI_IOC_WR_BITS_PER_WORD, &spiBPW) < 0)
{ {
printf("ERROR: SPI BPW Change failure: %s\n", strerror(errno)); LOGGER.error("ERROR: SPI BPW Change failure: %s", strerror(errno));
close(_spiFd); close(_spiFd);
return; return;
} }
if (ioctl(_spiFd, SPI_IOC_WR_MAX_SPEED_HZ, &speed) < 0) if (ioctl(_spiFd, SPI_IOC_WR_MAX_SPEED_HZ, &speed) < 0)
{ {
printf("ERROR: SPI Speed Change failure: %s\n", strerror(errno)); LOGGER.error("ERROR: SPI Speed Change failure: %s", strerror(errno));
close(_spiFd); close(_spiFd);
return; return;
} }
printf("SPI device setup ok.\r\n"); LOGGER.info("SPI device setup ok.");
} }
void LinuxPlatform::flashFilePath(const std::string path) void LinuxPlatform::flashFilePath(const std::string path)
@ -856,13 +859,13 @@ int gpio_export(int pin)
int fd; /* Filedescriptor */ int fd; /* Filedescriptor */
int res; /* Result from write() */ int res; /* Result from write() */
fprintf(stderr, "Export GPIO pin %d\n", pin); LOGGER.info("Export GPIO pin %d", pin);
fd = open("/sys/class/gpio/export", O_WRONLY); fd = open("/sys/class/gpio/export", O_WRONLY);
if (fd < 0) if (fd < 0)
{ {
perror("Could not export GPIO pin(open)!\n"); LOGGER.error("Could not export GPIO pin(open)! %s", strerror(errno));
return (-1); return (-1);
} }
@ -871,7 +874,7 @@ int gpio_export(int pin)
if (res < 0) if (res < 0)
{ {
perror("Could not export GPIO pin(write)!\n"); LOGGER.error("Could not export GPIO pin(write)! %s", strerror(errno));
return (-1); return (-1);
} }
@ -892,7 +895,7 @@ int gpio_unexport(int pin)
int fd; /* Filedescriptor */ int fd; /* Filedescriptor */
int res; /* Result from write() */ int res; /* Result from write() */
fprintf(stderr, "Unexport GPIO pin %d\n", pin); LOGGER.info("Unexport GPIO pin %d", pin);
close(gpioFds[pin]); close(gpioFds[pin]);
@ -900,7 +903,7 @@ int gpio_unexport(int pin)
if (fd < 0) if (fd < 0)
{ {
perror("Could not unexport GPIO pin(open)!\n"); LOGGER.error("Could not unexport GPIO pin(open)! %s", strerror(errno));
return (-1); return (-1);
} }
@ -909,7 +912,7 @@ int gpio_unexport(int pin)
if (res < 0) if (res < 0)
{ {
perror("Could not unexport GPIO pin(write)!\n"); LOGGER.error("Could not unexport GPIO pin(write)! %s", strerror(errno));
return (-1); return (-1);
} }
@ -928,14 +931,14 @@ int gpio_direction(int pin, int dir)
int fd; /* Filedescriptor */ int fd; /* Filedescriptor */
int res; /* Result from write() */ int res; /* Result from write() */
fprintf(stderr, "Set GPIO direction for pin %d to %s\n", pin, (dir == INPUT) ? "INPUT" : "OUTPUT"); LOGGER.info("Set GPIO direction for pin %d to %s", pin, (dir == INPUT) ? "INPUT" : "OUTPUT");
snprintf(path, MAX_STRBUF_SIZE, "/sys/class/gpio/gpio%d/direction", pin); snprintf(path, MAX_STRBUF_SIZE, "/sys/class/gpio/gpio%d/direction", pin);
fd = open(path, O_WRONLY); fd = open(path, O_WRONLY);
if (fd < 0) if (fd < 0)
{ {
perror("Could not set mode for GPIO pin(open)!\n"); LOGGER.error("Could not set mode for GPIO pin(open)! %s", strerror(errno));
return (-1); return (-1);
} }
@ -956,7 +959,7 @@ int gpio_direction(int pin, int dir)
if (res < 0) if (res < 0)
{ {
perror("Could not set mode for GPIO pin(write)!\n"); LOGGER.error("Could not set mode for GPIO pin(write)! %s", strerror(errno));
return (-1); return (-1);
} }
@ -979,7 +982,7 @@ int gpio_read(int pin)
if (gpioFds[pin] < 0) if (gpioFds[pin] < 0)
{ {
perror("Could not read from GPIO(open)!\n"); LOGGER.error("Could not read from GPIO(open)! %s", strerror(errno));
return (-1); return (-1);
} }
@ -987,7 +990,7 @@ int gpio_read(int pin)
if (read(gpioFds[pin], &c, 1) < 0) if (read(gpioFds[pin], &c, 1) < 0)
{ {
perror("Could not read from GPIO(read)!\n"); LOGGER.error("Could not read from GPIO(read)! %s", strerror(errno));
return (-1); return (-1);
} }
@ -999,6 +1002,9 @@ int gpio_read(int pin)
*/ */
int gpio_write(int pin, int value) int gpio_write(int pin, int value)
{ {
if (pin < 0)
return -1;
char path[MAX_STRBUF_SIZE]; /* Buffer for path */ char path[MAX_STRBUF_SIZE]; /* Buffer for path */
int res; /* Result from write()*/ int res; /* Result from write()*/
@ -1009,7 +1015,7 @@ int gpio_write(int pin, int value)
if (gpioFds[pin] < 0) if (gpioFds[pin] < 0)
{ {
perror("Could not write to GPIO(open)!\n"); LOGGER.error("Could not write to GPIO(open)! %s", strerror(errno));
return (-1); return (-1);
} }
@ -1030,7 +1036,7 @@ int gpio_write(int pin, int value)
if (res < 0) if (res < 0)
{ {
perror("Could not write to GPIO(write)!\n"); LOGGER.error("Could not write to GPIO(write)! %s", strerror(errno));
return (-1); return (-1);
} }
@ -1053,7 +1059,7 @@ int gpio_edge(unsigned int pin, char edge)
if (fd < 0) if (fd < 0)
{ {
perror("Could not set GPIO edge detection(open)!\n"); LOGGER.error("Could not set GPIO edge detection(open)! %s", strerror(errno));
return (-1); return (-1);
} }
@ -1106,7 +1112,7 @@ int gpio_wait(unsigned int pin, int timeout)
if (fd < 0) if (fd < 0)
{ {
perror("Could not wait for GPIO edge(open)!\n"); LOGGER.error("Could not wait for GPIO edge(open)! %s", strerror(errno));
return (-1); return (-1);
} }
@ -1125,7 +1131,7 @@ int gpio_wait(unsigned int pin, int timeout)
if (rc < 0) if (rc < 0)
{ {
/* poll() failed! */ /* poll() failed! */
perror("Could not wait for GPIO edge(poll)!\n"); LOGGER.error("Could not wait for GPIO edge(poll)! %s", strerror(errno));
close(fd); close(fd);
return (-1); return (-1);
} }
@ -1142,7 +1148,7 @@ int gpio_wait(unsigned int pin, int timeout)
if (rc < 0) if (rc < 0)
{ {
/* read() failed! */ /* read() failed! */
perror("Could not wait for GPIO edge(read)!\n"); LOGGER.error("Could not wait for GPIO edge(read)! %s", strerror(errno));
close(fd); close(fd);
return (-2); return (-2);
} }

View File

@ -1,8 +1,17 @@
#include "logger.h" #include "logger.h"
Logger Logger::logger(const std::string name) Map<std::string, Logger::LogType, 64> Logger::_loggers;
Logger Logger::_logger;
Logger& Logger::logger(const std::string name)
{ {
return Logger(name); _logger.name(name);
return _logger;
}
void Logger::logLevel(const std::string name, LogType level)
{
_loggers.insertOrAssign(name, level);
} }
void Logger::info(const std::string message, ...) void Logger::info(const std::string message, ...)
@ -58,6 +67,20 @@ void Logger::exception(const std::string message, ...)
void Logger::log(LogType type, const char* format, va_list args) void Logger::log(LogType type, const char* format, va_list args)
{ {
#ifndef KNX_NO_PRINT #ifndef KNX_NO_PRINT
LogType* level = _loggers.get(_name);
if(level == nullptr) {
print("Logger ");
print(_name.c_str());
print(" is disabled. Use Logger::logLevel(\"");
print(_name.c_str());
println("\", Logger::Info) to enable.");
_loggers.insertOrAssign(_name, Disabled);
return;
}
if(*level > type)
return;
print(millis()); print(millis());
print(" "); print(" ");
print(_name.c_str()); print(_name.c_str());
@ -119,6 +142,9 @@ const std::string Logger::enum_name(LogType type)
case LogType::Exception: case LogType::Exception:
return "EXCE"; return "EXCE";
case LogType::Disabled:
return "DISA";
} }
return std::to_string(type); return std::to_string(type);

View File

@ -1,20 +1,27 @@
#include "../bits.h" #include "../bits.h"
#include <stdarg.h> #include <stdarg.h>
#include <string> #include <string>
#include "simple_map.h"
class NoOpLogger;
class Logger class Logger
{ {
public: public:
static Logger logger(const std::string name); enum LogType { Info, Warning, Error, Critical, Exception, Disabled};
static Logger& logger(const std::string name);
static void logLevel(const std::string name, LogType level);
void info(const std::string message, ...); void info(const std::string message, ...);
void warning(const std::string message, ...); void warning(const std::string message, ...);
void error(const std::string message, ...); void error(const std::string message, ...);
void critical(const std::string message, ...); void critical(const std::string message, ...);
void exception(const std::string message, ...); void exception(const std::string message, ...);
protected:
Logger() {}
virtual void log(LogType type, const char* format, va_list args);
void name(std::string value) { _name = value; }
private: private:
enum LogType { Info, Warning, Error, Critical, Exception};
const std::string enum_name(LogType type); const std::string enum_name(LogType type);
const std::string _name; std::string _name = "";
Logger(const std::string name) : _name(name) {} static Map<std::string, LogType, 64> _loggers;
inline void log(LogType type, const char* format, va_list args); static Logger _logger;
}; };