swimtracker-firmware/firmware/lib/logging/Logger.h

115 lines
2.6 KiB
C
Raw Normal View History

2021-08-10 22:47:47 +02:00
#pragma once
2023-08-28 15:06:29 +02:00
#include "TimeAbstraction.h"
#ifdef PLATFORM_ESP32
2021-08-10 22:47:47 +02:00
#include "Arduino.h"
2023-08-28 15:06:29 +02:00
#endif
#ifdef PLATFORM_NATIVE
#include <stdint.h>
#include <string.h>
#include <utility>
2023-09-07 15:27:29 +02:00
#include <stdio.h>
2023-08-28 15:06:29 +02:00
#endif
2021-08-10 22:47:47 +02:00
#define LOG_INFO(...) \
{ \
Logger::getInstance()->log(__VA_ARGS__); \
}
2023-09-14 16:16:17 +02:00
/*#define LOG_TRACE(...) \
2021-08-10 22:47:47 +02:00
{ \
Logger::getInstance()->log(__VA_ARGS__); \
}
2023-09-14 16:16:17 +02:00
*/
#define LOG_TRACE(...) {}
2021-08-10 22:47:47 +02:00
#define LOG_WARNING(...) \
{ \
Logger::getInstance()->log(__VA_ARGS__); \
}
2023-08-28 11:55:39 +02:00
2021-08-10 22:47:47 +02:00
class Logger
{
public:
2023-08-28 11:55:39 +02:00
using TimeT = unsigned long;
2023-07-16 13:20:19 +02:00
2021-08-10 22:47:47 +02:00
~Logger();
template <class... Args>
inline bool log(const char *formatStr, Args &&...args)
{
2023-08-28 11:55:39 +02:00
const TimeT time = millis();
2021-08-10 22:47:47 +02:00
if (totalSize_ - currentSize_ <= sizeof(time))
return false;
memcpy(&data_[currentSize_], &time, sizeof(time));
2023-07-06 09:26:39 +02:00
currentSize_ += sizeof(time);
2021-08-10 22:47:47 +02:00
const auto spaceLeft = totalSize_ - currentSize_;
2023-09-07 15:27:29 +02:00
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat-security"
2021-08-10 22:47:47 +02:00
auto charsWritten = snprintf(&data_[currentSize_], spaceLeft, formatStr,
std::forward<Args>(args)...);
2023-09-07 15:27:29 +02:00
#pragma GCC diagnostic pop
2023-09-14 16:16:17 +02:00
Serial.println(&data_[currentSize_]);
2023-09-07 15:27:29 +02:00
if(charsWritten > 0)
2021-08-10 22:47:47 +02:00
{
2023-09-07 15:27:29 +02:00
currentSize_ += charsWritten + 1; // + 1 for trailing zero
2021-08-10 22:47:47 +02:00
return true;
}
else
return false;
2023-09-08 10:26:49 +02:00
return true;
2021-08-10 22:47:47 +02:00
}
static Logger *getInstance();
static void init();
2023-07-16 13:20:19 +02:00
const size_t totalSize() const { return totalSize_; }
const size_t currentSize() const { return currentSize_; }
const char * data() const { return data_; }
2023-08-28 11:55:39 +02:00
// Iteration
class iterator
{
public:
using TimeT = unsigned long;
iterator(const char * ptr) : current_position_(ptr) {}
TimeT time_millis() const {
return *reinterpret_cast<const TimeT*>(current_position_);
}
const char * message() const {
return current_position_ + sizeof(TimeT);
}
void operator++(){
current_position_ += sizeof(TimeT) + strlen(message()) + 1;
}
2023-09-07 15:27:29 +02:00
bool operator==(const iterator & o) const { return current_position_ == o.current_position_; }
bool operator!=(const iterator & o) const { return current_position_ != o.current_position_; }
2023-08-28 11:55:39 +02:00
private:
const char * current_position_;
};
iterator begin() const { return {data_}; }
iterator end() const { return {data_ + currentSize_}; }
2021-08-10 22:47:47 +02:00
private:
2023-08-28 11:55:39 +02:00
2021-08-10 22:47:47 +02:00
Logger();
char *data_;
2023-09-08 10:26:49 +02:00
size_t totalSize_ = 0;
size_t currentSize_ = 0;
2021-08-10 22:47:47 +02:00
};