Add ESP3.0 log like support for socket debug in web files

This commit is contained in:
Luc 2020-04-19 13:23:57 +02:00
parent c8007cca38
commit 3291a90bf2
4 changed files with 114 additions and 131 deletions

View File

@ -1562,7 +1562,7 @@ void CONFIG::print_config (tpipe output, bool plaintext, ESPResponseStream *esp
}
#else
//go next record
station = STAILQ_NEXT (station, next);
station = STAILQ_NEXT (station, next);
}
wifi_softap_free_station_info();
#endif
@ -1979,3 +1979,50 @@ void CONFIG::print_config (tpipe output, bool plaintext, ESPResponseStream *esp
ESPCOM::print (F ("\n"), output, espresponse);
}
}
#ifdef DEBUG_OUTPUT_SOCKET
#if defined(ARDUINO_ARCH_ESP8266)
#define NODEBUG_WEBSOCKETS
#include <WebSocketsServer.h>
extern WebSocketsServer * socket_server;
const char * pathToFileName(const char * path)
{
size_t i = 0;
size_t pos = 0;
char * p = (char *)path;
while(*p) {
i++;
if(*p == '/' || *p == '\\') {
pos = i;
}
p++;
}
return path+pos;
}
#endif //ARDUINO_ARCH_ESP8266
void log_socket(const char *format, ...){
if(socket_server){
char loc_buf[255];
char * temp = loc_buf;
va_list arg;
va_list copy;
va_start(arg, format);
va_copy(copy, arg);
size_t len = vsnprintf(NULL, 0, format, arg);
va_end(copy);
if(len >= sizeof(loc_buf)){
temp = new char[len+1];
if(temp == NULL) {
return;
}
}
len = vsnprintf(temp, len+1, format, arg);
socket_server->sendBIN(ESPCOM::current_socket_id,(uint8_t *)temp,strlen(temp));
va_end(arg);
if(len > 255){
delete[] temp;
}
}
}
#endif

View File

@ -19,7 +19,7 @@
*/
//version and sources location
#define FW_VERSION "2.1.1.b3"
#define FW_VERSION "2.1.1.b4"
#define REPOSITORY "https://github.com/luc-github/ESP3D"
//Customize ESP3D ////////////////////////////////////////////////////////////////////////
@ -211,6 +211,14 @@ using fs::File;
//NOT YET IMPLEMENTED!!! Keep it as TODO
//#define WEBHOST_SDCARD_FEATURE
#ifdef DEBUG_OUTPUT_SOCKET
extern void log_socket(const char *format, ...);
extern const char * pathToFileName(const char * path);
#define log_esp3d(format, ...) log_socket("\n[ESP3D][%s:%u] %s(): " format "\n", pathToFileName(__FILE__), __LINE__, __FUNCTION__, ##__VA_ARGS__)
#else
#define log_esp3d(format, ...)
#endif
#ifdef DEBUG_ESP3D
#ifdef DEBUG_OUTPUT_SPIFFS
#ifndef FS_NO_GLOBALS

View File

