mirror of
https://github.com/arendst/Tasmota.git
synced 2025-07-26 20:26:32 +00:00
Add logging buffer management
This commit is contained in:
parent
845b89d260
commit
244611f12f
@ -1924,9 +1924,11 @@ void Syslog(void)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void SyslogAsync(void) {
|
void SyslogAsync(bool refresh) {
|
||||||
static uint32_t index = 1;
|
static uint32_t index = 1;
|
||||||
|
|
||||||
|
if (refresh && !NeedLogRefresh(TasmotaGlobal.syslog_level, index)) { return; }
|
||||||
|
|
||||||
char* line;
|
char* line;
|
||||||
size_t len;
|
size_t len;
|
||||||
while (GetLog(TasmotaGlobal.syslog_level, &index, &line, &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) {
|
bool GetLog(uint32_t req_loglevel, uint32_t* index_p, char** entry_pp, size_t* len_p) {
|
||||||
uint32_t index = *index_p;
|
uint32_t index = *index_p;
|
||||||
|
|
||||||
@ -1996,6 +2008,8 @@ void AddLog(uint32_t loglevel) {
|
|||||||
uint32_t highest_loglevel = Settings.weblog_level;
|
uint32_t highest_loglevel = Settings.weblog_level;
|
||||||
if (Settings.mqttlog_level > highest_loglevel) { highest_loglevel = Settings.mqttlog_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.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
|
if ((loglevel <= highest_loglevel) && // Log only when needed
|
||||||
(TasmotaGlobal.masterlog_level <= highest_loglevel)) {
|
(TasmotaGlobal.masterlog_level <= highest_loglevel)) {
|
||||||
// Delimited, zero-terminated buffer of log lines.
|
// Delimited, zero-terminated buffer of log lines.
|
||||||
|
@ -845,8 +845,8 @@ void PerformEverySecond(void)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
MqttPublishLoggingAsync();
|
MqttPublishLoggingAsync(false);
|
||||||
SyslogAsync();
|
SyslogAsync(false);
|
||||||
|
|
||||||
ResetGlobalValues();
|
ResetGlobalValues();
|
||||||
|
|
||||||
@ -984,6 +984,10 @@ void Every250mSeconds(void)
|
|||||||
SetLedPower(tstate);
|
SetLedPower(tstate);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Check if log refresh needed in case of fast buffer fill
|
||||||
|
MqttPublishLoggingAsync(true);
|
||||||
|
SyslogAsync(true);
|
||||||
|
|
||||||
/*-------------------------------------------------------------------------------------------*\
|
/*-------------------------------------------------------------------------------------------*\
|
||||||
* Every second at 0.25 second interval
|
* Every second at 0.25 second interval
|
||||||
\*-------------------------------------------------------------------------------------------*/
|
\*-------------------------------------------------------------------------------------------*/
|
||||||
|
@ -151,6 +151,7 @@ struct {
|
|||||||
uint8_t masterlog_level; // Master log level used to override set log level
|
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 seriallog_level; // Current copy of Settings.seriallog_level
|
||||||
uint8_t syslog_level; // Current copy of Settings.syslog_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 module_type; // Current copy of Settings.module or user template type
|
||||||
uint8_t last_source; // Last command source
|
uint8_t last_source; // Last command source
|
||||||
uint8_t shutters_present; // Number of actual define shutters
|
uint8_t shutters_present; // Number of actual define shutters
|
||||||
@ -197,7 +198,6 @@ void setup(void) {
|
|||||||
memset(&TasmotaGlobal, 0, sizeof(TasmotaGlobal));
|
memset(&TasmotaGlobal, 0, sizeof(TasmotaGlobal));
|
||||||
TasmotaGlobal.baudrate = APP_BAUDRATE;
|
TasmotaGlobal.baudrate = APP_BAUDRATE;
|
||||||
TasmotaGlobal.seriallog_timer = SERIALLOG_TIMER;
|
TasmotaGlobal.seriallog_timer = SERIALLOG_TIMER;
|
||||||
TasmotaGlobal.gpio_optiona.data = 0;
|
|
||||||
TasmotaGlobal.temperature_celsius = NAN;
|
TasmotaGlobal.temperature_celsius = NAN;
|
||||||
TasmotaGlobal.blinks = 201;
|
TasmotaGlobal.blinks = 201;
|
||||||
TasmotaGlobal.wifi_state_flag = WIFI_RESTART;
|
TasmotaGlobal.wifi_state_flag = WIFI_RESTART;
|
||||||
|
@ -3017,32 +3017,30 @@ void HandleHttpCommand(void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
WSContentBegin(200, CT_JSON);
|
WSContentBegin(200, CT_JSON);
|
||||||
uint32_t curridx = TasmotaGlobal.log_buffer_pointer;
|
|
||||||
String svalue = Webserver->arg("cmnd");
|
String svalue = Webserver->arg("cmnd");
|
||||||
if (svalue.length() && (svalue.length() < MQTT_MAX_PACKET_SIZE)) {
|
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);
|
ExecuteWebCommand((char*)svalue.c_str(), SRC_WEBCOMMAND);
|
||||||
if (TasmotaGlobal.log_buffer_pointer != curridx) {
|
WSContentSend_P(PSTR("{"));
|
||||||
WSContentSend_P(PSTR("{"));
|
bool cflg = false;
|
||||||
bool cflg = false;
|
uint32_t index = curridx;
|
||||||
uint32_t index = curridx;
|
char* line;
|
||||||
char* line;
|
size_t len;
|
||||||
size_t len;
|
while (GetLog(TasmotaGlobal.templog_level, &index, &line, &len)) {
|
||||||
while (GetLog(Settings.weblog_level, &index, &line, &len)) {
|
// [14:49:36.123 MQTT: stat/wemos5/RESULT = {"POWER":"OFF"}] > [{"POWER":"OFF"}]
|
||||||
// [14:49:36.123 MQTT: stat/wemos5/RESULT = {"POWER":"OFF"}] > [{"POWER":"OFF"}]
|
char* JSON = (char*)memchr(line, '{', len);
|
||||||
char* JSON = (char*)memchr(line, '{', len);
|
if (JSON) { // Is it a JSON message (and not only [15:26:08 MQT: stat/wemos5/POWER = O])
|
||||||
if (JSON) { // Is it a JSON message (and not only [15:26:08 MQT: stat/wemos5/POWER = O])
|
size_t JSONlen = len - (JSON - line);
|
||||||
size_t JSONlen = len - (JSON - line);
|
if (JSONlen > sizeof(TasmotaGlobal.mqtt_data)) { JSONlen = sizeof(TasmotaGlobal.mqtt_data); }
|
||||||
if (JSONlen > sizeof(TasmotaGlobal.mqtt_data)) { JSONlen = sizeof(TasmotaGlobal.mqtt_data); }
|
char stemp[JSONlen];
|
||||||
char stemp[JSONlen];
|
strlcpy(stemp, JSON +1, JSONlen -2);
|
||||||
strlcpy(stemp, JSON +1, JSONlen -2);
|
WSContentSend_P(PSTR("%s%s"), (cflg) ? "," : "", stemp);
|
||||||
WSContentSend_P(PSTR("%s%s"), (cflg) ? "," : "", stemp);
|
cflg = true;
|
||||||
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 {
|
} else {
|
||||||
WSContentSend_P(PSTR("{\"" D_RSLT_WARNING "\":\"" D_ENTER_COMMAND " cmnd=\"}"));
|
WSContentSend_P(PSTR("{\"" D_RSLT_WARNING "\":\"" D_ENTER_COMMAND " cmnd=\"}"));
|
||||||
}
|
}
|
||||||
|
@ -291,10 +291,11 @@ void MqttUnsubscribe(const char *topic)
|
|||||||
MqttUnsubscribeLib(topic);
|
MqttUnsubscribeLib(topic);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MqttPublishLoggingAsync(void) {
|
void MqttPublishLoggingAsync(bool refresh) {
|
||||||
static uint32_t index = 1;
|
static uint32_t index = 1;
|
||||||
|
|
||||||
if (!Settings.flag.mqtt_enabled) { return; } // SetOption3 - Enable MQTT
|
if (!Settings.flag.mqtt_enabled) { return; } // SetOption3 - Enable MQTT
|
||||||
|
if (refresh && !NeedLogRefresh(Settings.mqttlog_level, index)) { return; }
|
||||||
|
|
||||||
char* line;
|
char* line;
|
||||||
size_t len;
|
size_t len;
|
||||||
|
@ -290,30 +290,28 @@ String TelegramExecuteCommand(const char *svalue) {
|
|||||||
String response = "";
|
String response = "";
|
||||||
|
|
||||||
uint32_t curridx = TasmotaGlobal.log_buffer_pointer;
|
uint32_t curridx = TasmotaGlobal.log_buffer_pointer;
|
||||||
|
TasmotaGlobal.templog_level = LOG_LEVEL_INFO;
|
||||||
ExecuteCommand(svalue, SRC_CHAT);
|
ExecuteCommand(svalue, SRC_CHAT);
|
||||||
if (TasmotaGlobal.log_buffer_pointer != curridx) {
|
response = F("{");
|
||||||
response = F("{");
|
bool cflg = false;
|
||||||
bool cflg = false;
|
uint32_t index = curridx;
|
||||||
uint32_t index = curridx;
|
char* line;
|
||||||
char* line;
|
size_t len;
|
||||||
size_t len;
|
while (GetLog(TasmotaGlobal.templog_level, &index, &line, &len)) {
|
||||||
while (GetLog(Settings.weblog_level, &index, &line, &len)) {
|
// [14:49:36.123 MQTT: stat/wemos5/RESULT = {"POWER":"OFF"}] > [{"POWER":"OFF"}]
|
||||||
// [14:49:36.123 MQTT: stat/wemos5/RESULT = {"POWER":"OFF"}] > [{"POWER":"OFF"}]
|
char* JSON = (char*)memchr(line, '{', len);
|
||||||
char* JSON = (char*)memchr(line, '{', len);
|
if (JSON) { // Is it a JSON message (and not only [15:26:08 MQT: stat/wemos5/POWER = O])
|
||||||
if (JSON) { // Is it a JSON message (and not only [15:26:08 MQT: stat/wemos5/POWER = O])
|
size_t JSONlen = len - (JSON - line);
|
||||||
size_t JSONlen = len - (JSON - line);
|
if (JSONlen > sizeof(TasmotaGlobal.mqtt_data)) { JSONlen = sizeof(TasmotaGlobal.mqtt_data); }
|
||||||
if (JSONlen > sizeof(TasmotaGlobal.mqtt_data)) { JSONlen = sizeof(TasmotaGlobal.mqtt_data); }
|
char stemp[JSONlen];
|
||||||
char stemp[JSONlen];
|
strlcpy(stemp, JSON +1, JSONlen -2);
|
||||||
strlcpy(stemp, JSON +1, JSONlen -2);
|
if (cflg) { response += F(","); }
|
||||||
if (cflg) { response += F(","); }
|
response += stemp;
|
||||||
response += stemp;
|
cflg = true;
|
||||||
cflg = true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
response += F("}");
|
|
||||||
} else {
|
|
||||||
response = F("{\"" D_RSLT_WARNING "\":\"" D_ENABLE_WEBLOG_FOR_RESPONSE "\"}");
|
|
||||||
}
|
}
|
||||||
|
response += F("}");
|
||||||
|
TasmotaGlobal.templog_level = 0;
|
||||||
|
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user