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"
3840size_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}
0 commit comments