Implemented Log app (#144)

This commit is contained in:
Ken Van Hoeylandt
2025-01-02 21:18:45 +01:00
committed by GitHub
parent 737c0f7447
commit a7d15056d8
6 changed files with 262 additions and 22 deletions
+79 -12
View File
@@ -1,9 +1,60 @@
#include "Mutex.h"
#include <cstring>
namespace tt {
static LogEntry* logEntries = nullptr;
static unsigned int nextLogEntryIndex;
static Mutex logMutex;
static void ensureLogEntriesExist() {
if (logEntries == nullptr) {
logEntries = new LogEntry[TT_LOG_ENTRY_COUNT];
assert(logEntries != nullptr);
nextLogEntryIndex = 0;
}
}
static void storeLog(LogLevel level, const char* format, va_list args) {
if (logMutex.lock(5 / portTICK_PERIOD_MS)) {
ensureLogEntriesExist();
logEntries[nextLogEntryIndex].level = level;
vsnprintf(logEntries[nextLogEntryIndex].message, TT_LOG_MESSAGE_SIZE, format, args);
nextLogEntryIndex++;
if (nextLogEntryIndex == TT_LOG_ENTRY_COUNT) {
nextLogEntryIndex = 0;
}
logMutex.unlock();
}
}
LogEntry* copyLogEntries(unsigned int& outIndex) {
if (logMutex.lock(5 / portTICK_PERIOD_MS)) {
auto* newEntries = new LogEntry[TT_LOG_ENTRY_COUNT];
assert(newEntries != nullptr);
for (int i = 0; i < TT_LOG_ENTRY_COUNT; ++i) {
memcpy(&newEntries[i], &logEntries[i], sizeof(LogEntry));
}
outIndex = nextLogEntryIndex;
logMutex.unlock();
return newEntries;
} else {
return nullptr;
}
}
} // namespace tt
#ifndef ESP_PLATFORM
#include "Log.h"
#include <cstdint>
#include <sys/time.h>
#include <sstream>
namespace tt {
@@ -17,7 +68,7 @@ static char toPrefix(LogLevel level) {
return 'I';
case LogLevelDebug:
return 'D';
case LogLevelTrace:
case LogLevelVerbose:
return 'T';
default:
return '?';
@@ -34,7 +85,7 @@ static const char* toColour(LogLevel level) {
return "\033[32m";
case LogLevelDebug:
return "\033[1;37m";
case LogLevelTrace:
case LogLevelVerbose:
return "\033[37m";
default:
return "";
@@ -66,22 +117,38 @@ static uint64_t getTimestamp() {
}
void log(LogLevel level, const char* tag, const char* format, ...) {
printf(
"%s%c (%lu) %s: ",
toColour(level),
toPrefix(level),
getTimestamp(),
tag
);
std::stringstream buffer;
buffer << toColour(level) << toPrefix(level) << " (" << getTimestamp() << ") " << tag << " " << format << "\033[0m\n";
va_list args;
va_start(args, format);
vprintf(format, args);
vprintf(buffer.str().c_str(), args);
va_end(args);
printf("\033[0m\n");
va_start(args, format);
tt::storeLog(level, buffer.str().c_str(), args);
va_end(args);
}
} // namespace
#endif
#else // ESP_PLATFORM
#include <esp_log.h>
extern "C" {
extern void __real_esp_log_write(esp_log_level_t level, const char* tag, const char* format, ...);
void __wrap_esp_log_write(esp_log_level_t level, const char* tag, const char* format, ...) {
va_list args;
va_start(args, format);
tt::storeLog((tt::LogLevel)level, format, args);
esp_log_writev(level, tag, format, args);
va_end(args);
}
}
#endif
+28 -8
View File
@@ -2,6 +2,34 @@
#include "LogMessages.h"
#if CONFIG_SPIRAM_USE_MALLOC == 1 or not defined(ESP_PLATFORM)
#define TT_LOG_ENTRY_COUNT 200
#define TT_LOG_MESSAGE_SIZE 128
#else
#define TT_LOG_ENTRY_COUNT 50
#define TT_LOG_MESSAGE_SIZE 50
#endif
namespace tt {
enum LogLevel {
LogLevelNone, /*!< No log output */
LogLevelError, /*!< Critical errors, software module can not recover on its own */
LogLevelWarning, /*!< Error conditions from which recovery measures have been taken */
LogLevelInfo, /*!< Information messages which describe normal flow of events */
LogLevelDebug, /*!< Extra information which is not necessary for normal use (values, pointers, sizes, etc). */
LogLevelVerbose /*!< Bigger chunks of debugging information, or frequent messages which can potentially flood the output. */
};
struct LogEntry {
LogLevel level = LogLevelNone;
char message[TT_LOG_MESSAGE_SIZE] = { 0 };
};
LogEntry* copyLogEntries(unsigned int& outIndex);
} // namespace tt
#ifdef ESP_TARGET
#include "esp_log.h"
#else
@@ -26,14 +54,6 @@
namespace tt {
typedef enum {
LogLevelError,
LogLevelWarning,
LogLevelInfo,
LogLevelDebug,
LogLevelTrace
} LogLevel;
void log(LogLevel level, const char* tag, const char* format, ...);
} // namespace