@ -61,6 +61,7 @@
#include "syncwebserver.h"
WebSocketsServer * socket_server;
#define ESP_ERROR_AUTHENTICATION 1
#define ESP_ERROR_FILE_CREATION 2
#define ESP_ERROR_FILE_WRITE 3
@ -74,6 +75,7 @@ WebSocketsServer * socket_server;
#define ESP_ERROR_BUFFER_OVERFLOW 11
#define ESP_ERROR_START_UPLOAD 12
void pushError(int code, const char * st, bool web_error = 500, uint16_t timeout = 1000){
if (socket_server && st) {
String s = "ERROR:" + String(code) + ":";
@ -725,7 +727,7 @@ void WebUpdateUpload()
if(web_interface->is_authenticated() != LEVEL_ADMIN) {
web_interface->_upload_status=UPLOAD_STATUS_FAILED;
ESPCOM::println (F ("Update failed"), PRINTER_PIPE);
LOG("Web Update failed\r\n");
log_esp3d("Web Update failed");
pushError(ESP_ERROR_AUTHENTICATION, "Upload rejected",401);
} else {
//get current file ID
@ -853,22 +855,7 @@ void handle_not_found()
String path = web_interface->web_server.urlDecode(web_interface->web_server.uri());
String contentType = web_interface->getContentType(path);
String pathWithGz = path + ".gz";
LOG("request:")
LOG(path)
LOG("\r\n")
#ifdef DEBUG_ESP3D
int nb = web_interface->web_server.args();
for (int i = 0 ; i < nb; i++) {
LOG(web_interface->web_server.argName(i))
LOG(":")
LOG(web_interface->web_server.arg(i))
LOG("\r\n")
}
#endif
LOG("type:")
LOG(contentType)
LOG("\r\n")
log_esp3d("Not found %s, type %s", path.c_str(), contentType.c_str());
if(SPIFFS.exists(pathWithGz) || SPIFFS.exists(path)) {
if(SPIFFS.exists(pathWithGz)) {
path = pathWithGz;
@ -901,7 +888,7 @@ void handle_not_found()
return;
}
#endif
LOG("Page not found\r\n")
log_esp3d("Page not found");
path = F("/404.htm");
contentType = web_interface->getContentType(path);
pathWithGz = path + F(".gz");
@ -946,17 +933,6 @@ void handle_web_command()
}*/
String buffer2send = "";
ESPResponseStream espresponse;
LOG(String (web_interface->web_server.args()))
LOG(" Web command\r\n")
#ifdef DEBUG_ESP3D
int nb = web_interface->web_server.args();
for (int i = 0 ; i < nb; i++) {
LOG(web_interface->web_server.argName(i))
LOG(":")
LOG(web_interface->web_server.arg(i))
LOG("\r\n")
}
#endif
String cmd = "";
if (web_interface->web_server.hasArg("plain") || web_interface->web_server.hasArg("commandText")) {
if (web_interface->web_server.hasArg("plain")) {
@ -964,11 +940,9 @@ void handle_web_command()
} else {
cmd = web_interface->web_server.arg("commandText");
}
LOG("Web Command:")
LOG(cmd)
LOG("\r\n")
log_esp3d("WebCommand %s",cmd.c_str());
} else {
LOG("invalid argument\r\n")
log_esp3d("Invalid arg");
web_interface->web_server.send(200,"text/plain","Invalid command");
return;
}
@ -1008,26 +982,25 @@ void handle_web_command()
if ((web_interface->blockserial) == false) {
//block every query
web_interface->blockserial = true;
LOG("Block Serial\r\n")
log_esp3d("Block Serial");
//empty the serial buffer and incoming data
LOG("Start PurgeSerial\r\n")
log_esp3d("Start PurgeSerial");
if(ESPCOM::available(DEFAULT_PRINTER_PIPE)) {
ESPCOM::bridge();
CONFIG::wait(1);
}
LOG("End PurgeSerial\r\n")
log_esp3d("End PurgeSerial");
web_interface->web_server.setContentLength(CONTENT_LENGTH_UNKNOWN);
web_interface->web_server.sendHeader("Content-Type","text/plain",true);
web_interface->web_server.sendHeader("Cache-Control","no-cache");
web_interface->web_server.send(200);
//send command
LOG(String(cmd.length()))
LOG("Start PurgeSerial\r\n")
log_esp3d("Start PurgeSerial");
if(ESPCOM::available(DEFAULT_PRINTER_PIPE)) {
ESPCOM::bridge();
CONFIG::wait(1);
}
LOG("End PurgeSerial\r\n")
log_esp3d("End PurgeSerial");
ESPCOM::println (cmd, DEFAULT_PRINTER_PIPE);
bool done = false;
String current_buffer;
@ -1041,7 +1014,7 @@ void handle_web_command()
while ((millis() - timeout < 2000) && !done) {
//give some time between each buffer
if (ESPCOM::available(DEFAULT_PRINTER_PIPE)) {
LOG("Got data\r\n")
log_esp3d("Got data");
timeout = millis();
size_t len = ESPCOM::available(DEFAULT_PRINTER_PIPE);
uint8_t sbuf[len+1];
@ -1049,12 +1022,10 @@ void handle_web_command()
ESPCOM::readBytes (DEFAULT_PRINTER_PIPE, sbuf, len);
//change buffer as string
sbuf[len]='\0';
LOG((const char*)sbuf)
LOG("\r\n")
//add buffer to current one if any
current_buffer += (char * ) sbuf;
while (current_buffer.indexOf("\n") !=-1) {
LOG("remove newline")
log_esp3d("Remove new line");
//remove the possible "\r"
current_buffer.replace("\r","");
//get line
@ -1063,17 +1034,11 @@ void handle_web_command()
if ((current_line == "ok") || (current_line == "wait") || (current_line.startsWith("ok") && !((CONFIG::GetFirmwareTarget() == REPETIER) || (CONFIG::GetFirmwareTarget() == REPETIER4DV)))) {
done = true;
buffer2send +=current_line;
LOG("new buffer: ")
LOG(buffer2send)
LOG("\r\n")
log_esp3d("Found ok/wait add New buffer %s", buffer2send.c_str());
buffer2send +="\n";
LOG("Found ok or wait\r\n")
break;
}
//get the line and transmit it
LOG("Check command: ")
LOG(current_line)
LOG("\r\n")
//check command
if ((CONFIG::GetFirmwareTarget() == REPETIER) || (CONFIG::GetFirmwareTarget() == REPETIER4DV)) {
//save time no need to continue
@ -1088,7 +1053,7 @@ void handle_web_command()
}
}
if (temp_counter > 5) {
LOG("Timeout X5\r\n")
log_esp3d("Timeout X5");
done = true;
break;
}
@ -1099,16 +1064,12 @@ void handle_web_command()
}
} else {
buffer2send +=current_line;
LOG("new buffer: ")
LOG(buffer2send)
LOG("\r\n")
log_esp3d("New buffer %s", buffer2send.c_str());
buffer2send +="\n";
}
if (buffer2send.length() > 1200) {
web_interface->web_server.sendContent(buffer2send);
LOG("sending: ")
LOG(buffer2send)
LOG("\r\n")
log_esp3d("Sending %s", buffer2send.c_str());
buffer2send = "";
datasent = true;
}
@ -1120,34 +1081,31 @@ void handle_web_command()
CONFIG::wait (0);
} else {
CONFIG::wait(1);
LOG(".")
}
//it is sending too many temp status should be heating so let's exit the loop
if (temp_counter > 5) {
done = true;
}
}
LOG("Finished\r\n")
log_esp3d("Finished");
//to be sure connection close
if (buffer2send.length() > 0) {
web_interface->web_server.sendContent(buffer2send);
LOG("sending: ")
LOG(buffer2send)
LOG("\r\n")
log_esp3d("Sending %s", buffer2send.c_str());
datasent = true;
}
if (!datasent) {
web_interface->web_server.sendContent(" \r\n");
}
web_interface->web_server.sendContent("");
LOG("Start PurgeSerial\r\n")
log_esp3d("Start PurgeSerial");
if(ESPCOM::available(DEFAULT_PRINTER_PIPE)) {
ESPCOM::bridge();
CONFIG::wait(1);
}
LOG("End PurgeSerial\r\n")
log_esp3d("End PurgeSerial");
web_interface->blockserial = false;
LOG("Release Serial\r\n")
log_esp3d("Release PurgeSerial");
} else {
web_interface->web_server.send(200,"text/plain","Serial is busy, retry later!");
}
@ -1163,17 +1121,6 @@ void handle_web_command_silent()
return;
}
String buffer2send = "";
LOG(String (web_interface->web_server.args()))
LOG(" Web silent command\r\n")
#ifdef DEBUG_ESP3D
int nb = web_interface->web_server.args();
for (int i = 0 ; i < nb; i++) {
LOG(web_interface->web_server.argName(i))
LOG(":")
LOG(web_interface->web_server.arg(i))
LOG("\r\n")
}
#endif
String cmd = "";
//int count ;
if (web_interface->web_server.hasArg("plain") || web_interface->web_server.hasArg("commandText")) {
@ -1182,11 +1129,9 @@ void handle_web_command_silent()
} else {
cmd = web_interface->web_server.arg("commandText");
}
LOG("Web Command:")
LOG(cmd)
LOG("\r\n")
log_esp3d("Web Command:%s", cmd.c_str());
} else {
LOG("invalid argument\r\n")
log_esp3d("Invalid argument");
web_interface->web_server.send(200,"text/plain","Invalid command");
return;
}
@ -1219,7 +1164,6 @@ void handle_web_command_silent()
//send command to serial as no need to transfer ESP command
//to avoid any pollution if Uploading file to SDCard
if ((web_interface->blockserial) == false) {
LOG("Send Command\r\n")
//send command
ESPCOM::println (cmd, DEFAULT_PRINTER_PIPE);
web_interface->web_server.send(200,"text/plain","ok");
@ -1241,7 +1185,8 @@ void handle_serial_SDFileList()
web_interface->web_server.send(401, "application/json", "{\"status\":\"Authentication failed!\"}");
return;
}
LOG("serial SD upload done\r\n")
log_esp3d("Serial SD upload done");
String sstatus="Ok";
if ((web_interface->_upload_status == UPLOAD_STATUS_FAILED) || (web_interface->_upload_status == UPLOAD_STATUS_FAILED)) {
sstatus = "Upload failed";
@ -1270,7 +1215,7 @@ void SDFile_serial_upload()
web_interface->_upload_status=UPLOAD_STATUS_FAILED;
ESPCOM::println (F ("SD upload rejected"), PRINTER_PIPE);
pushError(ESP_ERROR_AUTHENTICATION, "Upload rejected", 401);
LOG("SD upload rejected\r\n");
log_esp3d("SD upload rejected");
} else {
//retrieve current file id
HTTPUpload& upload = (web_interface->web_server).upload();
@ -1279,7 +1224,7 @@ void SDFile_serial_upload()
//**************
if(upload.status == UPLOAD_FILE_START) {
web_interface->_upload_status= UPLOAD_STATUS_ONGOING;
LOG("Upload Start\r\n")
log_esp3d("Upload start");
String command = "M29";
String resetcmd = "M110 N0";
if (CONFIG::GetFirmwareTarget() == SMOOTHIEWARE)resetcmd = "N0 M110";
@ -1289,12 +1234,12 @@ void SDFile_serial_upload()
//it can failed for repetier
if ( ( CONFIG::GetFirmwareTarget() == REPETIER4DV) || (CONFIG::GetFirmwareTarget() == REPETIER) ) {
if(!sendLine2Serial (command,-1, NULL)){
LOG("Start Upload failed")
log_esp3d("Upload start failed");
web_interface->_upload_status= UPLOAD_STATUS_FAILED;
pushError(ESP_ERROR_START_UPLOAD, "Upload rejected");
}
} else {
LOG("Start Upload failed")
log_esp3d("Upload start failed");
web_interface->_upload_status= UPLOAD_STATUS_FAILED;
pushError(ESP_ERROR_START_UPLOAD, "Upload rejected");
}
@ -1302,14 +1247,14 @@ void SDFile_serial_upload()
//Mount SD card
command = "M21";
if(!sendLine2Serial (command,-1, NULL)){
LOG("Mounting SD failed")
log_esp3d("Mounting SD failed");
web_interface->_upload_status= UPLOAD_STATUS_FAILED;
pushError(ESP_ERROR_MOUNT_SD, "Mounting SD failed");
}
if (web_interface->_upload_status != UPLOAD_STATUS_FAILED) {
//Reset line numbering
if(!sendLine2Serial (resetcmd,-1, NULL)){
LOG("Reset Numbering failed")
log_esp3d("Reset Numbering failed");
web_interface->_upload_status= UPLOAD_STATUS_FAILED;
pushError(ESP_ERROR_RESET_NUMBERING, "Reset Numbering failed");
}
@ -1336,11 +1281,11 @@ void SDFile_serial_upload()
//additional purge, in case it is slow to answer
purge_serial();
web_interface->_upload_status= UPLOAD_STATUS_ONGOING;
LOG("Creation Ok\r\n")
log_esp3d("Creation Ok");
} else {
web_interface->_upload_status= UPLOAD_STATUS_FAILED;
LOG("Creation failed\r\n");
log_esp3d("Creation failed");
pushError(ESP_ERROR_FILE_CREATION, "File creation failed");
}
}
@ -1354,7 +1299,7 @@ void SDFile_serial_upload()
CONFIG::wait(0);
//it is a comment
if (upload.buf[pos] == ';') {
LOG ("Comment\r\n")
log_esp3d("Comment found");
is_comment = true;
}
//it is an end line
@ -1367,7 +1312,7 @@ void SDFile_serial_upload()
if (current_line.length() > 0 ) {
lineNb++;
if (!sendLine2Serial (current_line, lineNb, NULL) ) {
LOG ("Error sending line\n")
log_esp3d("Error sending line");
CloseSerialUpload (true, current_filename,lineNb);
web_interface->_upload_status= UPLOAD_STATUS_FAILED;
pushError(ESP_ERROR_FILE_WRITE, "File write failed");
@ -1376,11 +1321,11 @@ void SDFile_serial_upload()
current_line = "";
} else {
LOG ("Empy line\n")
log_esp3d ("Empy line");
}
} else {
//error buffer overload
LOG ("Error over buffer\n")
log_esp3d ("Error over buffer(1)");
lineNb++;
web_interface->_upload_status= UPLOAD_STATUS_FAILED;
pushError(ESP_ERROR_BUFFER_OVERFLOW, "Error buffer overflow");
@ -1389,7 +1334,7 @@ void SDFile_serial_upload()
if (current_line.length() < MAX_RESEND_BUFFER) {
current_line += char (upload.buf[pos]); //copy current char to buffer to send/resend
} else {
LOG ("Error over buffer\n")
log_esp3d ("Error over buffer(2)");
lineNb++;
web_interface->_upload_status= UPLOAD_STATUS_FAILED;
pushError(ESP_ERROR_BUFFER_OVERFLOW, "Error buffer overflow");
@ -1403,20 +1348,20 @@ void SDFile_serial_upload()
if (current_line.length() > 0) {
lineNb++;
if (!sendLine2Serial (current_line, lineNb, NULL) ) {
LOG ("Error sending buffer\n")
log_esp3d ("Error sending buffer");
lineNb++;
CloseSerialUpload (true, current_filename, lineNb);
web_interface->_upload_status= UPLOAD_STATUS_FAILED;
pushError(ESP_ERROR_FILE_WRITE, "File write failed");
}
}
LOG ("Upload finished ");
log_esp3d ("Upload finished");
lineNb++;
CloseSerialUpload (false, current_filename, lineNb);
//Upload cancelled
//**************
} else { //UPLOAD_FILE_ABORTED
LOG("Error, Something happened\r\n");
log_esp3d("Error, Something happened");
web_interface->_upload_status= UPLOAD_STATUS_FAILED;
//pushError(ESP_ERROR_UPLOAD_CANCELLED, "Upload cancelled");
}

View File

@ -96,29 +96,26 @@ bool purge_serial()
uint8_t buf [51];
ESPCOM::flush (DEFAULT_PRINTER_PIPE);
CONFIG::wait (5);
LOG("Purge Serial\r\n")
log_esp3d("Purge Serial");
while (ESPCOM::available(DEFAULT_PRINTER_PIPE) > 0 ) {
if ((millis() - start ) > 2000) {
LOG("Purge timeout\r\n")
log_esp3d("Purge timeout");
return false;
}
size_t len = ESPCOM::readBytes (DEFAULT_PRINTER_PIPE, buf, 50);
buf[len] = '\0';
LOG("Purge: \r\n************\r\n")
LOG((const char *)buf)
LOG("\r\n************\r\n")
if ( ( CONFIG::GetFirmwareTarget() == REPETIER4DV) || (CONFIG::GetFirmwareTarget() == REPETIER) ) {
String s = (const char *)buf;
//repetier never stop sending data so no need to wait if have 'wait' or 'busy'
if((s.indexOf ("wait") > -1) || (s.indexOf ("busy") > -1)) {
return true;
}
LOG("Purge interrupted\r\n")
log_esp3d("Purge interrupted");
}
CONFIG::wait (5);
}
CONFIG::wait (0);
LOG("Purge done\r\n")
log_esp3d("Purge done");
return true;
}
@ -148,9 +145,7 @@ uint32_t Get_lineNumber(String & response)
//remove potential unwished char
snum.replace("\r", "");
l = snum.toInt();
LOG("Line requested is ")
LOG(String(l))
LOG("\r\n")
log_esp3d("Line requested is %d", l);
return l;
}
@ -158,11 +153,7 @@ uint32_t Get_lineNumber(String & response)
//if newlinenb is NULL no auto correction of line number in case of resend
bool sendLine2Serial (String & line, int32_t linenb, int32_t * newlinenb)
{
LOG ("Send line ")
LOG (line )
LOG (" NB:")
LOG (String(linenb))
LOG ("\r\n")
log_esp3d ("Send line %d");
String line2send;
String sok = "ok";
String sresend = "Resend:";
@ -182,9 +173,6 @@ bool sendLine2Serial (String & line, int32_t linenb, int32_t * newlinenb)
}
//purge serial as nothing is supposed to interfere with upload
purge_serial();
LOG ("Send line ")
LOG (line2send )
LOG ("\r\n")
//send line
ESPCOM::println (line2send, DEFAULT_PRINTER_PIPE);
ESPCOM::flush(DEFAULT_PRINTER_PIPE);
@ -209,19 +197,16 @@ bool sendLine2Serial (String & line, int32_t linenb, int32_t * newlinenb)
sbuf[len] = '\0';
//use string because easier to handle and allow to re-assemble cutted answer
response += (const char*) sbuf;
LOG ("Response:\r\n************\r\n")
LOG (response)
LOG ("\r\n************\r\n")
//in that case there is no way to know what is the right number to use and so send should be failed
if (( ( CONFIG::GetFirmwareTarget() == REPETIER4DV) || (CONFIG::GetFirmwareTarget() == REPETIER) ) && (response.indexOf ("skip") != -1)) {
LOG ("Wrong line requested\r\n")
log_esp3d ("Wrong line requested");
count = 5;
}
//it is resend ?
int pos = response.indexOf (sresend);
//be sure we get full line to be able to process properly
if (( pos > -1) && (response.lastIndexOf("\n") > pos)) {
LOG ("Resend detected\r\n")
log_esp3d ("Resend detected");
uint32_t line_number = Get_lineNumber(response);
//this part is only if have newlinenb variable
if (newlinenb != nullptr) {
@ -232,20 +217,18 @@ bool sendLine2Serial (String & line, int32_t linenb, int32_t * newlinenb)
} else {
//the line requested is not the current one so we stop
if (line_number !=linenb) {
LOG ("Wrong line requested\r\n")
log_esp3d ("Wrong line requested");
count = 5;
}
}
count++;
if (count > 5) {
free(sbuf);
LOG ("Exit too many resend or wrong line\r\n")
log_esp3d ("Exit too many resend or wrong line");
return false;
}
purge_serial();
LOG ("Resend ")
LOG (String(count))
LOG ("\r\n")
log_esp3d ("Resend x%d", count);
response="";
ESPCOM::println (line2send, DEFAULT_PRINTER_PIPE);
ESPCOM::flush (DEFAULT_PRINTER_PIPE);
@ -255,7 +238,7 @@ bool sendLine2Serial (String & line, int32_t linenb, int32_t * newlinenb)
} else {
if ( (response.indexOf (sok) > -1) ) { //we have ok so it is done
free(sbuf);
LOG ("Got ok\r\n")
log_esp3d ("Got ok");
purge_serial();
return true;
}
@ -264,13 +247,13 @@ bool sendLine2Serial (String & line, int32_t linenb, int32_t * newlinenb)
}
//no answer or over buffer exit
if ( (millis() - timeout > 2000) || (response.length() >200)) {
LOG ("Time out\r\n")
log_esp3d("Time out");
done = true;
}
CONFIG::wait (5);
}
}
LOG ("Send line error\r\n")
log_esp3d ("Send line error");
return false;
}