New logger & removed old unused files

This commit is contained in:
Martin Bauer
2021-08-10 22:47:47 +02:00
parent 28c85f749c
commit ce12c846f6
18 changed files with 179 additions and 534 deletions

View File

@@ -0,0 +1,50 @@
#include "Logger.h"
constexpr size_t LOG_SIZE = 1024 * 1024 * 2;
static Logger *theLogger = nullptr;
Logger *Logger::getInstance()
{
return theLogger;
}
void Logger::init()
{
theLogger = new Logger();
Serial.begin(115200);
while (!Serial)
{
}
}
Logger::Logger()
{
data_ = (char *)heap_caps_malloc(LOG_SIZE, MALLOC_CAP_SPIRAM);
totalSize_ = LOG_SIZE;
currentSize_ = 0;
// write header placeholder
const auto millisPlaceholder = 0;
memcpy(&data_[currentSize_], &millisPlaceholder, sizeof(millisPlaceholder));
currentSize_ += sizeof(millisPlaceholder);
NtpTimeT ntpPlaceholder = 0;
memcpy(&data_[currentSize_], &ntpPlaceholder, sizeof(ntpPlaceholder));
currentSize_ += sizeof(ntpPlaceholder);
}
void Logger::setNtpTime(NtpTimeT ntpTime)
{
auto data = getInstance()->data_;
const auto millisTime = millis();
memcpy(&data[0], &millisTime, sizeof(millisTime));
memcpy(&data[sizeof(millisTime)], &ntpTime, sizeof(ntpTime));
}
Logger::~Logger()
{
free(data_);
}

View File

@@ -0,0 +1,62 @@
#pragma once
#include "Arduino.h"
#define LOG_INFO(...) \
{ \
Logger::getInstance()->log(__VA_ARGS__); \
}
#define LOG_TRACE(...) \
{ \
Logger::getInstance()->log(__VA_ARGS__); \
}
#define LOG_WARNING(...) \
{ \
Logger::getInstance()->log(__VA_ARGS__); \
}
class Logger
{
public:
using NtpTimeT = unsigned long;
~Logger();
template <class... Args>
inline bool log(const char *formatStr, Args &&...args)
{
const auto time = millis();
if (totalSize_ - currentSize_ <= sizeof(time))
return false;
memcpy(&data_[currentSize_], &time, sizeof(time));
currentSize_ += time;
const auto spaceLeft = totalSize_ - currentSize_;
auto charsWritten = snprintf(&data_[currentSize_], spaceLeft, formatStr,
std::forward<Args>(args)...);
Serial.println(&data_[currentSize_]);
if (charsWritten < spaceLeft)
{
currentSize_ += charsWritten;
return true;
}
else
return false;
}
static Logger *getInstance();
static void init();
static void setNtpTime(NtpTimeT time);
private:
Logger();
char *data_;
size_t totalSize_;
size_t currentSize_;
};