fome-fw/firmware/util/datalogging.h

74 lines
1.6 KiB
C
Raw Normal View History

2015-07-10 06:01:56 -07:00
/**
* @file datalogging.h
* @brief Buffered console output stream header
*
* @date Feb 25, 2013
2020-01-13 18:57:43 -08:00
* @author Andrey Belomutskiy, (c) 2012-2020
2015-07-10 06:01:56 -07:00
*/
2020-04-01 18:32:21 -07:00
#pragma once
2015-07-10 06:01:56 -07:00
#include <cstdarg>
#include <cstdint>
#include <cstddef>
2015-07-10 06:01:56 -07:00
class Logging {
public:
Logging() = delete;
2015-07-10 06:01:56 -07:00
Logging(const char *name, char *buffer, int bufferSize);
void reset();
2018-08-31 18:38:14 -07:00
void append(const char *text);
void appendFast(const char *text);
void appendPrintf(const char *fmt, ...);
void appendFloat(float value, int precision);
void terminate() {
2023-11-01 15:52:30 -07:00
m_linePointer[0] = '\0';
}
/**
* This macro breaks the normal zero=termination constraint, please take care of this outside of this function
*/
void appendChar(char c) {
2023-11-01 15:52:30 -07:00
*m_linePointer = c;
m_linePointer++;
}
size_t loggingSize() const {
2023-11-01 15:52:30 -07:00
return (uintptr_t)m_linePointer - (uintptr_t)m_buffer;
}
size_t remainingSize() const {
2023-11-01 15:52:30 -07:00
return m_bufferSize - loggingSize();
}
//private:
bool validateBuffer(uint32_t extraLen);
2023-11-01 15:52:30 -07:00
const char* const m_name;
2015-07-10 06:01:56 -07:00
/**
* Zero-terminated buffer of pending debug message
*
* Unless a larger external buffer is specified, this is just a pointer to DEFAULT_BUFFER
*/
2023-11-01 15:52:30 -07:00
char* const m_buffer;
const int m_bufferSize;
2015-07-10 06:01:56 -07:00
/**
* This pointer is always pointing at the position within the buffer into which next
* write operation would append additional data
*/
2023-11-01 15:52:30 -07:00
char* m_linePointer = nullptr;
2015-07-10 06:01:56 -07:00
};
class LoggingWithStorage : public Logging {
public:
explicit LoggingWithStorage(const char *name);
char DEFAULT_BUFFER[100];
2015-07-10 06:01:56 -07:00
};
void initLoggingExt(Logging *logging, const char *name, char *buffer, int bufferSize);