Skip to content

Commit 29d5987

Browse files
meltdown03me-no-dev
authored andcommitted
Webserver library - fix logging (#2355) (#2359)
* Webserver fix logging (#1) * Change logging to use esp32-hal-log.h fixes #2355 * adjust log parameter output positions, reduce lines The DEBUG_ESP method used less lines than I originally set `log_v` to use when displaying the details of the received params ("@" and "=" indexes, and File info on a single line)
1 parent 8cbc60e commit 29d5987

File tree

2 files changed

+35
-148
lines changed

2 files changed

+35
-148
lines changed

libraries/WebServer/src/Parsing.cpp

+27-121
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,12 @@
2020
*/
2121

2222
#include <Arduino.h>
23+
#include <esp32-hal-log.h>
2324
#include "WiFiServer.h"
2425
#include "WiFiClient.h"
2526
#include "WebServer.h"
2627
#include "detail/mimetable.h"
2728

28-
//#define DEBUG_ESP_HTTP_SERVER
29-
#ifdef DEBUG_ESP_PORT
30-
#define DEBUG_OUTPUT DEBUG_ESP_PORT
31-
#else
32-
#define DEBUG_OUTPUT Serial
33-
#endif
34-
3529
#ifndef WEBSERVER_MAX_POST_ARGS
3630
#define WEBSERVER_MAX_POST_ARGS 32
3731
#endif
@@ -85,10 +79,7 @@ bool WebServer::_parseRequest(WiFiClient& client) {
8579
int addr_start = req.indexOf(' ');
8680
int addr_end = req.indexOf(' ', addr_start + 1);
8781
if (addr_start == -1 || addr_end == -1) {
88-
#ifdef DEBUG_ESP_HTTP_SERVER
89-
DEBUG_OUTPUT.print("Invalid request: ");
90-
DEBUG_OUTPUT.println(req);
91-
#endif
82+
log_e("Invalid request: %s", req.c_str());
9283
return false;
9384
}
9485

@@ -119,14 +110,7 @@ bool WebServer::_parseRequest(WiFiClient& client) {
119110
}
120111
_currentMethod = method;
121112

122-
#ifdef DEBUG_ESP_HTTP_SERVER
123-
DEBUG_OUTPUT.print("method: ");
124-
DEBUG_OUTPUT.print(methodStr);
125-
DEBUG_OUTPUT.print(" url: ");
126-
DEBUG_OUTPUT.print(url);
127-
DEBUG_OUTPUT.print(" search: ");
128-
DEBUG_OUTPUT.println(searchStr);
129-
#endif
113+
log_v("method: %s url: %s search: %s", methodStr.c_str(), url.c_str(), searchStr.c_str());
130114

131115
//attach handler
132116
RequestHandler* handler;
@@ -159,12 +143,8 @@ bool WebServer::_parseRequest(WiFiClient& client) {
159143
headerValue.trim();
160144
_collectHeader(headerName.c_str(),headerValue.c_str());
161145

162-
#ifdef DEBUG_ESP_HTTP_SERVER
163-
DEBUG_OUTPUT.print("headerName: ");
164-
DEBUG_OUTPUT.println(headerName);
165-
DEBUG_OUTPUT.print("headerValue: ");
166-
DEBUG_OUTPUT.println(headerValue);
167-
#endif
146+
log_v("headerName: %s", headerName.c_str());
147+
log_v("headerValue: %s", headerValue.c_str());
168148

169149
if (headerName.equalsIgnoreCase(FPSTR(Content_Type))){
170150
using namespace mime;
@@ -206,10 +186,7 @@ bool WebServer::_parseRequest(WiFiClient& client) {
206186
arg.value = String(plainBuf);
207187
}
208188

209-
#ifdef DEBUG_ESP_HTTP_SERVER
210-
DEBUG_OUTPUT.print("Plain: ");
211-
DEBUG_OUTPUT.println(plainBuf);
212-
#endif
189+
log_v("Plain: %s", plainBuf);
213190
free(plainBuf);
214191
} else {
215192
// No content - but we can still have arguments in the URL.
@@ -239,12 +216,8 @@ bool WebServer::_parseRequest(WiFiClient& client) {
239216
headerValue = req.substring(headerDiv + 2);
240217
_collectHeader(headerName.c_str(),headerValue.c_str());
241218

242-
#ifdef DEBUG_ESP_HTTP_SERVER
243-
DEBUG_OUTPUT.print("headerName: ");
244-
DEBUG_OUTPUT.println(headerName);
245-
DEBUG_OUTPUT.print("headerValue: ");
246-
DEBUG_OUTPUT.println(headerValue);
247-
#endif
219+
log_v("headerName: %s", headerName.c_str());
220+
log_v("headerValue: %s", headerValue.c_str());
248221

249222
if (headerName.equalsIgnoreCase("Host")){
250223
_hostHeader = headerValue;
@@ -254,12 +227,8 @@ bool WebServer::_parseRequest(WiFiClient& client) {
254227
}
255228
client.flush();
256229

257-
#ifdef DEBUG_ESP_HTTP_SERVER
258-
DEBUG_OUTPUT.print("Request: ");
259-
DEBUG_OUTPUT.println(url);
260-
DEBUG_OUTPUT.print(" Arguments: ");
261-
DEBUG_OUTPUT.println(searchStr);
262-
#endif
230+
log_v("Request: %s", url.c_str());
231+
log_v(" Arguments: %s", searchStr.c_str());
263232

264233
return true;
265234
}
@@ -275,10 +244,7 @@ bool WebServer::_collectHeader(const char* headerName, const char* headerValue)
275244
}
276245

277246
void WebServer::_parseArguments(String data) {
278-
#ifdef DEBUG_ESP_HTTP_SERVER
279-
DEBUG_OUTPUT.print("args: ");
280-
DEBUG_OUTPUT.println(data);
281-
#endif
247+
log_v("args: %s", data.c_str());
282248
if (_currentArgs)
283249
delete[] _currentArgs;
284250
_currentArgs = 0;
@@ -296,30 +262,17 @@ void WebServer::_parseArguments(String data) {
296262
++i;
297263
++_currentArgCount;
298264
}
299-
#ifdef DEBUG_ESP_HTTP_SERVER
300-
DEBUG_OUTPUT.print("args count: ");
301-
DEBUG_OUTPUT.println(_currentArgCount);
302-
#endif
265+
log_v("args count: %d", _currentArgCount);
303266

304267
_currentArgs = new RequestArgument[_currentArgCount+1];
305268
int pos = 0;
306269
int iarg;
307270
for (iarg = 0; iarg < _currentArgCount;) {
308271
int equal_sign_index = data.indexOf('=', pos);
309272
int next_arg_index = data.indexOf('&', pos);
310-
#ifdef DEBUG_ESP_HTTP_SERVER
311-
DEBUG_OUTPUT.print("pos ");
312-
DEBUG_OUTPUT.print(pos);
313-
DEBUG_OUTPUT.print("=@ ");
314-
DEBUG_OUTPUT.print(equal_sign_index);
315-
DEBUG_OUTPUT.print(" &@ ");
316-
DEBUG_OUTPUT.println(next_arg_index);
317-
#endif
273+
log_v("pos %d =@%d &@%d", pos, equal_sign_index, next_arg_index);
318274
if ((equal_sign_index == -1) || ((equal_sign_index > next_arg_index) && (next_arg_index != -1))) {
319-
#ifdef DEBUG_ESP_HTTP_SERVER
320-
DEBUG_OUTPUT.print("arg missing value: ");
321-
DEBUG_OUTPUT.println(iarg);
322-
#endif
275+
log_e("arg missing value: %d", iarg);
323276
if (next_arg_index == -1)
324277
break;
325278
pos = next_arg_index + 1;
@@ -328,24 +281,14 @@ void WebServer::_parseArguments(String data) {
328281
RequestArgument& arg = _currentArgs[iarg];
329282
arg.key = urlDecode(data.substring(pos, equal_sign_index));
330283
arg.value = urlDecode(data.substring(equal_sign_index + 1, next_arg_index));
331-
#ifdef DEBUG_ESP_HTTP_SERVER
332-
DEBUG_OUTPUT.print("arg ");
333-
DEBUG_OUTPUT.print(iarg);
334-
DEBUG_OUTPUT.print(" key: ");
335-
DEBUG_OUTPUT.print(arg.key);
336-
DEBUG_OUTPUT.print(" value: ");
337-
DEBUG_OUTPUT.println(arg.value);
338-
#endif
284+
log_v("arg %d key: %s value: %s", iarg, arg.key.c_str(), arg.value.c_str());
339285
++iarg;
340286
if (next_arg_index == -1)
341287
break;
342288
pos = next_arg_index + 1;
343289
}
344290
_currentArgCount = iarg;
345-
#ifdef DEBUG_ESP_HTTP_SERVER
346-
DEBUG_OUTPUT.print("args count: ");
347-
DEBUG_OUTPUT.println(_currentArgCount);
348-
#endif
291+
log_v("args count: %d", _currentArgCount);
349292

350293
}
351294

@@ -371,12 +314,7 @@ int WebServer::_uploadReadByte(WiFiClient& client){
371314

372315
bool WebServer::_parseForm(WiFiClient& client, String boundary, uint32_t len){
373316
(void) len;
374-
#ifdef DEBUG_ESP_HTTP_SERVER
375-
DEBUG_OUTPUT.print("Parse Form: Boundary: ");
376-
DEBUG_OUTPUT.print(boundary);
377-
DEBUG_OUTPUT.print(" Length: ");
378-
DEBUG_OUTPUT.println(len);
379-
#endif
317+
log_v("Parse Form: Boundary: %s Length: %d", boundary.c_str(), len);
380318
String line;
381319
int retry = 0;
382320
do {
@@ -410,18 +348,12 @@ bool WebServer::_parseForm(WiFiClient& client, String boundary, uint32_t len){
410348
argFilename = argName.substring(nameStart+2, argName.length() - 1);
411349
argName = argName.substring(0, argName.indexOf('"'));
412350
argIsFile = true;
413-
#ifdef DEBUG_ESP_HTTP_SERVER
414-
DEBUG_OUTPUT.print("PostArg FileName: ");
415-
DEBUG_OUTPUT.println(argFilename);
416-
#endif
351+
log_v("PostArg FileName: %s",argFilename.c_str());
417352
//use GET to set the filename if uploading using blob
418-
if (argFilename == F("blob") && hasArg(FPSTR(filename)))
353+
if (argFilename == F("blob") && hasArg(FPSTR(filename)))
419354
argFilename = arg(FPSTR(filename));
420355
}
421-
#ifdef DEBUG_ESP_HTTP_SERVER
422-
DEBUG_OUTPUT.print("PostArg Name: ");
423-
DEBUG_OUTPUT.println(argName);
424-
#endif
356+
log_v("PostArg Name: %s", argName.c_str());
425357
using namespace mime;
426358
argType = FPSTR(mimeTable[txt].mimeType);
427359
line = client.readStringUntil('\r');
@@ -432,10 +364,7 @@ bool WebServer::_parseForm(WiFiClient& client, String boundary, uint32_t len){
432364
client.readStringUntil('\r');
433365
client.readStringUntil('\n');
434366
}
435-
#ifdef DEBUG_ESP_HTTP_SERVER
436-
DEBUG_OUTPUT.print("PostArg Type: ");
437-
DEBUG_OUTPUT.println(argType);
438-
#endif
367+
log_v("PostArg Type: %s", argType.c_str());
439368
if (!argIsFile){
440369
while(1){
441370
line = client.readStringUntil('\r');
@@ -444,20 +373,14 @@ bool WebServer::_parseForm(WiFiClient& client, String boundary, uint32_t len){
444373
if (argValue.length() > 0) argValue += "\n";
445374
argValue += line;
446375
}
447-
#ifdef DEBUG_ESP_HTTP_SERVER
448-
DEBUG_OUTPUT.print("PostArg Value: ");
449-
DEBUG_OUTPUT.println(argValue);
450-
DEBUG_OUTPUT.println();
451-
#endif
376+
log_v("PostArg Value: %s", argValue.c_str());
452377

453378
RequestArgument& arg = _postArgs[_postArgsLen++];
454379
arg.key = argName;
455380
arg.value = argValue;
456381

457382
if (line == ("--"+boundary+"--")){
458-
#ifdef DEBUG_ESP_HTTP_SERVER
459-
DEBUG_OUTPUT.println("Done Parsing POST");
460-
#endif
383+
log_v("Done Parsing POST");
461384
break;
462385
}
463386
} else {
@@ -468,12 +391,7 @@ bool WebServer::_parseForm(WiFiClient& client, String boundary, uint32_t len){
468391
_currentUpload->type = argType;
469392
_currentUpload->totalSize = 0;
470393
_currentUpload->currentSize = 0;
471-
#ifdef DEBUG_ESP_HTTP_SERVER
472-
DEBUG_OUTPUT.print("Start File: ");
473-
DEBUG_OUTPUT.print(_currentUpload->filename);
474-
DEBUG_OUTPUT.print(" Type: ");
475-
DEBUG_OUTPUT.println(_currentUpload->type);
476-
#endif
394+
log_v("Start File: %s Type: %s", _currentUpload->filename.c_str(), _currentUpload->type.c_str());
477395
if(_currentHandler && _currentHandler->canUpload(_currentUri))
478396
_currentHandler->upload(*this, _currentUri, *_currentUpload);
479397
_currentUpload->status = UPLOAD_FILE_WRITE;
@@ -518,20 +436,11 @@ bool WebServer::_parseForm(WiFiClient& client, String boundary, uint32_t len){
518436
_currentUpload->status = UPLOAD_FILE_END;
519437
if(_currentHandler && _currentHandler->canUpload(_currentUri))
520438
_currentHandler->upload(*this, _currentUri, *_currentUpload);
521-
#ifdef DEBUG_ESP_HTTP_SERVER
522-
DEBUG_OUTPUT.print("End File: ");
523-
DEBUG_OUTPUT.print(_currentUpload->filename);
524-
DEBUG_OUTPUT.print(" Type: ");
525-
DEBUG_OUTPUT.print(_currentUpload->type);
526-
DEBUG_OUTPUT.print(" Size: ");
527-
DEBUG_OUTPUT.println(_currentUpload->totalSize);
528-
#endif
439+
log_v("End File: %s Type: %s Size: %d", _currentUpload->filename.c_str(), _currentUpload->type.c_str(), _currentUpload->totalSize);
529440
line = client.readStringUntil(0x0D);
530441
client.readStringUntil(0x0A);
531442
if (line == "--"){
532-
#ifdef DEBUG_ESP_HTTP_SERVER
533-
DEBUG_OUTPUT.println("Done Parsing POST");
534-
#endif
443+
log_v("Done Parsing POST");
535444
break;
536445
}
537446
continue;
@@ -579,10 +488,7 @@ bool WebServer::_parseForm(WiFiClient& client, String boundary, uint32_t len){
579488
}
580489
return true;
581490
}
582-
#ifdef DEBUG_ESP_HTTP_SERVER
583-
DEBUG_OUTPUT.print("Error: line: ");
584-
DEBUG_OUTPUT.println(line);
585-
#endif
491+
log_e("Error: line: %s", line.c_str());
586492
return false;
587493
}
588494

libraries/WebServer/src/WebServer.cpp

+8-27
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323

2424
#include <Arduino.h>
25+
#include <esp32-hal-log.h>
2526
#include <libb64/cencode.h>
2627
#include "WiFiServer.h"
2728
#include "WiFiClient.h"
@@ -30,12 +31,6 @@
3031
#include "detail/RequestHandlersImpl.h"
3132
#include "mbedtls/md5.h"
3233

33-
//#define DEBUG_ESP_HTTP_SERVER
34-
#ifdef DEBUG_ESP_PORT
35-
#define DEBUG_OUTPUT DEBUG_ESP_PORT
36-
#else
37-
#define DEBUG_OUTPUT Serial
38-
#endif
3934

4035
static const char AUTHORIZATION_HEADER[] = "Authorization";
4136
static const char qop_auth[] = "qop=auth";
@@ -163,9 +158,7 @@ bool WebServer::authenticate(const char * username, const char * password){
163158
delete[] encoded;
164159
} else if(authReq.startsWith(F("Digest"))) {
165160
authReq = authReq.substring(7);
166-
#ifdef DEBUG_ESP_HTTP_SERVER
167-
DEBUG_OUTPUT.println(authReq);
168-
#endif
161+
log_v("%s", authReq.c_str());
169162
String _username = _extractParam(authReq,F("username=\""));
170163
if(!_username.length() || _username != String(username)) {
171164
authReq = "";
@@ -193,9 +186,7 @@ bool WebServer::authenticate(const char * username, const char * password){
193186
_cnonce = _extractParam(authReq, F("cnonce=\""));
194187
}
195188
String _H1 = md5str(String(username) + ':' + _realm + ':' + String(password));
196-
#ifdef DEBUG_ESP_HTTP_SERVER
197-
DEBUG_OUTPUT.println("Hash of user:realm:pass=" + _H1);
198-
#endif
189+
log_v("Hash of user:realm:pass=%s", _H1.c_str());
199190
String _H2 = "";
200191
if(_currentMethod == HTTP_GET){
201192
_H2 = md5str(String(F("GET:")) + _uri);
@@ -208,18 +199,14 @@ bool WebServer::authenticate(const char * username, const char * password){
208199
}else{
209200
_H2 = md5str(String(F("GET:")) + _uri);
210201
}
211-
#ifdef DEBUG_ESP_HTTP_SERVER
212-
DEBUG_OUTPUT.println("Hash of GET:uri=" + _H2);
213-
#endif
202+
log_v("Hash of GET:uri=%s", _H2.c_str());
214203
String _responsecheck = "";
215204
if(authReq.indexOf(FPSTR(qop_auth)) != -1) {
216205
_responsecheck = md5str(_H1 + ':' + _nonce + ':' + _nc + ':' + _cnonce + F(":auth:") + _H2);
217206
} else {
218207
_responsecheck = md5str(_H1 + ':' + _nonce + ':' + _H2);
219208
}
220-
#ifdef DEBUG_ESP_HTTP_SERVER
221-
DEBUG_OUTPUT.println("The Proper response=" +_responsecheck);
222-
#endif
209+
log_v("The Proper response=%s", _responsecheck.c_str());
223210
if(_response == _responsecheck){
224211
authReq = "";
225212
return true;
@@ -294,9 +281,7 @@ void WebServer::handleClient() {
294281
return;
295282
}
296283

297-
#ifdef DEBUG_ESP_HTTP_SERVER
298-
DEBUG_OUTPUT.println("New client");
299-
#endif
284+
log_v("New client");
300285

301286
_currentClient = client;
302287
_currentStatus = HC_WAIT_READ;
@@ -614,17 +599,13 @@ void WebServer::onNotFound(THandlerFunction fn) {
614599
void WebServer::_handleRequest() {
615600
bool handled = false;
616601
if (!_currentHandler){
617-
#ifdef DEBUG_ESP_HTTP_SERVER
618-
DEBUG_OUTPUT.println("request handler not found");
619-
#endif
602+
log_e("request handler not found");
620603
}
621604
else {
622605
handled = _currentHandler->handle(*this, _currentMethod, _currentUri);
623-
#ifdef DEBUG_ESP_HTTP_SERVER
624606
if (!handled) {
625-
DEBUG_OUTPUT.println("request handler failed to handle request");
607+
log_e("request handler failed to handle request");
626608
}
627-
#endif
628609
}
629610
if (!handled && _notFoundHandler) {
630611
_notFoundHandler();

0 commit comments

Comments
 (0)