Skip to content

Commit 728e40b

Browse files
committed
Runtime setting of logback lines
1 parent e52578b commit 728e40b

4 files changed

Lines changed: 18 additions & 17 deletions

File tree

src/asyncbufferedtcplogger.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
#include "asyncbufferedtcplogger.h"
44
#include "configuration.h"
55

6-
APB::AsyncBufferedTCPLogger::AsyncBufferedTCPLogger(uint16_t port) : loggerServer{port} {
6+
APB::AsyncBufferedTCPLogger::AsyncBufferedTCPLogger(uint16_t port, uint16_t backlog_lines) : loggerServer{port}, backlog_lines{backlog_lines} {
77
loggerServer.onClient([this](void *,AsyncClient *c){
88
this->client = c;
9+
c->onDisconnect([this](void *,AsyncClient *){
10+
this->client = nullptr;
11+
}, nullptr);
912
if(!this->backlog.empty()) {
1013
c->write("==== Flushing backlog ====\n");
1114
while(!this->backlog.empty()) {
@@ -34,16 +37,18 @@ void APB::AsyncBufferedTCPLogger::setup() {
3437
});
3538
}
3639

37-
#include "utils.h"
3840
size_t APB::AsyncBufferedTCPLogger::write(uint8_t c) {
3941
buffer[currentPosition++] = c;
4042
if(c == '\n') {
41-
ScopeGuard resetBuffer(std::bind(&AsyncBufferedTCPLogger::reset, this));
4243
if(!client) {
43-
return fillBacklog();
44+
fillBacklog();
45+
reset();
46+
return 0;
4447
}
4548
client->write(buffer.data(), currentPosition);
49+
reset();
4650
}
51+
4752
return 1;
4853
}
4954

@@ -52,12 +57,11 @@ void APB::AsyncBufferedTCPLogger::reset() {
5257
currentPosition = 0;
5358
}
5459

55-
size_t APB::AsyncBufferedTCPLogger::fillBacklog() {
56-
#if APB_NETWORK_LOGGER_BACKLOG > 0
60+
void APB::AsyncBufferedTCPLogger::fillBacklog() {
61+
if (backlog_lines > 0) {
5762
backlog.push(String{buffer.data()});
58-
while(backlog.size() > APB_NETWORK_LOGGER_BACKLOG) {
63+
while(backlog.size() > backlog_lines) {
5964
backlog.pop();
6065
}
61-
#endif
62-
return 0;
66+
}
6367
}

src/asyncbufferedtcplogger.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,19 @@
77
namespace APB {
88
class AsyncBufferedTCPLogger: public Print {
99
public:
10-
AsyncBufferedTCPLogger(uint16_t port);
10+
AsyncBufferedTCPLogger(uint16_t port, uint16_t backlog_lines = 0);
1111
void setup();
12-
12+
void setBacklogLines(uint16_t backlog_lines) { this->backlog_lines = backlog_lines; };
1313
virtual size_t write(uint8_t c);
1414
private:
1515
AsyncClient *client = nullptr;
1616
void reset();
17-
size_t fillBacklog();
17+
void fillBacklog();
1818
std::array<char, 1024> buffer = {0};
1919
std::queue<String> backlog;
2020
uint16_t currentPosition = 0;
2121
AsyncServer loggerServer;
22+
uint16_t backlog_lines;
2223
};
2324

2425
}

src/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
Scheduler scheduler;
2424

2525

26-
APB::AsyncBufferedTCPLogger bufferedLogger{9911};
26+
APB::AsyncBufferedTCPLogger bufferedLogger{9911, APB_NETWORK_LOGGER_BACKLOG };
2727

2828
#ifdef ONEBUTTON_USER_BUTTON_1
2929
OneButton userButton;

src/wifimanager.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@
22
#include <ArduinoLog.h>
33
#include <WiFi.h>
44

5-
#include "time.h"
6-
#include "esp_sntp.h"
7-
8-
95
#define LOG_SCOPE "APB::WiFiManager:"
106

117
using namespace std::placeholders;

0 commit comments

Comments
 (0)