#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 TimeT = unsigned long; ~Logger(); template inline bool log(const char *formatStr, Args &&...args) { const TimeT time = millis(); if (totalSize_ - currentSize_ <= sizeof(time)) return false; memcpy(&data_[currentSize_], &time, sizeof(time)); currentSize_ += sizeof(time); const auto spaceLeft = totalSize_ - currentSize_; auto charsWritten = snprintf(&data_[currentSize_], spaceLeft, formatStr, std::forward(args)...); //Serial.println(&data_[currentSize_]); if (charsWritten < spaceLeft) { currentSize_ += charsWritten; return true; } else return false; } static Logger *getInstance(); static void init(); const size_t totalSize() const { return totalSize_; } const size_t currentSize() const { return currentSize_; } const char * data() const { return data_; } // Iteration class iterator { public: using TimeT = unsigned long; iterator(const char * ptr) : current_position_(ptr) {} TimeT time_millis() const { return *reinterpret_cast(current_position_); } const char * message() const { return current_position_ + sizeof(TimeT); } void operator++(){ current_position_ += sizeof(TimeT) + strlen(message()) + 1; } private: const char * current_position_; }; iterator begin() const { return {data_}; } iterator end() const { return {data_ + currentSize_}; } private: Logger(); char *data_; size_t totalSize_; size_t currentSize_; };