use buffering for all logs

pull/56/head
theFeiter 2 years ago
parent 876d860a05
commit 107c1413a1

@ -88,11 +88,16 @@ uint8_t LoRa2[] = {mac_prefix[3], mac_prefix[4], LORA2_PEER};
//uint8_t LoRaAddress[] = {0x42, 0x00}; //uint8_t LoRaAddress[] = {0x42, 0x00};
#endif #endif
#ifdef USE_SD_LOG #ifndef USE_WIFI
unsigned long last_millis = 0; unsigned long last_millis = 0;
unsigned long seconds_since_reset = 0; unsigned long seconds_since_reset = 0;
#endif #endif
#if defined (USE_SD_LOG) || defined (USE_FS_LOG)
char logBuffer[512];
uint16_t logBufferPos = 0; // datatype depends on size of sdBuffer
#endif
DataReading theData[256]; DataReading theData[256];
uint8_t ln; uint8_t ln;
uint8_t newData = event_clear; uint8_t newData = event_clear;
@ -191,36 +196,45 @@ void getSerial() {
} }
} }
void sendSD(const char filename[32]) { #if defined (USE_SD_LOG) || defined (USE_FS_LOG)
void releaseLogBuffer()
{
#ifdef USE_SD_LOG #ifdef USE_SD_LOG
DBG("Logging to SD card."); DBG("Releasing Log buffer to SD");
File logfile = SD.open(filename, FILE_WRITE); File logfile = SD.open(SD_FILENAME, FILE_WRITE);
for (int i = 0; i < ln; i++) { logfile.print(logBuffer);
char linebuf[32];
#ifdef USE_WIFI
sprintf(linebuf, "%ld,%d,%d,%g", timeClient.getEpochTime(), theData[i].id, theData[i].t, theData[i].d);
#else
sprintf(linebuf, "%ld,%d,%d,%g", seconds_since_reset, theData[i].id, theData[i].t, theData[i].d);
#endif
logfile.println(linebuf);
}
logfile.close(); logfile.close();
#endif #endif
}
void sendFS(const char filename[32]) {
#ifdef USE_FS_LOG #ifdef USE_FS_LOG
DBG("Logging to internal flash."); DBG("Releasing Log buffer to internal flash.");
File logfile = LittleFS.open(filename, "a"); File logfile = LittleFS.open(filename, "a");
for (int i = 0; i < ln; i++) { logfile.print(logBuffer);
char linebuf[32]; logfile.close();
#endif
memset(&(logBuffer[0]), 0, sizeof(logBuffer)/sizeof(char));
logBufferPos = 0;
}
#endif
void sendLog()
{
#if defined (USE_SD_LOG) || defined (USE_FS_LOG)
DBG("Logging to buffer");
for (int i = 0; i < ln; i++)
{
char linebuf[34]; // size depends on resulting length of the formatting string
#ifdef USE_WIFI #ifdef USE_WIFI
sprintf(linebuf, "%ld,%d,%d,%g", timeClient.getEpochTime(), theData[i].id, theData[i].t, theData[i].d); sprintf(linebuf, "%ld,%d,%d,%g\r\n", timeClient.getEpochTime(), theData[i].id, theData[i].t, theData[i].d);
#else #else
sprintf(linebuf, "%ld,%d,%d,%g", seconds_since_reset, theData[i].id, theData[i].t, theData[i].d); sprintf(linebuf, "%ld,%d,%d,%g\r\n", seconds_since_reset, theData[i].id, theData[i].t, theData[i].d);
#endif #endif
logfile.println(linebuf);
if (logBufferPos+strlen(linebuf) >= (sizeof(logBuffer)/sizeof(char))) // if buffer would overflow, release first
{
releaseLogBuffer();
}
memcpy(&logBuffer[logBufferPos], linebuf, strlen(linebuf)); //append line to buffer
logBufferPos+=strlen(linebuf);
} }
logfile.close();
#endif #endif
} }
void reconnect(short int attempts, bool silent) { void reconnect(short int attempts, bool silent) {
@ -283,8 +297,7 @@ void mqtt_publish(const char* payload) {
#ifdef USE_WIFI #ifdef USE_WIFI
if (!client.publish(TOPIC_DATA, payload)) { if (!client.publish(TOPIC_DATA, payload)) {
DBG(" Error on sending MQTT"); DBG(" Error on sending MQTT");
sendSD(SD_FILENAME); sendLog();
sendFS(FS_FILENAME);
} }
#endif #endif
} }

Loading…
Cancel
Save