From 244611f12f6e9cbfbcf8ff15996bdedfd8829bc9 Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Tue, 22 Dec 2020 15:26:07 +0100 Subject: [PATCH] Add logging buffer management --- tasmota/support.ino | 16 +++++++++++++- tasmota/support_tasmota.ino | 8 +++++-- tasmota/tasmota.ino | 2 +- tasmota/xdrv_01_webserver.ino | 40 +++++++++++++++++------------------ tasmota/xdrv_02_mqtt.ino | 3 ++- tasmota/xdrv_40_telegram.ino | 40 +++++++++++++++++------------------ 6 files changed, 62 insertions(+), 47 deletions(-) diff --git a/tasmota/support.ino b/tasmota/support.ino index b54a15f28..85840fb8a 100644 --- a/tasmota/support.ino +++ b/tasmota/support.ino @@ -1924,9 +1924,11 @@ void Syslog(void) } } -void SyslogAsync(void) { +void SyslogAsync(bool refresh) { static uint32_t index = 1; + if (refresh && !NeedLogRefresh(TasmotaGlobal.syslog_level, index)) { return; } + char* line; size_t len; while (GetLog(TasmotaGlobal.syslog_level, &index, &line, &len)) { @@ -1940,6 +1942,16 @@ void SyslogAsync(void) { } } +bool NeedLogRefresh(uint32_t req_loglevel, uint32_t index) { + // Skip initial buffer fill + if (strlen(TasmotaGlobal.log_buffer) < LOG_BUFFER_SIZE - LOGSZ) { return false; } + + char* line; + size_t len; + if (!GetLog(req_loglevel, &index, &line, &len)) { return false; } + return ((line - TasmotaGlobal.log_buffer) < LOG_BUFFER_SIZE / 4); +} + bool GetLog(uint32_t req_loglevel, uint32_t* index_p, char** entry_pp, size_t* len_p) { uint32_t index = *index_p; @@ -1996,6 +2008,8 @@ void AddLog(uint32_t loglevel) { uint32_t highest_loglevel = Settings.weblog_level; if (Settings.mqttlog_level > highest_loglevel) { highest_loglevel = Settings.mqttlog_level; } if (TasmotaGlobal.syslog_level > highest_loglevel) { highest_loglevel = TasmotaGlobal.syslog_level; } + if (TasmotaGlobal.templog_level > highest_loglevel) { highest_loglevel = TasmotaGlobal.templog_level; } + if ((loglevel <= highest_loglevel) && // Log only when needed (TasmotaGlobal.masterlog_level <= highest_loglevel)) { // Delimited, zero-terminated buffer of log lines. diff --git a/tasmota/support_tasmota.ino b/tasmota/support_tasmota.ino index f9a501b39..98da69ee2 100644 --- a/tasmota/support_tasmota.ino +++ b/tasmota/support_tasmota.ino @@ -845,8 +845,8 @@ void PerformEverySecond(void) } } - MqttPublishLoggingAsync(); - SyslogAsync(); + MqttPublishLoggingAsync(false); + SyslogAsync(false); ResetGlobalValues(); @@ -984,6 +984,10 @@ void Every250mSeconds(void) SetLedPower(tstate); } + // Check if log refresh needed in case of fast buffer fill + MqttPublishLoggingAsync(true); + SyslogAsync(true); + /*-------------------------------------------------------------------------------------------*\ * Every second at 0.25 second interval \*-------------------------------------------------------------------------------------------*/ diff --git a/tasmota/tasmota.ino b/tasmota/tasmota.ino index 300c09b68..5befb1a68 100644 --- a/tasmota/tasmota.ino +++ b/tasmota/tasmota.ino @@ -151,6 +151,7 @@ struct { uint8_t masterlog_level; // Master log level used to override set log level uint8_t seriallog_level; // Current copy of Settings.seriallog_level uint8_t syslog_level; // Current copy of Settings.syslog_level + uint8_t templog_level; // Temporary log level to be used by HTTP cm and Telegram uint8_t module_type; // Current copy of Settings.module or user template type uint8_t last_source; // Last command source uint8_t shutters_present; // Number of actual define shutters @@ -197,7 +198,6 @@ void setup(void) { memset(&TasmotaGlobal, 0, sizeof(TasmotaGlobal)); TasmotaGlobal.baudrate = APP_BAUDRATE; TasmotaGlobal.seriallog_timer = SERIALLOG_TIMER; - TasmotaGlobal.gpio_optiona.data = 0; TasmotaGlobal.temperature_celsius = NAN; TasmotaGlobal.blinks = 201; TasmotaGlobal.wifi_state_flag = WIFI_RESTART; diff --git a/tasmota/xdrv_01_webserver.ino b/tasmota/xdrv_01_webserver.ino index e2de549fa..f0f65e77b 100644 --- a/tasmota/xdrv_01_webserver.ino +++ b/tasmota/xdrv_01_webserver.ino @@ -3017,32 +3017,30 @@ void HandleHttpCommand(void) } WSContentBegin(200, CT_JSON); - uint32_t curridx = TasmotaGlobal.log_buffer_pointer; String svalue = Webserver->arg("cmnd"); if (svalue.length() && (svalue.length() < MQTT_MAX_PACKET_SIZE)) { + uint32_t curridx = TasmotaGlobal.log_buffer_pointer; + TasmotaGlobal.templog_level = LOG_LEVEL_INFO; ExecuteWebCommand((char*)svalue.c_str(), SRC_WEBCOMMAND); - if (TasmotaGlobal.log_buffer_pointer != curridx) { - WSContentSend_P(PSTR("{")); - bool cflg = false; - uint32_t index = curridx; - char* line; - size_t len; - while (GetLog(Settings.weblog_level, &index, &line, &len)) { - // [14:49:36.123 MQTT: stat/wemos5/RESULT = {"POWER":"OFF"}] > [{"POWER":"OFF"}] - char* JSON = (char*)memchr(line, '{', len); - if (JSON) { // Is it a JSON message (and not only [15:26:08 MQT: stat/wemos5/POWER = O]) - size_t JSONlen = len - (JSON - line); - if (JSONlen > sizeof(TasmotaGlobal.mqtt_data)) { JSONlen = sizeof(TasmotaGlobal.mqtt_data); } - char stemp[JSONlen]; - strlcpy(stemp, JSON +1, JSONlen -2); - WSContentSend_P(PSTR("%s%s"), (cflg) ? "," : "", stemp); - cflg = true; - } + WSContentSend_P(PSTR("{")); + bool cflg = false; + uint32_t index = curridx; + char* line; + size_t len; + while (GetLog(TasmotaGlobal.templog_level, &index, &line, &len)) { + // [14:49:36.123 MQTT: stat/wemos5/RESULT = {"POWER":"OFF"}] > [{"POWER":"OFF"}] + char* JSON = (char*)memchr(line, '{', len); + if (JSON) { // Is it a JSON message (and not only [15:26:08 MQT: stat/wemos5/POWER = O]) + size_t JSONlen = len - (JSON - line); + if (JSONlen > sizeof(TasmotaGlobal.mqtt_data)) { JSONlen = sizeof(TasmotaGlobal.mqtt_data); } + char stemp[JSONlen]; + strlcpy(stemp, JSON +1, JSONlen -2); + WSContentSend_P(PSTR("%s%s"), (cflg) ? "," : "", stemp); + cflg = true; } - WSContentSend_P(PSTR("}")); - } else { - WSContentSend_P(PSTR("{\"" D_RSLT_WARNING "\":\"" D_ENABLE_WEBLOG_FOR_RESPONSE "\"}")); } + WSContentSend_P(PSTR("}")); + TasmotaGlobal.templog_level = 0; } else { WSContentSend_P(PSTR("{\"" D_RSLT_WARNING "\":\"" D_ENTER_COMMAND " cmnd=\"}")); } diff --git a/tasmota/xdrv_02_mqtt.ino b/tasmota/xdrv_02_mqtt.ino index 87d1cf4a5..a91e801e2 100644 --- a/tasmota/xdrv_02_mqtt.ino +++ b/tasmota/xdrv_02_mqtt.ino @@ -291,10 +291,11 @@ void MqttUnsubscribe(const char *topic) MqttUnsubscribeLib(topic); } -void MqttPublishLoggingAsync(void) { +void MqttPublishLoggingAsync(bool refresh) { static uint32_t index = 1; if (!Settings.flag.mqtt_enabled) { return; } // SetOption3 - Enable MQTT + if (refresh && !NeedLogRefresh(Settings.mqttlog_level, index)) { return; } char* line; size_t len; diff --git a/tasmota/xdrv_40_telegram.ino b/tasmota/xdrv_40_telegram.ino index cf4ab4baa..628ae0782 100644 --- a/tasmota/xdrv_40_telegram.ino +++ b/tasmota/xdrv_40_telegram.ino @@ -290,30 +290,28 @@ String TelegramExecuteCommand(const char *svalue) { String response = ""; uint32_t curridx = TasmotaGlobal.log_buffer_pointer; + TasmotaGlobal.templog_level = LOG_LEVEL_INFO; ExecuteCommand(svalue, SRC_CHAT); - if (TasmotaGlobal.log_buffer_pointer != curridx) { - response = F("{"); - bool cflg = false; - uint32_t index = curridx; - char* line; - size_t len; - while (GetLog(Settings.weblog_level, &index, &line, &len)) { - // [14:49:36.123 MQTT: stat/wemos5/RESULT = {"POWER":"OFF"}] > [{"POWER":"OFF"}] - char* JSON = (char*)memchr(line, '{', len); - if (JSON) { // Is it a JSON message (and not only [15:26:08 MQT: stat/wemos5/POWER = O]) - size_t JSONlen = len - (JSON - line); - if (JSONlen > sizeof(TasmotaGlobal.mqtt_data)) { JSONlen = sizeof(TasmotaGlobal.mqtt_data); } - char stemp[JSONlen]; - strlcpy(stemp, JSON +1, JSONlen -2); - if (cflg) { response += F(","); } - response += stemp; - cflg = true; - } + response = F("{"); + bool cflg = false; + uint32_t index = curridx; + char* line; + size_t len; + while (GetLog(TasmotaGlobal.templog_level, &index, &line, &len)) { + // [14:49:36.123 MQTT: stat/wemos5/RESULT = {"POWER":"OFF"}] > [{"POWER":"OFF"}] + char* JSON = (char*)memchr(line, '{', len); + if (JSON) { // Is it a JSON message (and not only [15:26:08 MQT: stat/wemos5/POWER = O]) + size_t JSONlen = len - (JSON - line); + if (JSONlen > sizeof(TasmotaGlobal.mqtt_data)) { JSONlen = sizeof(TasmotaGlobal.mqtt_data); } + char stemp[JSONlen]; + strlcpy(stemp, JSON +1, JSONlen -2); + if (cflg) { response += F(","); } + response += stemp; + cflg = true; } - response += F("}"); - } else { - response = F("{\"" D_RSLT_WARNING "\":\"" D_ENABLE_WEBLOG_FOR_RESPONSE "\"}"); } + response += F("}"); + TasmotaGlobal.templog_level = 0; return response; }