Skip to content

Commit b88adca

Browse files
committed
logger: setLogger
1 parent fb4455c commit b88adca

File tree

7 files changed

+45
-3
lines changed

7 files changed

+45
-3
lines changed

examples/AsyncCam/AsyncCam.ino

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ void
1313
setup() {
1414
Serial.begin(115200);
1515
Serial.println();
16+
esp32cam::setLogger(Serial);
1617
delay(1000);
1718

1819
WiFi.persistent(false);
@@ -56,7 +57,7 @@ setup() {
5657

5758
void
5859
loop() {
59-
// esp32cam-asyncweb.h depends on FreeRTOS task API including vTaskDelete, so you must have a
60+
// esp32cam/asyncweb.hpp depends on FreeRTOS task API including vTaskDelete, so you must have a
6061
// non-zero delay in the loop() function; otherwise, FreeRTOS kernel memory cannot be freed
6162
// properly and the system would run out of memory.
6263
delay(1);

examples/AsyncCam/handlers.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ addRequestHandlers() {
9191
});
9292

9393
server.on("/robots.txt", HTTP_GET, [](AsyncWebServerRequest* request) {
94-
request->send(200, "text/html", "User-Agent: *\nDisallow: /\n");
94+
request->send(200, "text/plain", "User-Agent: *\nDisallow: /\n");
9595
});
9696

9797
server.on("/change-resolution.cgi", HTTP_POST, [](AsyncWebServerRequest* request) {

src/esp32cam.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include "esp32cam/asyncweb.hpp"
1111
#include "esp32cam/camera.hpp"
12+
#include "esp32cam/logger.hpp"
1213

1314
namespace esp32cam {
1415

src/esp32cam/asyncweb.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
#if __has_include(<ESPAsyncWebServer.h>)
22

33
#include "asyncweb.hpp"
4+
#include "logger.hpp"
45
#include <freertos/FreeRTOS.h>
56
#include <freertos/idf_additions.h>
67

7-
#define MJPEG_LOG(fmt, ...) Serial.printf("MjpegResponse(%p) " fmt "\n", this, ##__VA_ARGS__)
8+
#define STILL_LOG(fmt, ...) ESP32CAM_LOG("StillResponse(%p) " fmt, this, ##__VA_ARGS__)
9+
#define MJPEG_LOG(fmt, ...) ESP32CAM_LOG("MjpegResponse(%p) " fmt, this, ##__VA_ARGS__)
810

911
namespace esp32cam {
1012
namespace detail {
@@ -79,6 +81,7 @@ namespace asyncweb {
7981

8082
StillResponse::StillResponse()
8183
: m_task(1) {
84+
STILL_LOG("created");
8285
if (!m_task) {
8386
_code = 500;
8487
return;
@@ -90,15 +93,21 @@ StillResponse::StillResponse()
9093
m_task.request();
9194
}
9295

96+
StillResponse::~StillResponse() {
97+
STILL_LOG("deleted");
98+
}
99+
93100
size_t
94101
StillResponse::_fillBuffer(uint8_t* buf, size_t buflen) {
95102
if (!m_frame) {
96103
if (m_frame = m_task.retrieve(); !m_frame) {
97104
return RESPONSE_TRY_AGAIN;
98105
}
106+
STILL_LOG("frame has %zu octets", m_frame->size());
99107
}
100108

101109
if (m_index >= m_frame->size()) {
110+
STILL_LOG("sent to client");
102111
return 0;
103112
}
104113

src/esp32cam/asyncweb.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ class StillResponse : public AsyncAbstractResponse {
5252
public:
5353
explicit StillResponse();
5454

55+
~StillResponse() override;
56+
5557
bool _sourceValid() const override {
5658
return true;
5759
}

src/esp32cam/camera.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
namespace esp32cam {
77

8+
Print* LogOutput = nullptr;
89
CameraClass Camera;
910

1011
bool

src/esp32cam/logger.hpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#ifndef ESP32CAM_LOGGER_HPP
2+
#define ESP32CAM_LOGGER_HPP
3+
4+
#include <Print.h>
5+
6+
namespace esp32cam {
7+
8+
extern Print* LogOutput;
9+
10+
/**
11+
* @brief Set logger output.
12+
* @param output Output printer, such as @c Serial .
13+
*/
14+
inline void
15+
setLogger(Print& output) {
16+
LogOutput = &output;
17+
}
18+
19+
#define ESP32CAM_LOG(fmt, ...) \
20+
do { \
21+
if (LogOutput != nullptr) { \
22+
LogOutput->printf("[%8lu] " fmt "\n", millis(), ##__VA_ARGS__); \
23+
} \
24+
} while (false)
25+
26+
} // namespace esp32cam
27+
28+
#endif // ESP32CAM_LOGGER_HPP

0 commit comments

Comments
 (0)