updated the logger for static use without a static interface

added the RGB sensor isl29125
added WIFI
This commit is contained in:
Matthias Mitscherlich
2024-03-14 16:42:57 +01:00
parent 58353a439c
commit 62c088256f
9 changed files with 473 additions and 72 deletions
+16 -43
View File
@@ -49,8 +49,12 @@
// Function definitions
// --------------------------------------------------------------------------------------------------------------------
std::list<struct logger::LogQueueItem> logger::queue;
uint32_t logger::queuesize = 16;
uint32_t logger::numberOfLostMessages = 0;
bool logger::overflowRecovery = false;
logger::logger(uint32_t queuesize, ISerialBus<uint8_t>& serialPort) : port {serialPort}, queuesize {queuesize}
logger::logger(uint32_t queuesize, ISerialBus<uint8_t>& serialPort) : port {serialPort}
{
numberOfLostMessages = 0;
// Compose the debug type level list
@@ -63,14 +67,13 @@ FunctionStatus logger::log(const char* fileName, const char* functionName, int l
FunctionStatus returnValue = FUNCTION_STATUS_OK;
#if defined(ENABLE_SERIAL_LOGGING)
bool overflowRecovery = false;
int nrOfMessages;
struct LogQueueItem logQueueItem;
va_list ap;
nrOfMessages = queue.size();
if((nrOfMessages == queuesize - 1) && !overflowRecovery)
if((nrOfMessages >= queuesize - 1) && !overflowRecovery)
{
// Queue almost full, only one entry left. Log a warning instead
composeLogQueueItem(&logQueueItem, __FILE__, __func__, __LINE__, LOGTYPE_WARNING, "Log queue overflow");
@@ -82,8 +85,7 @@ FunctionStatus logger::log(const char* fileName, const char* functionName, int l
else if((nrOfMessages == 0) && overflowRecovery)
{
// Queue empty again after an overflow
char str[128];
snprintf(str, sizeof(str) / sizeof(str[0]), "%d messages lost", (unsigned int)numberOfLostMessages);
std::string str = std::to_string(numberOfLostMessages) + " messages were lost in the logger due to QUEUE overflow";
composeLogQueueItem(&logQueueItem, __FILE__, __func__, __LINE__, LOGTYPE_WARNING, str);
queue.push_back(logQueueItem);
@@ -130,46 +132,16 @@ void logger::composeTypeParameterList(void)
#if defined(ENABLE_SERIAL_LOGGING)
void logger::composeLogQueueItem(struct LogQueueItem* logQueueItem, std::string fileName, std::string functionName,
int lineNumber, LogType logType, std::string context)
void logger::composeLogQueueItem(struct LogQueueItem* logQueueItem, const std::string& fileName, const std::string& functionName,
int lineNumber, LogType logType, const std::string& context)
{
logQueueItem->logType = logType;
logQueueItem->context = context;
// strncpy((char*)&(logQueueItem->context[0]), context, contextSize);
// logQueueItem->context[contextSize - 1] = '\0';
if(logType != LOGTYPE_PRINT)
{
int fileNameIndex = 0;
// If filename starts with "src/", strip this part
if((fileName[0] == 's') &&
(fileName[1] == 'r') &&
(fileName[2] == 'c') &&
(fileName[3] == '/'))
{
fileNameIndex = 4;
}
// It is known that the strncpy use can potentially truncate the source string, meaning
// that the target string size is smaller then the original string
// This is not a problem in this particular case, so the compiler warning is disabled
// for this situation
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wstringop-truncation"
// All logtypes except LOGTYPE_PRINT need filename, functionname and linenumber.
logQueueItem->fileName = fileName;
logQueueItem->functionName = functionName;
// strncpy((char*)&(logQueueItem->fileName[0]), &fileName[fileNameIndex], fileNameSize);
// strncpy((char*)&(logQueueItem->functionName[0]), functionName, functionNameSize);
#pragma GCC diagnostic pop
logQueueItem->lineNumber = lineNumber;
// Fix terminating null byte in strncpy in case string to be copied is too long
// logQueueItem->fileName[fileNameSize - 1] = '\0';
// logQueueItem->functionName[functionNameSize - 1] = '\0';
}
logQueueItem->fileName = fileName;
logQueueItem->functionName = functionName;
logQueueItem->lineNumber = lineNumber;
}
#endif
@@ -186,7 +158,7 @@ void logger::task()
// Get the first log item from queue
logQueueItem = queue.front();
// Remove the item from the queue
this->queue.pop_front();
queue.pop_front();
if(logQueueItem.logType == LOGTYPE_PRINT)
@@ -194,7 +166,7 @@ void logger::task()
// Raw print
#if defined(ENABLE_SERIAL_LOGGING)
port.write(NO_DEVICE_ADDRESS, NO_REGISTER_ADDRESS, (uint8_t*)logQueueItem.context.c_str(), logQueueItem.context.length() + 1);
port.write(NO_DEVICE_ADDRESS, NO_REGISTER_ADDRESS, (uint8_t*)logQueueItem.context.c_str(), logQueueItem.context.length());
#endif
}
@@ -208,7 +180,8 @@ void logger::task()
// Find the correct Log level type
auto it = std::find_if(typeParameterList.begin(), typeParameterList.end(), [&logQueueItem](const struct typeParameters& obj) {return obj.logType == logQueueItem.logType;});
std::string outputString = it->vt100Prefix + " "
std::string outputString = "\n\r"
+ it->vt100Prefix + " "
+ it->vt100Prefix + " "
+ std::to_string(seconds) + " "
+ logQueueItem.fileName + " "