some formating, add variadic templ for logf()

This commit is contained in:
Simon Schmidt 2021-07-10 04:12:50 +02:00
parent 6e810fad59
commit 2820869e14
4 changed files with 91 additions and 100 deletions

View File

@ -1,4 +1,5 @@
#pragma once
#ifndef __OTA_H
#define __OTA_H
#include <ArduinoOTA.h>
#include <ESP8266WiFi.h>
@ -46,3 +47,5 @@ void ota_setup() {
ArduinoOTA.begin();
}
#endif // __OTA_H

View File

@ -2,7 +2,7 @@
FSInfo fsinfo;
bool mount_fs() {
auto mount_fs() -> bool {
if(!LittleFS.begin()) {
Serial.println("[ERROR] LittleFS.info(), reset ...");
return false;
@ -25,7 +25,7 @@ bool mount_fs() {
return true;
}
bool format_fs() {
auto format_fs() -> bool {
printf("Formatting FS ! \n\r");
return LittleFS.format();
}

View File

@ -1,11 +1,11 @@
#include "httpserver.h"
bool HTTPServer::start() {
auto HTTPServer::start() -> bool {
if (!mount_fs()) {
logf("cant mount filesystem, EXIT !\n\r");
return false;
}
logf("LittleFS mounted !\n\r");
logf("root:\n\r");
this->listRoot();
@ -35,27 +35,6 @@ bool HTTPServer::start() {
return true;
}
void HTTPServer::logf(const char *format, ...) {
va_list args;
va_start(args, format);
char temp[64];
size_t len = vsnprintf(temp, sizeof(temp), format, args);
Serial.print(log_prefix);
Serial.write(temp, len);
va_end(args);
}
void HTTPServer::logt(const char *format, ...) {
va_list args;
va_start(args, format);
// append logging string to local buffer
if(tbuf_head < TBUF_LEN) {
size_t len = vsnprintf(tbuf+tbuf_head, TBUF_LEN-tbuf_head, format, args);
tbuf_head += len;
}
va_end(args);
}
void HTTPServer::start_apps() {
// application handler
@ -76,10 +55,25 @@ void HTTPServer::start_apps() {
if (tbuf_head) {
send(200, "text/plain", tbuf);
tbuf_head = 0;
}
else {
} else {
send(202, "text/plain", "");
}
});
}
template <class... Args>
void HTTPServer::logf(const char *format, Args... args) const {
Serial.print(log_prefix);
Serial.printf(format, args...);
}
void HTTPServer::logt(const char *format, ...) {
va_list args;
va_start(args, format);
// append logging string to local buffer
if (tbuf_head < TBUF_LEN) {
tbuf_head +=
vsnprintf(tbuf + tbuf_head, TBUF_LEN - tbuf_head, format, args);
}
va_end(args);
}

View File

@ -2,17 +2,16 @@
#define __HTTPSERVER_H
// Wrapper for ESP8266WebServer with Filesystem as HTTP source
#include <ESP8266WebServer.h>
#include <stdarg.h>
#include "filesys.h"
#include "treppe.h"
#include <ESP8266WebServer.h>
#include <stdarg.h>
// debug log <ESP8266WebServer.h>
// #define DEBUGV(f,...) do { Serial.printf(PSTR(f), ##__VA_ARGS__); } while (0)
#define TBUF_LEN 256
class HTTPServer : public ESP8266WebServer {
private:
@ -22,24 +21,19 @@ private:
size_t tbuf_head = 0;
char tbuf[TBUF_LEN];
void listRoot() {
ls(rootDir);
}
void listRoot() { ls(rootDir); }
Treppe *treppe;
public:
HTTPServer(const int _port, const char* _rootDir, Treppe* _treppe) :
ESP8266WebServer(_port), rootDir(_rootDir), treppe(_treppe)
{ }
~HTTPServer()
{
Serial.printf("[HTTPServer] shut down ...\n\r");
}
HTTPServer(const int _port, const char *_rootDir, Treppe *_treppe)
: ESP8266WebServer(_port), rootDir(_rootDir), treppe(_treppe) {}
~HTTPServer() { Serial.printf("[HTTPServer] shut down ...\n\r"); }
bool start();
void start_apps();
void logf(const char *format, ...);
template <class... Args>
void logf(const char *format, Args... args) const;
void logt(const char *format, ...);
};