[Serial log] Small optimization to reduce build size + process logs

This commit is contained in:
TD-er
2021-05-24 22:32:54 +02:00
parent dcaf9f3d3a
commit 9c363dcc05
3 changed files with 41 additions and 9 deletions
+4 -4
View File
@@ -183,14 +183,14 @@ void addToLog(byte logLevel, const char *line)
{
// Please note all functions called from here handling line must be PROGMEM aware.
if (loglevelActiveFor(LOG_TO_SERIAL, logLevel)) {
addToSerialBuffer(String(millis()).c_str());
addToSerialBuffer(String(F(" : ")).c_str());
addToSerialBuffer(String(millis()));
addToSerialBuffer(F(" : "));
String loglevelDisplayString = getLogLevelDisplayString(logLevel);
while (loglevelDisplayString.length() < 6) {
loglevelDisplayString += ' ';
}
addToSerialBuffer(loglevelDisplayString.c_str());
addToSerialBuffer(String(F(" : ")).c_str());
addToSerialBuffer(loglevelDisplayString);
addToSerialBuffer(F(" : "));
addToSerialBuffer(line);
addNewlineToSerialBuffer();
}
+33 -5
View File
@@ -77,8 +77,7 @@ void serial()
}
}
void addToSerialBuffer(const char *line) {
process_serialWriteBuffer(); // Try to make some room first.
int getRoomLeft() {
int roomLeft = getMaxFreeBlock();
if (roomLeft < 1000) {
@@ -88,12 +87,18 @@ void addToSerialBuffer(const char *line) {
} else {
roomLeft -= 4000; // leave some free for normal use.
}
return roomLeft;
}
void addToSerialBuffer(const char *line) {
process_serialWriteBuffer(); // Try to make some room first.
int roomLeft = getRoomLeft();
const char *c = line;
while (roomLeft > 0) {
// Must use PROGMEM aware functions here.
char ch = pgm_read_byte(c++);
const char ch = pgm_read_byte(c++);
if (ch == '\0') {
return;
@@ -104,6 +109,18 @@ void addToSerialBuffer(const char *line) {
}
}
void addToSerialBuffer(const String& line) {
process_serialWriteBuffer(); // Try to make some room first.
int roomLeft = getRoomLeft();
auto it = line.begin();
while (roomLeft > 0 && it != line.end()) {
serialWriteBuffer.push_back(*it);
--roomLeft;
++it;
}
}
void addNewlineToSerialBuffer() {
process_serialWriteBuffer(); // Try to make some room first.
serialWriteBuffer.push_back('\r');
@@ -132,13 +149,24 @@ void process_serialWriteBuffer() {
// For now, only send it to the serial buffer and try to process it.
// Later we may want to wrap it into a log.
void serialPrint(const __FlashStringHelper * text) {
addToSerialBuffer(text);
process_serialWriteBuffer();
}
void serialPrint(const String& text) {
addToSerialBuffer(text.c_str());
addToSerialBuffer(text);
process_serialWriteBuffer();
}
void serialPrintln(const __FlashStringHelper * text) {
addToSerialBuffer(text);
addNewlineToSerialBuffer();
process_serialWriteBuffer();
}
void serialPrintln(const String& text) {
addToSerialBuffer(text.c_str());
addToSerialBuffer(text);
addNewlineToSerialBuffer();
process_serialWriteBuffer();
}
+4
View File
@@ -15,14 +15,18 @@ void serial();
void addToSerialBuffer(const char *line);
void addToSerialBuffer(const String& line);
void addNewlineToSerialBuffer();
void process_serialWriteBuffer();
// For now, only send it to the serial buffer and try to process it.
// Later we may want to wrap it into a log.
void serialPrint(const __FlashStringHelper * text);
void serialPrint(const String& text);
void serialPrintln(const __FlashStringHelper * text);
void serialPrintln(const String& text);
void serialPrintln();