From 9c6b39fd504643c59b2bdd5e8d17f92bad58a347 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Sat, 3 Jul 2021 20:49:43 +0300 Subject: [PATCH] fixing logging --- fan_control/data/index.html | 23 ++++++++++------------- fan_control/include/serial.hpp | 15 +++++++++++---- fan_control/include/server.hpp | 2 -- fan_control/src/collector.cpp | 2 ++ fan_control/src/server.cpp | 14 +++++++++----- 5 files changed, 32 insertions(+), 24 deletions(-) diff --git a/fan_control/data/index.html b/fan_control/data/index.html index 4ce97bb..7ad1122 100644 --- a/fan_control/data/index.html +++ b/fan_control/data/index.html @@ -23,7 +23,6 @@ var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { - console.log(this.responseText); var series = JSON.parse(this.responseText); var data = { series: series }; chart = new Chartist.Line(clazz, data , {high: 30 , low: -25}); @@ -38,19 +37,17 @@ } var log = document.getElementById("log") - function myCallback() { - function get_log( ){ - var xhttp = new XMLHttpRequest(); - xhttp.onreadystatechange = function() { - if (this.readyState == 4 && this.status == 200) { - console.log(this.responseText); - log.innerHTML += this.responseText; - } + function get_log( ){ + var xhttp = new XMLHttpRequest(); + xhttp.onreadystatechange = function() { + if (this.readyState == 4 && this.status == 200) { + console.log(this.responseText); + log.innerHTML = this.responseText + log.innerHTML; } - xhttp.open("GET", "/log", true); - xhttp.send(); - }; - } + } + xhttp.open("GET", "/log", true); + xhttp.send(); + }; var intervalID = window.setInterval(get_log, 1500); diff --git a/fan_control/include/serial.hpp b/fan_control/include/serial.hpp index e8f66e7..a94d487 100644 --- a/fan_control/include/serial.hpp +++ b/fan_control/include/serial.hpp @@ -5,15 +5,22 @@ class WsSerial{ std::queue debug; public: - inline void print(const String& line){ debug.push(line); } - inline void println(const String& line){ debug.push(line); } + inline void print(const String& line){ + // enable debug the debugging Serial.println(line.c_str()); + debug.push(line); + } + inline void println(const String& line){ + // enable debug the debugging Serial.println(line.c_str()); + debug.push(line); + } inline void println(const float num){ println(String(num)) ; }; - inline const String& pop(){ const String& first = debug.front(); debug.pop(); return first;}; + inline const String& first(){ return debug.front();}; + inline void pop(){ debug.pop(); }; inline bool empty(){ return debug.empty();}; }; -static WsSerial debug_out; +extern WsSerial debug_out; #if(false) #define DEBUG_OUT Serial diff --git a/fan_control/include/server.hpp b/fan_control/include/server.hpp index 5f6b52e..8bb09cf 100644 --- a/fan_control/include/server.hpp +++ b/fan_control/include/server.hpp @@ -1,10 +1,8 @@ #include #include -#include #include static AsyncWebServer server(80); -static AsyncEventSource events("/events"); void server_setup(); void server_loop(); diff --git a/fan_control/src/collector.cpp b/fan_control/src/collector.cpp index 2a87be7..c555111 100644 --- a/fan_control/src/collector.cpp +++ b/fan_control/src/collector.cpp @@ -5,6 +5,8 @@ #include #include +WsSerial debug_out; + // DHT Temperature & Humidity Sensor // Unified Sensor Library Example // Written by Tony DiCola for Adafruit Industries diff --git a/fan_control/src/server.cpp b/fan_control/src/server.cpp index 9db7a38..0d2119e 100644 --- a/fan_control/src/server.cpp +++ b/fan_control/src/server.cpp @@ -83,9 +83,15 @@ void server_setup(){ DEBUG_OUT.println("log start"); AsyncWebServerResponse *response = request->beginChunkedResponse("text/html", [](uint8_t *buffer, size_t maxLen, size_t index) -> size_t { if(debug_out.empty()) return 0; - String to_send = debug_out.pop(); - to_send += "
"; - to_send.getBytes(buffer, maxLen); + String to_send = String(maxLen); + while(!debug_out.empty()){ + String add_next = debug_out.first(); + if( (add_next.length() + 5 + to_send.length() ) > maxLen ) break ; // dont overflow buffer + to_send.concat(add_next); + to_send.concat("
"); + debug_out.pop(); + } + to_send.getBytes(buffer, maxLen); return to_send.length(); }); request->send(response); @@ -116,8 +122,6 @@ void server_setup(){ server.onNotFound(notFound); - server.addHandler(&events); - server.begin(); }