Switch to ArduinoLog library

This commit is contained in:
fvanroie 2020-03-26 23:21:47 +01:00
parent 2f5d78e47e
commit 36bce5c10f

View File

@ -1,9 +1,13 @@
#include "ArduinoJson.h" #include "ArduinoJson.h"
#include "ArduinoLog.h" #include "ArduinoLog.h"
#include "lvgl.h" #include "lvgl.h"
#include "StringStream.h"
#include "time.h"
#include "hasp_conf.h" #include "hasp_conf.h"
#if defined(ARDUINO_ARCH_ESP8266) #if defined(ARDUINO_ARCH_ESP8266)
#include <sntp.h> // sntp_servermode_dhcp()
#include <ESP8266WiFi.h> #include <ESP8266WiFi.h>
#else #else
#include <Wifi.h> #include <Wifi.h>
@ -40,12 +44,17 @@
#define APP_NAME "HASP" #define APP_NAME "HASP"
#endif #endif
// variables for debug stream writer
// static String debugOutput((char *)0);
// static StringStream debugStream((String &)debugOutput);
extern char mqttNodeName[16];
const char * syslogAppName = APP_NAME; const char * syslogAppName = APP_NAME;
char debugSyslogHost[32] = SYSLOG_SERVER; char debugSyslogHost[32] = SYSLOG_SERVER;
uint16_t debugSyslogPort = SYSLOG_PORT; uint16_t debugSyslogPort = SYSLOG_PORT;
uint8_t debugSyslogFacility = 0; uint8_t debugSyslogFacility = 0;
uint8_t debugSyslogProtocol = 0; uint8_t debugSyslogProtocol = 0;
extern char mqttNodeName[16]; bool debugAnsiCodes = true;
// A UDP instance to let us send and receive packets over UDP // A UDP instance to let us send and receive packets over UDP
WiFiUDP syslogClient; WiFiUDP syslogClient;
@ -103,44 +112,44 @@ void debugStart()
// log/logf method) // log/logf method)
} }
void serialPrintln(const char * debugText, uint8_t level) // void serialPrintln(const char * debugText, uint8_t level)
{ //{
/* /*
String debugTimeText((char *)0); String debugTimeText((char *)0);
debugTimeText.reserve(128); debugTimeText.reserve(128);
uint8_t heapfrag = halGetHeapFragmentation(); uint8_t heapfrag = halGetHeapFragmentation();
debugTimeText = F("["); debugTimeText = F("[");
debugTimeText += String(float(millis()) / 1000, 3); debugTimeText += String(float(millis()) / 1000, 3);
debugTimeText += F("s] "); debugTimeText += F("s] ");
debugTimeText += halGetMaxFreeBlock(); debugTimeText += halGetMaxFreeBlock();
debugTimeText += F("/"); debugTimeText += F("/");
debugTimeText += ESP.getFreeHeap(); debugTimeText += ESP.getFreeHeap();
debugTimeText += F(" "); debugTimeText += F(" ");
if(heapfrag < 10) debugTimeText += F(" "); if(heapfrag < 10) debugTimeText += F(" ");
debugTimeText += heapfrag; debugTimeText += heapfrag;
debugTimeText += F(" "); debugTimeText += F(" ");
#if LV_MEM_CUSTOM == 0 #if LV_MEM_CUSTOM == 0
lv_mem_monitor_t mem_mon; lv_mem_monitor_t mem_mon;
lv_mem_monitor(&mem_mon); lv_mem_monitor(&mem_mon);
debugTimeText += F("| "); debugTimeText += F("| ");
debugTimeText += mem_mon.used_pct; debugTimeText += mem_mon.used_pct;
debugTimeText += F("% "); debugTimeText += F("% ");
debugTimeText += mem_mon.free_biggest_size; debugTimeText += mem_mon.free_biggest_size;
debugTimeText += F("b/"); debugTimeText += F("b/");
debugTimeText += mem_mon.free_size; debugTimeText += mem_mon.free_size;
debugTimeText += F("b "); debugTimeText += F("b ");
debugTimeText += (mem_mon.total_size - mem_mon.free_size); debugTimeText += (mem_mon.total_size - mem_mon.free_size);
debugTimeText += F("b | "); debugTimeText += F("b | ");
#endif #endif
if(debugSerialStarted) { if(debugSerialStarted) {
// Serial.print(debugTimeText); // Serial.print(debugTimeText);
// Serial.println(debugText); // Serial.println(debugText);
}*/ }/
switch(level) { switch(level) {
case LOG_LEVEL_FATAL: case LOG_LEVEL_FATAL:
Log.fatal(debugText); Log.fatal(debugText);
break; break;
@ -158,18 +167,18 @@ void serialPrintln(const char * debugText, uint8_t level)
break; break;
default: default:
Log.notice(debugText); Log.notice(debugText);
} }
#if HASP_USE_TELNET != 0 #if HASP_USE_TELNET != 0
// telnetPrint(debugTimeText.c_str()); // telnetPrint(debugTimeText.c_str());
telnetPrintln(debugText); telnetPrintln(debugText);
#endif #endif
} }
void serialPrintln(String & debugText, uint8_t level) void serialPrintln(String & debugText, uint8_t level)
{ {
serialPrintln(debugText.c_str(), level); serialPrintln(debugText.c_str(), level);
} } */
#if HASP_USE_SYSLOG != 0 #if HASP_USE_SYSLOG != 0
void syslogSend(uint8_t priority, const char * debugText) void syslogSend(uint8_t priority, const char * debugText)
@ -248,12 +257,32 @@ bool debugSetConfig(const JsonObject & settings)
return changed; return changed;
} }
void printTimestamp(int level, Print * _logOutput) static void printTimestamp(int level, Print * _logOutput, String & debugOutput)
{ {
char c[128]; char buffer[128];
/*int m =*/
snprintf(c, sizeof(c), PSTR("[%10.3fs] %5u/%5u %2u | "), float(millis()) / 1000, halGetMaxFreeBlock(), /* Print Current Time */
time_t rawtime;
struct tm * timeinfo;
// if(!time(nullptr)) return;
time(&rawtime);
timeinfo = localtime(&rawtime);
strftime(buffer, sizeof(buffer), ("[%b %d %H:%M:%S."), timeinfo);
if(debugSerialStarted) {
if(debugAnsiCodes) Serial.print(TERM_COLOR_CYAN);
Serial.print(buffer);
}
if(debugAnsiCodes) telnetPrint(TERM_COLOR_CYAN);
telnetPrint(buffer);
/* Print Memory Info */
snprintf(buffer, sizeof(buffer), PSTR("%8.3fs] %5u/%5u %2u | "), float(millis()) / 1000, halGetMaxFreeBlock(),
ESP.getFreeHeap(), halGetHeapFragmentation()); ESP.getFreeHeap(), halGetHeapFragmentation());
if(debugSerialStarted) {
if(debugAnsiCodes) Serial.print(buffer);
}
telnetPrint(buffer);
#if LV_MEM_CUSTOM == 0 #if LV_MEM_CUSTOM == 0
/* lv_mem_monitor_t mem_mon; /* lv_mem_monitor_t mem_mon;
@ -269,38 +298,54 @@ void printTimestamp(int level, Print * _logOutput)
debugTimeText += F("b | ");*/ debugTimeText += F("b | ");*/
#endif #endif
_logOutput->print(TERM_COLOR_CYAN);
_logOutput->print(c);
switch(level) { switch(level) {
case LOG_LEVEL_FATAL: case LOG_LEVEL_FATAL:
case LOG_LEVEL_ERROR: case LOG_LEVEL_ERROR:
_logOutput->print(TERM_COLOR_RED); strcpy(buffer, TERM_COLOR_RED);
break; break;
case LOG_LEVEL_WARNING: case LOG_LEVEL_WARNING:
_logOutput->print(TERM_COLOR_YELLOW); strcpy(buffer, TERM_COLOR_YELLOW);
break; break;
case LOG_LEVEL_NOTICE: case LOG_LEVEL_NOTICE:
_logOutput->print(TERM_COLOR_WHITE); strcpy(buffer, TERM_COLOR_WHITE);
break; break;
case LOG_LEVEL_VERBOSE: case LOG_LEVEL_VERBOSE:
_logOutput->print(TERM_COLOR_CYAN); strcpy(buffer, TERM_COLOR_CYAN);
break; break;
case LOG_LEVEL_TRACE: case LOG_LEVEL_TRACE:
_logOutput->print(TERM_COLOR_GRAY); strcpy(buffer, TERM_COLOR_GRAY);
break; break;
default: default:
_logOutput->print(TERM_COLOR_RESET); strcpy(buffer, TERM_COLOR_RESET);
} }
if(debugSerialStarted) {
if(debugAnsiCodes) Serial.print(buffer);
}
telnetPrint(buffer);
} }
void printNewline(int level, Print * _logOutput) static void printNewline(int level, Print * _logOutput, String & debugOutput)
{ {
_logOutput->print(TERM_COLOR_MAGENTA); if(debugSerialStarted) {
_logOutput->print("\r\n"); Serial.print(debugOutput);
if(debugAnsiCodes) Serial.print(TERM_COLOR_RESET);
Serial.print("\r\n");
if(debugAnsiCodes) Serial.print(TERM_COLOR_MAGENTA);
}
telnetPrint(debugOutput.c_str());
if(debugAnsiCodes) telnetPrint(TERM_COLOR_RESET);
telnetPrint("\r\n");
if(debugAnsiCodes) telnetPrint(TERM_COLOR_MAGENTA);
syslogSend(level, debugOutput.c_str());
} }
void debugPreSetup(JsonObject settings) void debugPreSetup(JsonObject settings)
{ {
// Link stream to debugOutput
// debugOutput.reserve(512);
Log.begin(LOG_LEVEL_VERBOSE, &Serial, true); Log.begin(LOG_LEVEL_VERBOSE, &Serial, true);
Log.setPrefix(printTimestamp); // Uncomment to get timestamps as prefix Log.setPrefix(printTimestamp); // Uncomment to get timestamps as prefix
Log.setSuffix(printNewline); // Uncomment to get newline as suffix Log.setSuffix(printNewline); // Uncomment to get newline as suffix
@ -308,6 +353,7 @@ void debugPreSetup(JsonObject settings)
uint16_t baudrate = settings[FPSTR(F_CONFIG_BAUD)].as<uint16_t>(); uint16_t baudrate = settings[FPSTR(F_CONFIG_BAUD)].as<uint16_t>();
if(baudrate > 0) { if(baudrate > 0) {
Serial.begin(baudrate * 10); /* prepare for possible serial debug */ Serial.begin(baudrate * 10); /* prepare for possible serial debug */
delay(10);
debugSerialStarted = true; debugSerialStarted = true;
} }
@ -318,10 +364,49 @@ void debugPreSetup(JsonObject settings)
void debugLoop() void debugLoop()
{} {}
void printLocalTime()
{
char buffer[128];
time_t rawtime;
struct tm * timeinfo;
// if(!time(nullptr)) return;
time(&rawtime);
timeinfo = localtime(&rawtime);
strftime(buffer, sizeof(buffer), "%b %d %H:%M:%S.", timeinfo);
Serial.println(buffer);
// struct tm timeinfo;
// time_t now = time(nullptr);
// Serial-.print(ctime(&now));
// Serial.print(&timeinfo, " %d %B %Y %H:%M:%S ");
#if LWIP_VERSION_MAJOR > 1
// LwIP v2 is able to list more details about the currently configured SNTP servers
for(int i = 0; i < SNTP_MAX_SERVERS; i++) {
IPAddress sntp = *sntp_getserver(i);
const char * name = sntp_getservername(i);
if(sntp.isSet()) {
Serial.printf("sntp%d: ", i);
if(name) {
Serial.printf("%s (%s) ", name, sntp.toString().c_str());
} else {
Serial.printf("%s ", sntp.toString().c_str());
}
Serial.printf("IPv6: %s Reachability: %o\n", sntp.isV6() ? "Yes" : "No", sntp_getreachability(i));
}
}
#endif
}
void debugEverySecond() void debugEverySecond()
{ {
if(debugTelePeriod > 0 && (millis() - debugLastMillis) >= debugTelePeriod * 1000) { if(debugTelePeriod > 0 && (millis() - debugLastMillis) >= debugTelePeriod * 1000) {
dispatchStatusUpdate(); dispatchStatusUpdate();
debugLastMillis = millis(); debugLastMillis = millis();
} }
// printLocalTime();
} }