36#ifndef SBL_COMMON_LOG_TAGGED_HPP_
37#define SBL_COMMON_LOG_TAGGED_HPP_
58template<
size_t SIZE = 128>
60 static_assert(
SIZE >= 8,
"a tagged line needs room for a tag and its line ending");
70 if (!append(
"#%s",
tag)) {
94 const double rounded =
static_cast<double>(value) + (value < 0.0f ? -
half :
half);
99 char spec[] =
" %s=%.0f";
106 template<
typename T,
typename = std::enable_if_t<std::is_
integral_v<T>>>
108 if constexpr (std::is_signed_v<T>) {
109 add(
" %s=%d",
key,
static_cast<int>(value));
111 add(
" %s=%u",
key,
static_cast<unsigned>(value));
118 add(
" %s=%s",
key, value);
129 const char*
c_str()
const {
return buffer_; }
132 template<
typename Output>
134 buffer_[length_] =
'\r';
135 buffer_[length_ + 1] =
'\n';
136 buffer_[length_ + 2] =
'\0';
137 Output::write(buffer_);
138 buffer_[length_] =
'\0';
142 static constexpr size_t ENDING = 3;
143 static constexpr size_t CAPACITY =
SIZE - ENDING;
145 template<
typename...
Args>
147 const size_t before = length_;
152 buffer_[length_] =
'\0';
158 template<
typename...
Args>
160 const size_t room = CAPACITY - length_ + 1;
165 length_ +=
static_cast<size_t>(
wanted);
169 char buffer_[
SIZE] = {};
172 bool overflowed_ =
false;
181template<
size_t COUNT,
size_t LINE = 128>
183 static_assert(COUNT > 0,
"TaggedValues needs at least one value");
197 for (
size_t i = 0;
i < COUNT; ++
i) {
212 void set_interval_ms(
uint32_t ms) { interval_ms_ =
ms; }
217 void set(
size_t index,
float value) {
218 if (index >= COUNT) {
221 values_[index] = value;
222 const float moved = value - sent_[index];
223 if (
moved > threshold_ ||
moved < -threshold_ || value != value) {
228 float value(
size_t index)
const {
return index < COUNT ? values_[index] : 0.0f; }
242 template<
typename Output>
247 if (!forced_ && written_ &&
now_ms - last_ms_ < interval_ms_) {
251 for (
size_t i = 0;
i < COUNT; ++
i) {
252 if (names_[
i] ==
nullptr) {
255 line.field(names_[
i], values_[
i], decimals_);
256 if (
line.overflowed()) {
257 if (
line.fields() > 0) {
261 line.field(names_[
i], values_[
i], decimals_);
263 sent_[
i] = values_[
i];
265 if (
line.fields() > 0) {
277 const char* names_[COUNT] = {};
278 float values_[COUNT] = {};
279 float sent_[COUNT] = {};
285 bool forced_ =
false;
286 bool written_ =
false;
One #TAG key=value ... line in a fixed buffer.
void reset(const char *tag)
TaggedLine & field(const char *key, float value, uint8_t decimals=3)
A float field, rounded to decimals places.
TaggedLine & field(const char *key, T value)
TaggedLine & field(const char *key, const char *value)
const char * c_str() const
Named values written under one tag, on change, at a capped rate.
void set(size_t index, float value)
TaggedValues(const char *tag, const char *const (&names)[COUNT])
TaggedValues(const char *tag)
bool poll(uint32_t now_ms)
Write the values if one moved and the interval has passed.
void name(size_t index, const char *key)
static constexpr uint32_t DEFAULT_INTERVAL_MS
static constexpr float DEFAULT_THRESHOLD
constexpr float TAGGED_MAX_MAGNITUDE
constexpr uint8_t TAGGED_MAX_DECIMALS
int format(char *buf, size_t size, const char *fmt,...)
Format a string into a buffer (snprintf-style)