Files
ESPEasy/src/WebServer_Log.ino
T
Gijs Noorlander 37538f4124 [Build Size] Replace all TXBuffer += calls where possible
The TXBuffer is a "header only" implementation.
This means it may be compiled "inline" and thus takes quite a bit of compiled code.
This makes the binary larger and slows the ESP down.

Now it is even possible to make core 2.6.3 builds using "minimal OTA".
The "Dev" build is roughly 55k smaller.
2020-02-28 02:28:03 +01:00

110 lines
3.5 KiB
Arduino

#include "src/Globals/Logging.h"
#include "src/Static/WebStaticData.h"
// ********************************************************************************
// Web Interface log page
// ********************************************************************************
void handle_log() {
if (!isLoggedIn()) { return; }
navMenuIndex = MENU_INDEX_TOOLS;
TXBuffer.startStream();
sendHeadandTail_stdtemplate(_HEAD);
html_table_class_normal();
#ifdef WEBSERVER_LOG
addHtml(F("<TR><TH id=\"headline\" align=\"left\">Log"));
addCopyButton(F("copyText"), "", F("Copy log to clipboard"));
addHtml(F(
"</TR></table><div id='current_loglevel' style='font-weight: bold;'>Logging: </div><div class='logviewer' id='copyText_1'></div>"));
addHtml(F("Autoscroll: "));
addCheckBox(F("autoscroll"), true);
addHtml(F("<BR></body>"));
html_add_script(true);
TXBuffer += DATA_FETCH_AND_PARSE_LOG_JS;
html_add_script_end();
#else // ifdef WEBSERVER_LOG
addHtml(F("Not included in build"));
#endif // ifdef WEBSERVER_LOG
sendHeadandTail_stdtemplate(_TAIL);
TXBuffer.endStream();
}
// ********************************************************************************
// Web Interface JSON log page
// ********************************************************************************
void handle_log_JSON() {
if (!isLoggedIn()) { return; }
#ifdef WEBSERVER_LOG
TXBuffer.startJsonStream();
String webrequest = WebServer.arg(F("view"));
addHtml(F("{\"Log\": {"));
if (webrequest == F("legend")) {
addHtml(F("\"Legend\": ["));
for (byte i = 0; i < LOG_LEVEL_NRELEMENTS; ++i) {
if (i != 0) {
addHtml(",");
}
addHtml("{");
int loglevel;
stream_next_json_object_value(F("label"), getLogLevelDisplayStringFromIndex(i, loglevel));
stream_last_json_object_value(F("loglevel"), String(loglevel));
}
addHtml(F("],\n"));
}
addHtml(F("\"Entries\": ["));
bool logLinesAvailable = true;
int nrEntries = 0;
unsigned long firstTimeStamp = 0;
unsigned long lastTimeStamp = 0;
while (logLinesAvailable) {
String reply = Logging.get_logjson_formatted(logLinesAvailable, lastTimeStamp);
if (reply.length() > 0) {
addHtml(reply);
if (nrEntries == 0) {
firstTimeStamp = lastTimeStamp;
}
++nrEntries;
}
// Do we need to do something here and maybe limit number of lines at once?
}
addHtml(F("],\n"));
long logTimeSpan = timeDiff(firstTimeStamp, lastTimeStamp);
long refreshSuggestion = 1000;
long newOptimum = 1000;
if ((nrEntries > 2) && (logTimeSpan > 1)) {
// May need to lower the TTL for refresh when time needed
// to fill half the log is lower than current TTL
newOptimum = logTimeSpan * (LOG_STRUCT_MESSAGE_LINES / 2);
newOptimum = newOptimum / (nrEntries - 1);
}
if (newOptimum < refreshSuggestion) { refreshSuggestion = newOptimum; }
if (refreshSuggestion < 100) {
// Reload times no lower than 100 msec.
refreshSuggestion = 100;
}
stream_next_json_object_value(F("TTL"), String(refreshSuggestion));
stream_next_json_object_value(F("timeHalfBuffer"), String(newOptimum));
stream_next_json_object_value(F("nrEntries"), String(nrEntries));
stream_next_json_object_value(F("SettingsWebLogLevel"), String(Settings.WebLogLevel));
stream_last_json_object_value(F("logTimeSpan"), String(logTimeSpan));
addHtml(F("}\n"));
TXBuffer.endStream();
updateLogLevelCache();
#else // ifdef WEBSERVER_LOG
handleNotFound();
#endif // ifdef WEBSERVER_LOG
}