From b1b20c53b49c33b16d5255d5a99d00102c699a26 Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Wed, 23 Dec 2020 18:00:59 +0100 Subject: [PATCH] Prep removal of global log_data Prep removal of global log_data providing re-entry --- tasmota/support.ino | 60 +++++++++++------------------- tasmota/support_tasmota.ino | 8 +--- tasmota/xdrv_02_mqtt.ino | 13 ++++--- tasmota/xdrv_10_scripter.ino | 5 ++- tasmota/xdrv_12_home_assistant.ino | 5 ++- tasmota/xdrv_16_tuyamcu.ino | 9 +++-- tasmota/xdrv_30_exs_dimmer.ino | 29 ++++----------- tasmota/xdrv_45_shelly_dimmer.ino | 26 ++++--------- 8 files changed, 56 insertions(+), 99 deletions(-) diff --git a/tasmota/support.ino b/tasmota/support.ino index bb4ae2de7..30aee6b68 100644 --- a/tasmota/support.ino +++ b/tasmota/support.ino @@ -1987,7 +1987,7 @@ bool GetLog(uint32_t req_loglevel, uint32_t* index_p, char** entry_pp, size_t* l return false; } -void AddLog(uint32_t loglevel) { +void AddLogData(uint32_t loglevel, const char* log_data) { // char mxtime[10]; // "13:45:21 " // snprintf_P(mxtime, sizeof(mxtime), PSTR("%02d" D_HOUR_MINUTE_SEPARATOR "%02d" D_MINUTE_SECOND_SEPARATOR "%02d "), RtcTime.hour, RtcTime.minute, RtcTime.second); char mxtime[14]; // "13:45:21.999 " @@ -1995,7 +1995,7 @@ void AddLog(uint32_t loglevel) { if ((loglevel <= TasmotaGlobal.seriallog_level) && (TasmotaGlobal.masterlog_level <= TasmotaGlobal.seriallog_level)) { - Serial.printf("%s%s\r\n", mxtime, TasmotaGlobal.log_data); + Serial.printf("%s%s\r\n", mxtime, log_data); } uint32_t highest_loglevel = Settings.weblog_level; @@ -2012,7 +2012,7 @@ void AddLog(uint32_t loglevel) { TasmotaGlobal.log_buffer_pointer++; // Index 0 is not allowed as it is the end of char string } while (TasmotaGlobal.log_buffer_pointer == TasmotaGlobal.log_buffer[0] || // If log already holds the next index, remove it - strlen(TasmotaGlobal.log_buffer) + strlen(TasmotaGlobal.log_data) + strlen(mxtime) + 4 > LOG_BUFFER_SIZE) // 4 = log_buffer_pointer + '\1' + '\0' + strlen(TasmotaGlobal.log_buffer) + strlen(log_data) + strlen(mxtime) + 4 > LOG_BUFFER_SIZE) // 4 = log_buffer_pointer + '\1' + '\0' { char* it = TasmotaGlobal.log_buffer; it++; // Skip log_buffer_pointer @@ -2021,64 +2021,44 @@ void AddLog(uint32_t loglevel) { memmove(TasmotaGlobal.log_buffer, it, LOG_BUFFER_SIZE -(it-TasmotaGlobal.log_buffer)); // Move buffer forward to remove oldest log line } snprintf_P(TasmotaGlobal.log_buffer, sizeof(TasmotaGlobal.log_buffer), PSTR("%s%c%c%s%s\1"), - TasmotaGlobal.log_buffer, TasmotaGlobal.log_buffer_pointer++, '0'+loglevel, mxtime, TasmotaGlobal.log_data); + TasmotaGlobal.log_buffer, TasmotaGlobal.log_buffer_pointer++, '0'+loglevel, mxtime, log_data); TasmotaGlobal.log_buffer_pointer &= 0xFF; if (!TasmotaGlobal.log_buffer_pointer) { TasmotaGlobal.log_buffer_pointer++; // Index 0 is not allowed as it is the end of char string } } -// TasmotaGlobal.prepped_loglevel = 0; } -/* -void PrepLog_P(uint32_t loglevel, PGM_P formatP, ...) -{ - va_list arg; - va_start(arg, formatP); - vsnprintf_P(TasmotaGlobal.log_data, sizeof(TasmotaGlobal.log_data), formatP, arg); - va_end(arg); - TasmotaGlobal.prepped_loglevel = loglevel; +void AddLog(uint32_t loglevel) { + AddLogData(loglevel, TasmotaGlobal.log_data); } -*/ + void AddLog_P(uint32_t loglevel, PGM_P formatP, ...) { -/* - if (TasmotaGlobal.prepped_loglevel) { - AddLog(TasmotaGlobal.prepped_loglevel); - } -*/ + char log_data[LOGSZ]; + va_list arg; va_start(arg, formatP); - vsnprintf_P(TasmotaGlobal.log_data, sizeof(TasmotaGlobal.log_data), formatP, arg); + vsnprintf_P(log_data, sizeof(log_data), formatP, arg); va_end(arg); - AddLog(loglevel); + AddLogData(loglevel, log_data); } void AddLog_Debug(PGM_P formatP, ...) { + char log_data[LOGSZ]; + va_list arg; va_start(arg, formatP); - vsnprintf_P(TasmotaGlobal.log_data, sizeof(TasmotaGlobal.log_data), formatP, arg); + vsnprintf_P(log_data, sizeof(log_data), formatP, arg); va_end(arg); - AddLog(LOG_LEVEL_DEBUG); + AddLogData(LOG_LEVEL_DEBUG, log_data); } void AddLogBuffer(uint32_t loglevel, uint8_t *buffer, uint32_t count) { -/* - snprintf_P(TasmotaGlobal.log_data, sizeof(TasmotaGlobal.log_data), PSTR("DMP:")); - for (uint32_t i = 0; i < count; i++) { - snprintf_P(TasmotaGlobal.log_data, sizeof(TasmotaGlobal.log_data), PSTR("%s %02X"), TasmotaGlobal.log_data, *(buffer++)); - } - AddLog(loglevel); -*/ -/* - strcpy_P(TasmotaGlobal.log_data, PSTR("DMP: ")); - ToHex_P(buffer, count, TasmotaGlobal.log_data + strlen(TasmotaGlobal.log_data), sizeof(TasmotaGlobal.log_data) - strlen(TasmotaGlobal.log_data), ' '); - AddLog(loglevel); -*/ char hex_char[(count * 3) + 2]; AddLog_P(loglevel, PSTR("DMP: %s"), ToHex_P(buffer, count, hex_char, sizeof(hex_char), ' ')); } @@ -2094,16 +2074,18 @@ void AddLogMissed(const char *sensor, uint32_t misses) } void AddLogBufferSize(uint32_t loglevel, uint8_t *buffer, uint32_t count, uint32_t size) { - snprintf_P(TasmotaGlobal.log_data, sizeof(TasmotaGlobal.log_data), PSTR("DMP:")); + char log_data[LOGSZ]; + + snprintf_P(log_data, sizeof(log_data), PSTR("DMP:")); for (uint32_t i = 0; i < count; i++) { if (1 == size) { // uint8_t - snprintf_P(TasmotaGlobal.log_data, sizeof(TasmotaGlobal.log_data), PSTR("%s %02X"), TasmotaGlobal.log_data, *(buffer)); + snprintf_P(log_data, sizeof(log_data), PSTR("%s %02X"), log_data, *(buffer)); } else { // uint16_t - snprintf_P(TasmotaGlobal.log_data, sizeof(TasmotaGlobal.log_data), PSTR("%s %02X%02X"), TasmotaGlobal.log_data, *(buffer +1), *(buffer)); + snprintf_P(log_data, sizeof(log_data), PSTR("%s %02X%02X"), log_data, *(buffer +1), *(buffer)); } buffer += size; } - AddLog(loglevel); + AddLogData(loglevel, log_data); } /*********************************************************************************************\ diff --git a/tasmota/support_tasmota.ino b/tasmota/support_tasmota.ino index 98da69ee2..bd92688f6 100644 --- a/tasmota/support_tasmota.ino +++ b/tasmota/support_tasmota.ino @@ -897,11 +897,6 @@ void Every100mSeconds(void) // As the max amount of sleep = 250 mSec this loop will shift in time... power_t power_now; -/* - if (TasmotaGlobal.prepped_loglevel) { - AddLog(TasmotaGlobal.prepped_loglevel); - } -*/ if (TasmotaGlobal.latching_relay_pulse) { TasmotaGlobal.latching_relay_pulse--; if (!TasmotaGlobal.latching_relay_pulse) SetLatchingRelay(0, 0); @@ -1019,7 +1014,8 @@ void Every250mSeconds(void) ota_result = 0; ota_retry_counter--; if (ota_retry_counter) { - strlcpy(TasmotaGlobal.mqtt_data, GetOtaUrl(TasmotaGlobal.log_data, sizeof(TasmotaGlobal.log_data)), sizeof(TasmotaGlobal.mqtt_data)); + char ota_url[TOPSZ]; + strlcpy(TasmotaGlobal.mqtt_data, GetOtaUrl(ota_url, sizeof(ota_url)), sizeof(TasmotaGlobal.mqtt_data)); #ifndef FIRMWARE_MINIMAL if (RtcSettings.ota_loader) { // OTA File too large so try OTA minimal version diff --git a/tasmota/xdrv_02_mqtt.ino b/tasmota/xdrv_02_mqtt.ino index 7e46058ca..5baba474f 100644 --- a/tasmota/xdrv_02_mqtt.ino +++ b/tasmota/xdrv_02_mqtt.ino @@ -331,13 +331,14 @@ void MqttPublish(const char* topic, bool retained) } } - snprintf_P(TasmotaGlobal.log_data, sizeof(TasmotaGlobal.log_data), PSTR("%s%s = %s"), slog_type, (Settings.flag.mqtt_enabled) ? topic : strrchr(topic,'/')+1, TasmotaGlobal.mqtt_data); // SetOption3 - Enable MQTT - if (strlen(TasmotaGlobal.log_data) >= (sizeof(TasmotaGlobal.log_data) - strlen(sretained) -1)) { - TasmotaGlobal.log_data[sizeof(TasmotaGlobal.log_data) - strlen(sretained) -5] = '\0'; - snprintf_P(TasmotaGlobal.log_data, sizeof(TasmotaGlobal.log_data), PSTR("%s ..."), TasmotaGlobal.log_data); + char log_data[LOGSZ]; + snprintf_P(log_data, sizeof(log_data), PSTR("%s%s = %s"), slog_type, (Settings.flag.mqtt_enabled) ? topic : strrchr(topic,'/')+1, TasmotaGlobal.mqtt_data); // SetOption3 - Enable MQTT + if (strlen(log_data) >= (sizeof(log_data) - strlen(sretained) -1)) { + log_data[sizeof(log_data) - strlen(sretained) -5] = '\0'; + snprintf_P(log_data, sizeof(log_data), PSTR("%s ..."), log_data); } - snprintf_P(TasmotaGlobal.log_data, sizeof(TasmotaGlobal.log_data), PSTR("%s%s"), TasmotaGlobal.log_data, sretained); - AddLog(LOG_LEVEL_INFO); + snprintf_P(log_data, sizeof(log_data), PSTR("%s%s"), log_data, sretained); + AddLogData(LOG_LEVEL_INFO, log_data); if (Settings.ledstate &0x04) { TasmotaGlobal.blinks++; diff --git a/tasmota/xdrv_10_scripter.ino b/tasmota/xdrv_10_scripter.ino index d91fb5cce..c43f47826 100755 --- a/tasmota/xdrv_10_scripter.ino +++ b/tasmota/xdrv_10_scripter.ino @@ -3622,7 +3622,8 @@ void toLogN(const char *cp, uint8_t len) { void toLogEOL(const char *s1,const char *str) { if (!str) return; uint8_t index = 0; - char *cp = TasmotaGlobal.log_data; + char log_data[LOGSZ]; + char *cp = log_data; strcpy(cp, s1); cp += strlen(s1); while (*str) { @@ -3630,7 +3631,7 @@ void toLogEOL(const char *s1,const char *str) { *cp++ = *str++; } *cp = 0; - AddLog(LOG_LEVEL_INFO); + AddLogData(LOG_LEVEL_INFO, log_data); } diff --git a/tasmota/xdrv_12_home_assistant.ino b/tasmota/xdrv_12_home_assistant.ino index 5636eeceb..cd92e2e32 100644 --- a/tasmota/xdrv_12_home_assistant.ino +++ b/tasmota/xdrv_12_home_assistant.ino @@ -370,8 +370,9 @@ void TryResponseAppend_P(const char *format, ...) { AddLog_P(LOG_LEVEL_ERROR, PSTR("%s (%u/%u):"), kHAssError1, dlen, slen); va_start(args, format); - vsnprintf_P(TasmotaGlobal.log_data, sizeof(TasmotaGlobal.log_data), format, args); - AddLog(LOG_LEVEL_ERROR); + char log_data[LOGSZ]; + vsnprintf_P(log_data, sizeof(log_data), format, args); + AddLogData(LOG_LEVEL_ERROR, log_data); } else { diff --git a/tasmota/xdrv_16_tuyamcu.ino b/tasmota/xdrv_16_tuyamcu.ino index 033fd3380..13a7864c5 100644 --- a/tasmota/xdrv_16_tuyamcu.ino +++ b/tasmota/xdrv_16_tuyamcu.ino @@ -404,16 +404,17 @@ void TuyaSendCmd(uint8_t cmd, uint8_t payload[] = nullptr, uint16_t payload_len TuyaSerial->write(cmd); // Tuya command TuyaSerial->write(payload_len >> 8); // following data length (Hi) TuyaSerial->write(payload_len & 0xFF); // following data length (Lo) - snprintf_P(TasmotaGlobal.log_data, sizeof(TasmotaGlobal.log_data), PSTR("TYA: Send \"55aa00%02x%02x%02x"), cmd, payload_len >> 8, payload_len & 0xFF); + char log_data[LOGSZ]; + snprintf_P(log_data, sizeof(log_data), PSTR("TYA: Send \"55aa00%02x%02x%02x"), cmd, payload_len >> 8, payload_len & 0xFF); for (uint32_t i = 0; i < payload_len; ++i) { TuyaSerial->write(payload[i]); checksum += payload[i]; - snprintf_P(TasmotaGlobal.log_data, sizeof(TasmotaGlobal.log_data), PSTR("%s%02x"), TasmotaGlobal.log_data, payload[i]); + snprintf_P(log_data, sizeof(log_data), PSTR("%s%02x"), log_data, payload[i]); } TuyaSerial->write(checksum); TuyaSerial->flush(); - snprintf_P(TasmotaGlobal.log_data, sizeof(TasmotaGlobal.log_data), PSTR("%s%02x\""), TasmotaGlobal.log_data, checksum); - AddLog(LOG_LEVEL_DEBUG); + snprintf_P(log_data, sizeof(log_data), PSTR("%s%02x\""), log_data, checksum); + AddLogData(LOG_LEVEL_DEBUG, log_data); } void TuyaSendState(uint8_t id, uint8_t type, uint8_t* value) diff --git a/tasmota/xdrv_30_exs_dimmer.ino b/tasmota/xdrv_30_exs_dimmer.ino index df32b6109..09a469fc1 100644 --- a/tasmota/xdrv_30_exs_dimmer.ino +++ b/tasmota/xdrv_30_exs_dimmer.ino @@ -24,7 +24,7 @@ * https://ex-store.de/2-Kanal-RS232-WiFi-WLan-Dimmer-Modul-V4-fuer-Unterputzmontage-230V-3A * https://ex-store.de/2-Kanal-RS232-WiFi-WLan-Dimmer-Modul-V4-fuer-Unterputzmontage-230V-3A-ESP8266-V12-Stift-und-Buchsenleisten \*********************************************************************************************/ -//#define EXS_DEBUG +#define EXS_DEBUG #define XDRV_30 30 @@ -124,13 +124,8 @@ void ExsSerialSend(const uint8_t data[] = nullptr, uint16_t len = 0) char rc; #ifdef EXS_DEBUG - snprintf_P(TasmotaGlobal.log_data, sizeof(TasmotaGlobal.log_data), PSTR("EXS: Tx Packet: \"")); - for (uint32_t i = 0; i < len; i++) - { - snprintf_P(TasmotaGlobal.log_data, sizeof(TasmotaGlobal.log_data), PSTR("%s%02x"), TasmotaGlobal.log_data, data[i]); - } - snprintf_P(TasmotaGlobal.log_data, sizeof(TasmotaGlobal.log_data), PSTR("%s\""), TasmotaGlobal.log_data); - AddLog(LOG_LEVEL_DEBUG_MORE); + AddLog_P(LOG_LEVEL_DEBUG_MORE, PSTR("EXS: Tx Packet:")); + AddLogBuffer(LOG_LEVEL_DEBUG_MORE, (uint8_t *)data, len); #endif while (retries) @@ -368,13 +363,8 @@ bool ExsModuleSelected(void) bool ExsSetChannels(void) { #ifdef EXS_DEBUG - snprintf_P(TasmotaGlobal.log_data, sizeof(TasmotaGlobal.log_data), PSTR("EXS: SetChannels: \"")); - for (int i = 0; i < XdrvMailbox.data_len; i++) - { - snprintf_P(TasmotaGlobal.log_data, sizeof(TasmotaGlobal.log_data), PSTR("%s%02x"), TasmotaGlobal.log_data, ((uint8_t *)XdrvMailbox.data)[i]); - } - snprintf_P(TasmotaGlobal.log_data, sizeof(TasmotaGlobal.log_data), PSTR("%s\""), TasmotaGlobal.log_data); - AddLog(LOG_LEVEL_DEBUG_MORE); + AddLog_P(LOG_LEVEL_DEBUG_MORE, PSTR("EXS: SetChannels:")); + AddLogBuffer(LOG_LEVEL_DEBUG_MORE, (uint8_t *)XdrvMailbox.data, XdrvMailbox.data_len); #endif Exs.dimm[0] = ((uint8_t *)XdrvMailbox.data)[0]; @@ -466,13 +456,8 @@ void ExsSerialInput(void) Exs.cmd_status = 0; #ifdef EXS_DEBUG - snprintf_P(TasmotaGlobal.log_data, sizeof(TasmotaGlobal.log_data), PSTR("EXS: RX Packet: \"")); - for (uint32_t i = 0; i < Exs.byte_counter; i++) - { - snprintf_P(TasmotaGlobal.log_data, sizeof(TasmotaGlobal.log_data), PSTR("%s%02x"), TasmotaGlobal.log_data, Exs.buffer[i]); - } - snprintf_P(TasmotaGlobal.log_data, sizeof(TasmotaGlobal.log_data), PSTR("%s\", CRC: 0x%02x"), TasmotaGlobal.log_data, crc); - AddLog(LOG_LEVEL_DEBUG_MORE); + AddLog_P(LOG_LEVEL_DEBUG_MORE, PSTR("EXS: CRC: 0x%02x, RX Packet:"), crc); + AddLogBuffer(LOG_LEVEL_DEBUG_MORE, (uint8_t *)Exs.buffer, Exs.byte_counter); #endif if (Exs.buffer[0] == crc) diff --git a/tasmota/xdrv_45_shelly_dimmer.ino b/tasmota/xdrv_45_shelly_dimmer.ino index ab5743bfa..b4785291e 100644 --- a/tasmota/xdrv_45_shelly_dimmer.ino +++ b/tasmota/xdrv_45_shelly_dimmer.ino @@ -267,10 +267,8 @@ bool ShdSerialSend(const uint8_t data[] = nullptr, uint16_t len = 0) int retries = 3; #ifdef SHELLY_DIMMER_DEBUG - snprintf_P(TasmotaGlobal.log_data, sizeof(TasmotaGlobal.log_data), PSTR(SHD_LOGNAME "Tx Packet:")); - for (uint32_t i = 0; i < len; i++) - snprintf_P(TasmotaGlobal.log_data, sizeof(TasmotaGlobal.log_data), PSTR("%s %02x"), TasmotaGlobal.log_data, data[i]); - AddLog(LOG_LEVEL_DEBUG_MORE); + AddLog_P(LOG_LEVEL_DEBUG_MORE, PSTR(SHD_LOGNAME "Tx Packet:")); + AddLogBuffer(LOG_LEVEL_DEBUG_MORE, (uint8_t*)data, len); #endif // SHELLY_DIMMER_DEBUG while (retries--) @@ -696,10 +694,8 @@ bool ShdSerialInput(void) // finished #ifdef SHELLY_DIMMER_DEBUG Shd.byte_counter++; - snprintf_P(TasmotaGlobal.log_data, sizeof(TasmotaGlobal.log_data), PSTR(SHD_LOGNAME "RX Packet:")); - for (uint32_t i = 0; i < Shd.byte_counter; i++) - snprintf_P(TasmotaGlobal.log_data, sizeof(TasmotaGlobal.log_data), PSTR("%s %02x"), TasmotaGlobal.log_data, Shd.buffer[i]); - AddLog(LOG_LEVEL_DEBUG_MORE); + AddLog_P(LOG_LEVEL_DEBUG_MORE, PSTR(SHD_LOGNAME "Rx Packet:")); + AddLogBuffer(LOG_LEVEL_DEBUG_MORE, Shd.buffer, Shd.byte_counter); #endif // SHELLY_DIMMER_DEBUG Shd.byte_counter = 0; @@ -711,12 +707,9 @@ bool ShdSerialInput(void) { // wrong data #ifdef SHELLY_DIMMER_DEBUG - AddLog_P(LOG_LEVEL_DEBUG, PSTR(SHD_LOGNAME "Byte %i of received data frame is invalid"), Shd.byte_counter); + AddLog_P(LOG_LEVEL_DEBUG, PSTR(SHD_LOGNAME "Byte %i of received data frame is invalid. Rx Packet:"), Shd.byte_counter); Shd.byte_counter++; - snprintf_P(TasmotaGlobal.log_data, sizeof(TasmotaGlobal.log_data), PSTR(SHD_LOGNAME "RX Packet:")); - for (uint32_t i = 0; i < Shd.byte_counter; i++) - snprintf_P(TasmotaGlobal.log_data, sizeof(TasmotaGlobal.log_data), PSTR("%s %02x"), TasmotaGlobal.log_data, Shd.buffer[i]); - AddLog(LOG_LEVEL_DEBUG_MORE); + AddLogBuffer(LOG_LEVEL_DEBUG_MORE, Shd.buffer, Shd.byte_counter); #endif // SHELLY_DIMMER_DEBUG Shd.byte_counter = 0; } @@ -746,11 +739,8 @@ bool ShdModuleSelected(void) { bool ShdSetChannels(void) { #ifdef SHELLY_DIMMER_DEBUG - snprintf_P(TasmotaGlobal.log_data, sizeof(TasmotaGlobal.log_data), PSTR(SHD_LOGNAME "SetChannels: \"")); - for (int i = 0; i < XdrvMailbox.data_len; i++) - snprintf_P(TasmotaGlobal.log_data, sizeof(TasmotaGlobal.log_data), PSTR("%s%02x"), TasmotaGlobal.log_data, ((uint8_t *)XdrvMailbox.data)[i]); - snprintf_P(TasmotaGlobal.log_data, sizeof(TasmotaGlobal.log_data), PSTR("%s\""), TasmotaGlobal.log_data); - AddLog(LOG_LEVEL_DEBUG_MORE); + AddLog_P(LOG_LEVEL_DEBUG_MORE, PSTR(SHD_LOGNAME "SetChannels:")); + AddLogBuffer(LOG_LEVEL_DEBUG_MORE, (uint8_t *)XdrvMailbox.data, XdrvMailbox.data_len); #endif // SHELLY_DIMMER_DEBUG uint16_t brightness = ((uint32_t *)XdrvMailbox.data)[0];