Add logging buffer management

This commit is contained in:
Theo Arends 2020-12-22 15:26:07 +01:00
parent 845b89d260
commit 244611f12f
6 changed files with 62 additions and 47 deletions

View File

@ -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.

View File

@ -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
\*-------------------------------------------------------------------------------------------*/

View File

@ -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;

View File

@ -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=\"}"));
}

View File

@ -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;

View File

@ -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;
}