Worked on logging + some cleanup
This commit is contained in:
@@ -23,24 +23,6 @@ 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));
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -17,19 +17,19 @@
|
||||
Logger::getInstance()->log(__VA_ARGS__); \
|
||||
}
|
||||
|
||||
|
||||
|
||||
class Logger
|
||||
{
|
||||
public:
|
||||
using NtpTimeT = unsigned long;
|
||||
|
||||
static constexpr int HEADER_SIZE = sizeof(NtpTimeT) + sizeof(millis());
|
||||
using TimeT = unsigned long;
|
||||
|
||||
~Logger();
|
||||
|
||||
template <class... Args>
|
||||
inline bool log(const char *formatStr, Args &&...args)
|
||||
{
|
||||
const auto time = millis();
|
||||
const TimeT time = millis();
|
||||
|
||||
if (totalSize_ - currentSize_ <= sizeof(time))
|
||||
return false;
|
||||
@@ -40,7 +40,8 @@ public:
|
||||
const auto spaceLeft = totalSize_ - currentSize_;
|
||||
auto charsWritten = snprintf(&data_[currentSize_], spaceLeft, formatStr,
|
||||
std::forward<Args>(args)...);
|
||||
Serial.println(&data_[currentSize_]);
|
||||
|
||||
//Serial.println(&data_[currentSize_]);
|
||||
|
||||
if (charsWritten < spaceLeft)
|
||||
{
|
||||
@@ -53,14 +54,39 @@ public:
|
||||
|
||||
static Logger *getInstance();
|
||||
static void init();
|
||||
static void setNtpTime(NtpTimeT time);
|
||||
|
||||
|
||||
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<const TimeT*>(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_;
|
||||
|
||||
@@ -52,8 +52,8 @@ private:
|
||||
uint8_t tareAvgCount_;
|
||||
int valueRightShift_;
|
||||
|
||||
AutoStart<MeasurementT> autoStart_;
|
||||
AutoStop<MeasurementT> autoStop_;
|
||||
AutoStart<MeasurementType> autoStart_;
|
||||
AutoStop<MeasurementType> autoStop_;
|
||||
};
|
||||
|
||||
// ------------------------------------------------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user