mirror of
https://github.com/HASwitchPlate/openHASP.git
synced 2025-07-24 11:46:34 +00:00
Xplatform debug
This commit is contained in:
parent
f383ecc15e
commit
4d6f59bb19
@ -31,7 +31,7 @@ void Win32Device::show_info()
|
||||
unsigned int eax, ebx, ecx, edx;
|
||||
eax = 0;
|
||||
native_cpuid(&eax, &ebx, &ecx, &edx);
|
||||
printf("EAX: %08X EBX: %08X ECX: %08X EDX: %08X\n", eax, ebx, ecx, edx);
|
||||
// printf("EAX: %08X EBX: %08X ECX: %08X EDX: %08X\n", eax, ebx, ecx, edx);
|
||||
char vendor[13];
|
||||
memcpy(vendor, &ebx, 4);
|
||||
memcpy(vendor + 4, &edx, 4);
|
||||
|
@ -65,8 +65,8 @@ void TftSdl::show_info()
|
||||
{
|
||||
SDL_version linked;
|
||||
SDL_GetVersion(&linked);
|
||||
LOG_VERBOSE(TAG_TFT, F("SDL2 : v%d.%d.%d"), linked.major, linked.minor, linked.patch);
|
||||
LOG_VERBOSE(TAG_TFT, F("Driver : SDL2"));
|
||||
LOG_VERBOSE(TAG_TFT, F("SDL Version: v%d.%d.%d"), linked.major, linked.minor, linked.patch);
|
||||
}
|
||||
|
||||
void TftSdl::splashscreen()
|
||||
|
407
src/hasp_debug.cpp
Normal file
407
src/hasp_debug.cpp
Normal file
@ -0,0 +1,407 @@
|
||||
/* MIT License - Copyright (c) 2019-2021 Francis Van Roie
|
||||
For full license information read the LICENSE file in the project folder */
|
||||
|
||||
#include "ArduinoJson.h"
|
||||
#include "lvgl.h"
|
||||
#include <sys/time.h>
|
||||
|
||||
#include "lang/lang.h"
|
||||
#include "hasp_conf.h"
|
||||
#include "hasp_debug.h"
|
||||
#include "hasp_macro.h"
|
||||
#include "hasp/hasp.h"
|
||||
|
||||
#if(!defined(WINDOWS)) && (!defined(POSIX))
|
||||
#include "ArduinoLog.h"
|
||||
|
||||
#define debug_print(io, ...) io->print(__VA_ARGS__)
|
||||
#define debug_newline(io) io->println()
|
||||
|
||||
bool debugSerialStarted = false;
|
||||
#else
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <iostream>
|
||||
|
||||
#define debug_print(io, ...) fprintf(stdout, __VA_ARGS__)
|
||||
#define debug_newline(io) fprintf(stdout, "\n")
|
||||
|
||||
bool debugSerialStarted = true;
|
||||
#endif
|
||||
|
||||
bool debugAnsiCodes = true;
|
||||
|
||||
inline void debugSendAnsiCode(const __FlashStringHelper* code, Print* _logOutput)
|
||||
{
|
||||
if(debugAnsiCodes) debug_print(_logOutput, code);
|
||||
}
|
||||
|
||||
void debug_timestamp()
|
||||
{
|
||||
timeval curTime;
|
||||
gettimeofday(&curTime, NULL);
|
||||
int milli = curTime.tv_usec / 1000;
|
||||
|
||||
char currentTime[80];
|
||||
time_t t = curTime.tv_sec;
|
||||
// strftime(currentTime, 80, "%Y-%m-%d %H:%M.%S", localtime(&t));
|
||||
strftime(currentTime, 80, "%H:%M:%S", localtime(&t));
|
||||
printf("[%s.%03d] ", currentTime, milli);
|
||||
}
|
||||
|
||||
static void debugPrintTimestamp(int level, Print* _logOutput)
|
||||
{ /* Print Current Time */
|
||||
|
||||
timeval curTime;
|
||||
int rslt = gettimeofday(&curTime, NULL);
|
||||
time_t t = curTime.tv_sec;
|
||||
tm* timeinfo = localtime(&t);
|
||||
int milli = curTime.tv_usec / 1000;
|
||||
|
||||
debugSendAnsiCode(F(TERM_COLOR_CYAN), _logOutput);
|
||||
|
||||
if(timeinfo->tm_year >= 120) {
|
||||
char buffer[24];
|
||||
strftime(buffer, sizeof(buffer), "[%b %d %H:%M:%S", timeinfo); // Literal String
|
||||
// strftime(buffer, sizeof(buffer), "[%H:%M:%S.", timeinfo); // Literal String
|
||||
debug_print(_logOutput, PSTR("%s.%03lu]"), buffer, curTime.tv_usec / 1000);
|
||||
} else {
|
||||
uint32_t msecs = millis();
|
||||
debug_print(_logOutput, PSTR("[%15d.%03d]"), msecs / 1000, msecs % 1000);
|
||||
}
|
||||
}
|
||||
|
||||
/* ===== Default Event Processors ===== */
|
||||
// void debugPreSetup(JsonObject settings);
|
||||
// void debugSetup();
|
||||
|
||||
static inline void debug_flush()
|
||||
{
|
||||
#if defined(ARDUINO)
|
||||
Serial.flush();
|
||||
#endif
|
||||
|
||||
#if defined(WINDOWS) || defined(POSIX)
|
||||
fflush(stdout);
|
||||
#endif
|
||||
}
|
||||
|
||||
void debugEverySecond()
|
||||
{
|
||||
// if(debugTelePeriod > 0 && (millis() - debugLastMillis) >= debugTelePeriod * 1000) {
|
||||
// dispatch_output_statusupdate(NULL, NULL);
|
||||
// debugLastMillis = millis();
|
||||
// }
|
||||
// printLocalTime();
|
||||
}
|
||||
|
||||
void debugStart()
|
||||
{
|
||||
|
||||
#if defined(WINDOWS) || defined(POSIX)
|
||||
debug_newline();
|
||||
debugPrintHaspHeader(NULL);
|
||||
debug_newline();
|
||||
|
||||
LOG_INFO(TAG_DEBG, F("Console started"));
|
||||
LOG_INFO(TAG_DEBG, F("Environment: " PIOENV));
|
||||
#endif
|
||||
|
||||
if(debugSerialStarted) {
|
||||
debug_flush;
|
||||
|
||||
// Serial.println();
|
||||
// Serial.println(debugHaspHeader());
|
||||
// debug_flush();
|
||||
}
|
||||
|
||||
// prepare syslog configuration here (can be anywhere before first call of
|
||||
// log/logf method)
|
||||
}
|
||||
|
||||
void debugStop()
|
||||
{
|
||||
if(debugSerialStarted) debug_flush();
|
||||
}
|
||||
|
||||
/* ===== Special Event Processors ===== */
|
||||
|
||||
void debugLvglLogEvent(lv_log_level_t level, const char* file, uint32_t line, const char* funcname, const char* descr)
|
||||
{
|
||||
#if LV_USE_LOG != 0
|
||||
/* used for duplicate detection */
|
||||
static uint32_t lastDbgLine;
|
||||
static uint32_t lastDbgFreeMem;
|
||||
|
||||
lv_mem_monitor_t mem_mon;
|
||||
lv_mem_monitor(&mem_mon);
|
||||
|
||||
/* Reduce the number of repeated debug message */
|
||||
if(line != lastDbgLine || mem_mon.free_biggest_size != lastDbgFreeMem) {
|
||||
switch(level) {
|
||||
case LV_LOG_LEVEL_TRACE:
|
||||
LOG_VERBOSE(TAG_LVGL, descr);
|
||||
break;
|
||||
case LV_LOG_LEVEL_WARN:
|
||||
LOG_WARNING(TAG_LVGL, descr);
|
||||
break;
|
||||
case LV_LOG_LEVEL_ERROR:
|
||||
LOG_ERROR(TAG_LVGL, descr);
|
||||
break;
|
||||
default:
|
||||
LOG_TRACE(TAG_LVGL, descr);
|
||||
}
|
||||
lastDbgLine = line;
|
||||
lastDbgFreeMem = mem_mon.free_biggest_size;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
// Send the HASP header and version to the output device specified
|
||||
void debugPrintHaspHeader(Print* output)
|
||||
{
|
||||
// if(debugAnsiCodes) debug_print(output,TERM_COLOR_YELLOW);
|
||||
debug_newline(output);
|
||||
debug_print(output, F(""
|
||||
" _____ _____ _____ _____\r\n"
|
||||
" | | | _ | __| _ |\r\n"
|
||||
" | | |__ | __|\r\n"
|
||||
" |__|__|__|__|_____|__|\r\n"
|
||||
" Home Automation Switch Plate\r\n"
|
||||
" Open Hardware edition v"));
|
||||
char buffer[32];
|
||||
haspGetVersion(buffer, sizeof(buffer));
|
||||
debug_print(output, buffer);
|
||||
debug_newline(output);
|
||||
}
|
||||
|
||||
void debug_get_tag(uint8_t tag, char* buffer)
|
||||
{
|
||||
switch(tag) {
|
||||
case TAG_MAIN:
|
||||
memcpy_P(buffer, PSTR("MAIN"), 5);
|
||||
break;
|
||||
|
||||
case TAG_HASP:
|
||||
memcpy_P(buffer, PSTR("HASP"), 5);
|
||||
break;
|
||||
|
||||
case TAG_DRVR:
|
||||
memcpy_P(buffer, PSTR("DRVR"), 5);
|
||||
break;
|
||||
|
||||
case TAG_ATTR:
|
||||
memcpy_P(buffer, PSTR("ATTR"), 5);
|
||||
break;
|
||||
|
||||
case TAG_MSGR:
|
||||
memcpy_P(buffer, PSTR("MSGR"), 5);
|
||||
break;
|
||||
|
||||
case TAG_OOBE:
|
||||
memcpy_P(buffer, PSTR("OOBE"), 5);
|
||||
break;
|
||||
case TAG_HAL:
|
||||
memcpy_P(buffer, PSTR("HAL "), 5);
|
||||
break;
|
||||
|
||||
case TAG_DEBG:
|
||||
memcpy_P(buffer, PSTR("DBUG"), 5);
|
||||
break;
|
||||
case TAG_TELN:
|
||||
memcpy_P(buffer, PSTR("TELN"), 5);
|
||||
break;
|
||||
case TAG_SYSL:
|
||||
memcpy_P(buffer, PSTR("SYSL"), 5);
|
||||
break;
|
||||
case TAG_TASM:
|
||||
memcpy_P(buffer, PSTR("TASM"), 5);
|
||||
break;
|
||||
|
||||
case TAG_CONF:
|
||||
memcpy_P(buffer, PSTR("CONF"), 5);
|
||||
break;
|
||||
case TAG_GUI:
|
||||
memcpy_P(buffer, PSTR("GUI "), 5);
|
||||
break;
|
||||
case TAG_TFT:
|
||||
memcpy_P(buffer, PSTR("TFT "), 5);
|
||||
break;
|
||||
|
||||
case TAG_EPRM:
|
||||
memcpy_P(buffer, PSTR("EPRM"), 5);
|
||||
break;
|
||||
case TAG_FILE:
|
||||
memcpy_P(buffer, PSTR("FILE"), 5);
|
||||
break;
|
||||
case TAG_GPIO:
|
||||
memcpy_P(buffer, PSTR("GPIO"), 5);
|
||||
break;
|
||||
|
||||
case TAG_ETH:
|
||||
memcpy_P(buffer, PSTR("ETH "), 5);
|
||||
break;
|
||||
case TAG_WIFI:
|
||||
memcpy_P(buffer, PSTR("WIFI"), 5);
|
||||
break;
|
||||
case TAG_HTTP:
|
||||
memcpy_P(buffer, PSTR("HTTP"), 5);
|
||||
break;
|
||||
case TAG_MDNS:
|
||||
memcpy_P(buffer, PSTR("MDNS"), 5);
|
||||
break;
|
||||
case TAG_MQTT:
|
||||
memcpy_P(buffer, PSTR("MQTT"), 5);
|
||||
break;
|
||||
case TAG_MQTT_PUB:
|
||||
memcpy_P(buffer, PSTR("MQTT PUB"), 9);
|
||||
break;
|
||||
case TAG_MQTT_RCV:
|
||||
memcpy_P(buffer, PSTR("MQTT RCV"), 9);
|
||||
break;
|
||||
|
||||
case TAG_OTA:
|
||||
memcpy_P(buffer, PSTR("OTA "), 5);
|
||||
break;
|
||||
case TAG_FWUP:
|
||||
memcpy_P(buffer, PSTR("FWUP"), 5);
|
||||
break;
|
||||
|
||||
case TAG_LVGL:
|
||||
memcpy_P(buffer, PSTR("LVGL"), 5);
|
||||
break;
|
||||
case TAG_LVFS:
|
||||
memcpy_P(buffer, PSTR("LVFS"), 5);
|
||||
break;
|
||||
case TAG_FONT:
|
||||
memcpy_P(buffer, PSTR("FONT"), 5);
|
||||
break;
|
||||
|
||||
default:
|
||||
memcpy_P(buffer, PSTR("----"), 5);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static void debugPrintHaspMemory(int level, Print* _logOutput)
|
||||
{
|
||||
size_t maxfree = haspDevice.get_free_max_block();
|
||||
size_t totalfree = haspDevice.get_free_heap();
|
||||
uint8_t frag = haspDevice.get_heap_fragmentation();
|
||||
|
||||
/* Print HASP Memory Info */
|
||||
if(debugAnsiCodes) {
|
||||
if(maxfree > (1024u * 5) && (totalfree > 1024u * 6) && (frag <= 10))
|
||||
debugSendAnsiCode(F(TERM_COLOR_GREEN), _logOutput);
|
||||
else if(maxfree > (1024u * 3) && (totalfree > 1024u * 5) && (frag <= 20))
|
||||
debugSendAnsiCode(F(TERM_COLOR_ORANGE), _logOutput);
|
||||
else
|
||||
debugSendAnsiCode(F(TERM_COLOR_RED), _logOutput);
|
||||
}
|
||||
debug_print(_logOutput, PSTR("[%5u/%5u%3u]"), maxfree, totalfree, frag);
|
||||
}
|
||||
|
||||
static void debugPrintLvglMemory(int level, Print* _logOutput)
|
||||
{
|
||||
#if LV_MEM_CUSTOM == 0
|
||||
lv_mem_monitor_t mem_mon;
|
||||
lv_mem_monitor(&mem_mon);
|
||||
|
||||
/* Print LVGL Memory Info */
|
||||
if(debugAnsiCodes) {
|
||||
if(mem_mon.free_biggest_size > (1024u * 2) && (mem_mon.free_size > 1024u * 2.5) && (mem_mon.frag_pct <= 10))
|
||||
debugSendAnsiCode(F(TERM_COLOR_GREEN), _logOutput);
|
||||
else if(mem_mon.free_biggest_size > (1024u * 1) && (mem_mon.free_size > 1024u * 1.5) &&
|
||||
(mem_mon.frag_pct <= 25))
|
||||
debugSendAnsiCode(F(TERM_COLOR_ORANGE), _logOutput);
|
||||
else
|
||||
debugSendAnsiCode(F(TERM_COLOR_RED), _logOutput);
|
||||
}
|
||||
debug_print(_logOutput, PSTR("[%5u/%5u%3u]"), mem_mon.free_biggest_size, mem_mon.free_size, mem_mon.frag_pct);
|
||||
#endif
|
||||
}
|
||||
|
||||
static void debugPrintPriority(int level, Print* _logOutput)
|
||||
{
|
||||
// if(_logOutput == &syslogClient) {
|
||||
// }
|
||||
|
||||
switch(level) {
|
||||
case LOG_LEVEL_FATAL ... LOG_LEVEL_ERROR:
|
||||
debugSendAnsiCode(F(TERM_COLOR_RED), _logOutput);
|
||||
break;
|
||||
case LOG_LEVEL_WARNING:
|
||||
debugSendAnsiCode(F(TERM_COLOR_YELLOW), _logOutput);
|
||||
break;
|
||||
case LOG_LEVEL_NOTICE:
|
||||
debugSendAnsiCode(F(TERM_COLOR_WHITE), _logOutput);
|
||||
break;
|
||||
case LOG_LEVEL_TRACE:
|
||||
debugSendAnsiCode(F(TERM_COLOR_GRAY), _logOutput);
|
||||
break;
|
||||
case LOG_LEVEL_VERBOSE:
|
||||
debugSendAnsiCode(F(TERM_COLOR_CYAN), _logOutput);
|
||||
break;
|
||||
case LOG_LEVEL_DEBUG:
|
||||
debugSendAnsiCode(F(TERM_COLOR_BLUE), _logOutput);
|
||||
break;
|
||||
case LOG_LEVEL_OUTPUT:
|
||||
default:
|
||||
debugSendAnsiCode(F(TERM_COLOR_RESET), _logOutput);
|
||||
}
|
||||
}
|
||||
|
||||
void debugPrintPrefix(uint8_t tag, int level, Print* _logOutput)
|
||||
{
|
||||
char buffer[10];
|
||||
|
||||
#if HASP_USE_SYSLOG > 0
|
||||
|
||||
if(_logOutput == syslogClient && syslogClient) {
|
||||
if(syslogClient->beginPacket(debugSyslogHost, debugSyslogPort)) {
|
||||
|
||||
// IETF Doc: https://tools.ietf.org/html/rfc5424 - The Syslog Protocol
|
||||
// BSD Doc: https://tools.ietf.org/html/rfc3164 - The BSD syslog Protocol
|
||||
|
||||
syslogClient->print(F("<"));
|
||||
syslogClient->print((16 + debugSyslogFacility) * 8 + level);
|
||||
syslogClient->print(F(">"));
|
||||
|
||||
if(debugSyslogProtocol == SYSLOG_PROTO_IETF) {
|
||||
syslogClient->print(F("1 - "));
|
||||
}
|
||||
|
||||
debug_get_tag(tag, buffer);
|
||||
syslogClient->print(F("%s %s"), haspDevice.get_hostname(), buffer);
|
||||
|
||||
if(debugSyslogProtocol == SYSLOG_PROTO_IETF) {
|
||||
syslogClient->print(F(" - - - \xEF\xBB\xBF")); // include UTF-8 BOM
|
||||
} else {
|
||||
syslogClient->print(F(": "));
|
||||
}
|
||||
|
||||
debugPrintHaspMemory(level, _logOutput);
|
||||
#if LV_MEM_CUSTOM == 0
|
||||
debugPrintLvglMemory(level, _logOutput);
|
||||
#endif
|
||||
}
|
||||
return;
|
||||
}
|
||||
#endif // HASP_USE_SYSLOG
|
||||
|
||||
debugSendAnsiCode(F(TERM_CLEAR_LINE), _logOutput);
|
||||
debugPrintTimestamp(level, _logOutput);
|
||||
debugPrintHaspMemory(level, _logOutput);
|
||||
debugPrintLvglMemory(level, _logOutput);
|
||||
|
||||
if(tag == TAG_MQTT_PUB && level == LOG_LEVEL_NOTICE) {
|
||||
debugSendAnsiCode(F(TERM_COLOR_GREEN), _logOutput);
|
||||
} else if(tag == TAG_MQTT_RCV && level == LOG_LEVEL_NOTICE) {
|
||||
debugSendAnsiCode(F(TERM_COLOR_ORANGE), _logOutput);
|
||||
} else {
|
||||
debugPrintPriority(level, _logOutput);
|
||||
}
|
||||
|
||||
debug_get_tag(tag, buffer);
|
||||
debug_print(_logOutput, F(" %s: "), buffer);
|
||||
}
|
@ -10,55 +10,77 @@
|
||||
|
||||
#include "lang/lang.h"
|
||||
|
||||
#if (!defined(WINDOWS)) && (!defined(POSIX))
|
||||
#if(!defined(WINDOWS)) && (!defined(POSIX))
|
||||
#include "ArduinoLog.h"
|
||||
|
||||
/* ===== Default Event Processors ===== */
|
||||
void debugPreSetup(JsonObject settings);
|
||||
void debugSetup();
|
||||
void debugLoop(void);
|
||||
void debugEverySecond(void);
|
||||
void debugStart(void);
|
||||
void debugStop(void);
|
||||
|
||||
/* ===== Special Event Processors ===== */
|
||||
void debugLvglLogEvent(lv_log_level_t level, const char* file, uint32_t line, const char* funcname, const char* descr);
|
||||
void debugPrintHaspHeader(Print* output);
|
||||
|
||||
void debugStartSyslog(void);
|
||||
void debugStopSyslog(void);
|
||||
// void syslogSend(uint8_t log, const char * debugText);
|
||||
|
||||
#else
|
||||
|
||||
#define Print void
|
||||
|
||||
#include <iostream>
|
||||
#include <sys/time.h>
|
||||
|
||||
#define LOG_LEVEL_SILENT -1
|
||||
|
||||
#define LOG_LEVEL_FATAL 0
|
||||
#define LOG_LEVEL_ALERT 1
|
||||
#define LOG_LEVEL_CRITICAL 2
|
||||
#define LOG_LEVEL_ERROR 3
|
||||
#define LOG_LEVEL_WARNING 4
|
||||
#define LOG_LEVEL_NOTICE 5
|
||||
#define LOG_LEVEL_INFO 5
|
||||
#define LOG_LEVEL_TRACE 6
|
||||
#define LOG_LEVEL_VERBOSE 7
|
||||
#define LOG_LEVEL_DEBUG 8
|
||||
#define LOG_LEVEL_OUTPUT 9
|
||||
|
||||
#define LOG_FATAL(x, ...) \
|
||||
debugPrintPrefix(x, LOG_LEVEL_FATAL, NULL); \
|
||||
printf(__VA_ARGS__); \
|
||||
std::cout << std::endl; \
|
||||
fflush(stdout)
|
||||
#define LOG_ERROR(x, ...) \
|
||||
debugPrintPrefix(x, LOG_LEVEL_ERROR, NULL); \
|
||||
printf(__VA_ARGS__); \
|
||||
std::cout << std::endl; \
|
||||
fflush(stdout)
|
||||
#define LOG_WARNING(x, ...) \
|
||||
debugPrintPrefix(x, LOG_LEVEL_WARNING, NULL); \
|
||||
printf(__VA_ARGS__); \
|
||||
std::cout << std::endl; \
|
||||
fflush(stdout)
|
||||
#define LOG_NOTICE(x, ...) \
|
||||
debugPrintPrefix(x, LOG_LEVEL_NOTICE, NULL); \
|
||||
printf(__VA_ARGS__); \
|
||||
std::cout << std::endl; \
|
||||
fflush(stdout)
|
||||
#define LOG_TRACE(x, ...) \
|
||||
debugPrintPrefix(x, LOG_LEVEL_TRACE, NULL); \
|
||||
printf(__VA_ARGS__); \
|
||||
std::cout << std::endl; \
|
||||
fflush(stdout)
|
||||
#define LOG_VERBOSE(x, ...) \
|
||||
debugPrintPrefix(x, LOG_LEVEL_VERBOSE, NULL); \
|
||||
printf(__VA_ARGS__); \
|
||||
std::cout << std::endl; \
|
||||
fflush(stdout)
|
||||
#define LOG_DEBUG(x, ...) \
|
||||
debugPrintPrefix(x, LOG_LEVEL_DEBUG, NULL); \
|
||||
printf(__VA_ARGS__); \
|
||||
std::cout << std::endl; \
|
||||
fflush(stdout)
|
||||
#define LOG_INFO(x, ...) \
|
||||
debugPrintPrefix(x, LOG_LEVEL_INFO, NULL); \
|
||||
printf(__VA_ARGS__); \
|
||||
std::cout << std::endl; \
|
||||
fflush(stdout)
|
||||
@ -105,16 +127,30 @@ void debugStopSyslog(void);
|
||||
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
// Functions used by ANDROID, WINDOWS and POSSIX
|
||||
void debugLvglLogEvent(lv_log_level_t level, const char* file, uint32_t line, const char* funcname, const char* descr);
|
||||
void debugLoop(void);
|
||||
void debugEverySecond(void);
|
||||
void debugStart(void);
|
||||
void debugStop(void);
|
||||
void debugPrintHaspHeader(Print* output);
|
||||
void debugPrintTag(uint8_t tag, Print* _logOutput);
|
||||
void debugPrintPrefix(uint8_t tag, int level, Print* _logOutput);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
/* ===== Read/Write Configuration ===== */
|
||||
#if HASP_USE_CONFIG > 0
|
||||
bool debugGetConfig(const JsonObject& settings);
|
||||
bool debugSetConfig(const JsonObject& settings);
|
||||
#endif
|
||||
|
||||
// void debugPrintPrefix(int level, Print * _logOutput);
|
||||
// void debugPrintSuffix(int level, Print * _logOutput);
|
||||
// void debugSendOuput(const char * buffer);
|
||||
|
||||
enum {
|
||||
TAG_MAIN = 0,
|
||||
TAG_HASP = 1,
|
||||
@ -154,4 +190,17 @@ enum {
|
||||
TAG_FONT = 92
|
||||
};
|
||||
|
||||
//#define TERM_COLOR_Black "\u001b[30m"
|
||||
#define TERM_COLOR_GRAY "\e[37m"
|
||||
#define TERM_COLOR_RED "\e[91m"
|
||||
#define TERM_COLOR_GREEN "\e[92m"
|
||||
#define TERM_COLOR_ORANGE "\e[38;5;214m"
|
||||
#define TERM_COLOR_YELLOW "\e[93m"
|
||||
#define TERM_COLOR_BLUE "\e[94m"
|
||||
#define TERM_COLOR_MAGENTA "\e[35m"
|
||||
#define TERM_COLOR_CYAN "\e[96m"
|
||||
#define TERM_COLOR_WHITE "\e[97m"
|
||||
#define TERM_COLOR_RESET "\e[0m"
|
||||
#define TERM_CLEAR_LINE "\e[1000D\e[0K"
|
||||
|
||||
#endif
|
||||
|
@ -89,58 +89,14 @@ WiFiUDP* syslogClient;
|
||||
// char serialInputBuffer[220] = "";
|
||||
// uint16_t historyIndex = sizeof(serialInputBuffer) - 1; // Empty buffer
|
||||
uint16_t debugSerialBaud = SERIAL_SPEED / 10; // Multiplied by 10
|
||||
bool debugSerialStarted = false;
|
||||
bool debugAnsiCodes = true;
|
||||
extern bool debugSerialStarted;
|
||||
extern bool debugAnsiCodes;
|
||||
|
||||
ConsoleInput debugConsole(&Serial, HASP_CONSOLE_BUFFER);
|
||||
|
||||
//#define TERM_COLOR_Black "\u001b[30m"
|
||||
#define TERM_COLOR_GRAY "\e[37m"
|
||||
#define TERM_COLOR_RED "\e[91m"
|
||||
#define TERM_COLOR_GREEN "\e[92m"
|
||||
#define TERM_COLOR_ORANGE "\e[38;5;214m"
|
||||
#define TERM_COLOR_YELLOW "\e[93m"
|
||||
#define TERM_COLOR_BLUE "\e[94m"
|
||||
#define TERM_COLOR_MAGENTA "\e[35m"
|
||||
#define TERM_COLOR_CYAN "\e[96m"
|
||||
#define TERM_COLOR_WHITE "\e[97m"
|
||||
#define TERM_COLOR_RESET "\e[0m"
|
||||
#define TERM_CLEAR_LINE "\e[1000D\e[0K"
|
||||
|
||||
unsigned long debugLastMillis = 0;
|
||||
uint16_t debugTelePeriod = 300;
|
||||
|
||||
// Send the HASP header and version to the output device specified
|
||||
void debugPrintHaspHeader(Print* output)
|
||||
{
|
||||
if(debugAnsiCodes) output->print(TERM_COLOR_YELLOW);
|
||||
output->println();
|
||||
output->print(F(""
|
||||
" _____ _____ _____ _____\r\n"
|
||||
" | | | _ | __| _ |\r\n"
|
||||
" | | |__ | __|\r\n"
|
||||
" |__|__|__|__|_____|__|\r\n"
|
||||
" Home Automation Switch Plate\r\n"
|
||||
" Open Hardware edition v"));
|
||||
char buffer[32];
|
||||
haspGetVersion(buffer, sizeof(buffer));
|
||||
output->println(buffer);
|
||||
output->println();
|
||||
}
|
||||
|
||||
void debugStart()
|
||||
{
|
||||
if(debugSerialStarted) {
|
||||
Serial.flush();
|
||||
// Serial.println();
|
||||
// Serial.println(debugHaspHeader());
|
||||
// Serial.flush();
|
||||
}
|
||||
|
||||
// prepare syslog configuration here (can be anywhere before first call of
|
||||
// log/logf method)
|
||||
}
|
||||
|
||||
// #if HASP_USE_SYSLOG > 0
|
||||
// void syslogSend(uint8_t priority, const char * debugText)
|
||||
// {
|
||||
@ -194,11 +150,6 @@ void debugStopSyslog()
|
||||
#endif
|
||||
}
|
||||
|
||||
void debugStop()
|
||||
{
|
||||
if(debugSerialStarted) Serial.flush();
|
||||
}
|
||||
|
||||
#if HASP_USE_CONFIG > 0
|
||||
bool debugGetConfig(const JsonObject& settings)
|
||||
{
|
||||
@ -262,10 +213,6 @@ bool debugSetConfig(const JsonObject& settings)
|
||||
}
|
||||
#endif // HASP_USE_CONFIG
|
||||
|
||||
inline void debugSendAnsiCode(const __FlashStringHelper* code, Print* _logOutput)
|
||||
{
|
||||
if(debugAnsiCodes) _logOutput->print(code);
|
||||
}
|
||||
/*
|
||||
size_t debugHistorycount()
|
||||
{
|
||||
@ -331,280 +278,6 @@ void debugGetHistoryLine(size_t num)
|
||||
}
|
||||
*/
|
||||
|
||||
static void debugPrintTimestamp(int level, Print* _logOutput)
|
||||
{ /* Print Current Time */
|
||||
|
||||
struct timeval tval;
|
||||
struct tm* timeinfo;
|
||||
int rslt;
|
||||
|
||||
/* rslt = gettimeofday(&tval, NULL);
|
||||
if(rslt) {
|
||||
// uint32_t msecs = millis();
|
||||
// _logOutput->printf(PSTR("[%9d.%03d]"), msecs / 1000, msecs % 1000);
|
||||
} else */
|
||||
{
|
||||
timeinfo = localtime(&tval.tv_sec);
|
||||
}
|
||||
|
||||
/*
|
||||
time_t rawtime;
|
||||
struct tm * timeinfo;
|
||||
|
||||
time(&rawtime);
|
||||
timeinfo = localtime(&rawtime);
|
||||
|
||||
// strftime(buffer, sizeof(buffer), "%b %d %H:%M:%S.", timeinfo);
|
||||
// Serial.println(buffer);
|
||||
*/
|
||||
debugSendAnsiCode(F(TERM_COLOR_CYAN), _logOutput);
|
||||
|
||||
if(timeinfo->tm_year >= 120) {
|
||||
char buffer[24];
|
||||
strftime(buffer, sizeof(buffer), "[%b %d %H:%M:%S.", timeinfo); // Literal String
|
||||
// strftime(buffer, sizeof(buffer), "[%H:%M:%S.", timeinfo); // Literal String
|
||||
_logOutput->print(buffer);
|
||||
_logOutput->printf(PSTR("%03lu]"), tval.tv_usec / 1000);
|
||||
} else {
|
||||
uint32_t msecs = millis();
|
||||
_logOutput->printf(PSTR("[%15d.%03d]"), msecs / 1000, msecs % 1000);
|
||||
}
|
||||
}
|
||||
|
||||
static void debugPrintHaspMemory(int level, Print* _logOutput)
|
||||
{
|
||||
size_t maxfree = haspDevice.get_free_max_block();
|
||||
size_t totalfree = haspDevice.get_free_heap();
|
||||
uint8_t frag = haspDevice.get_heap_fragmentation();
|
||||
|
||||
/* Print HASP Memory Info */
|
||||
if(debugAnsiCodes) {
|
||||
if(maxfree > (1024u * 5) && (totalfree > 1024u * 6) && (frag <= 10))
|
||||
debugSendAnsiCode(F(TERM_COLOR_GREEN), _logOutput);
|
||||
else if(maxfree > (1024u * 3) && (totalfree > 1024u * 5) && (frag <= 20))
|
||||
debugSendAnsiCode(F(TERM_COLOR_ORANGE), _logOutput);
|
||||
else
|
||||
debugSendAnsiCode(F(TERM_COLOR_RED), _logOutput);
|
||||
}
|
||||
_logOutput->printf(PSTR("[%5u/%5u%3u]"), maxfree, totalfree, frag);
|
||||
}
|
||||
|
||||
#if LV_MEM_CUSTOM == 0
|
||||
static void debugPrintLvglMemory(int level, Print* _logOutput)
|
||||
{
|
||||
lv_mem_monitor_t mem_mon;
|
||||
lv_mem_monitor(&mem_mon);
|
||||
|
||||
/* Print LVGL Memory Info */
|
||||
if(debugAnsiCodes) {
|
||||
if(mem_mon.free_biggest_size > (1024u * 2) && (mem_mon.free_size > 1024u * 2.5) && (mem_mon.frag_pct <= 10))
|
||||
debugSendAnsiCode(F(TERM_COLOR_GREEN), _logOutput);
|
||||
else if(mem_mon.free_biggest_size > (1024u * 1) && (mem_mon.free_size > 1024u * 1.5) &&
|
||||
(mem_mon.frag_pct <= 25))
|
||||
debugSendAnsiCode(F(TERM_COLOR_ORANGE), _logOutput);
|
||||
else
|
||||
debugSendAnsiCode(F(TERM_COLOR_RED), _logOutput);
|
||||
}
|
||||
_logOutput->printf(PSTR("[%5u/%5u%3u]"), mem_mon.free_biggest_size, mem_mon.free_size, mem_mon.frag_pct);
|
||||
}
|
||||
#endif
|
||||
|
||||
static void debugPrintPriority(int level, Print* _logOutput)
|
||||
{
|
||||
// if(_logOutput == &syslogClient) {
|
||||
// }
|
||||
|
||||
switch(level) {
|
||||
case LOG_LEVEL_FATAL ... LOG_LEVEL_ERROR:
|
||||
debugSendAnsiCode(F(TERM_COLOR_RED), _logOutput);
|
||||
break;
|
||||
case LOG_LEVEL_WARNING:
|
||||
debugSendAnsiCode(F(TERM_COLOR_YELLOW), _logOutput);
|
||||
break;
|
||||
case LOG_LEVEL_NOTICE:
|
||||
debugSendAnsiCode(F(TERM_COLOR_WHITE), _logOutput);
|
||||
break;
|
||||
case LOG_LEVEL_TRACE:
|
||||
debugSendAnsiCode(F(TERM_COLOR_GRAY), _logOutput);
|
||||
break;
|
||||
case LOG_LEVEL_VERBOSE:
|
||||
debugSendAnsiCode(F(TERM_COLOR_CYAN), _logOutput);
|
||||
break;
|
||||
case LOG_LEVEL_DEBUG:
|
||||
debugSendAnsiCode(F(TERM_COLOR_BLUE), _logOutput);
|
||||
break;
|
||||
case LOG_LEVEL_OUTPUT:
|
||||
default:
|
||||
debugSendAnsiCode(F(TERM_COLOR_RESET), _logOutput);
|
||||
}
|
||||
}
|
||||
|
||||
static void debugPrintTag(uint8_t tag, Print* _logOutput)
|
||||
{
|
||||
switch(tag) {
|
||||
case TAG_MAIN:
|
||||
_logOutput->print(F("MAIN"));
|
||||
break;
|
||||
|
||||
case TAG_HASP:
|
||||
_logOutput->print(F("HASP"));
|
||||
break;
|
||||
|
||||
case TAG_DRVR:
|
||||
_logOutput->print(F("DRVR"));
|
||||
break;
|
||||
|
||||
case TAG_ATTR:
|
||||
_logOutput->print(F("ATTR"));
|
||||
break;
|
||||
|
||||
case TAG_MSGR:
|
||||
_logOutput->print(F("MSGR"));
|
||||
break;
|
||||
|
||||
case TAG_OOBE:
|
||||
_logOutput->print(F("OOBE"));
|
||||
break;
|
||||
case TAG_HAL:
|
||||
_logOutput->print(F("HAL "));
|
||||
break;
|
||||
|
||||
case TAG_DEBG:
|
||||
_logOutput->print(F("DEBG"));
|
||||
break;
|
||||
case TAG_TELN:
|
||||
_logOutput->print(F("TELN"));
|
||||
break;
|
||||
case TAG_SYSL:
|
||||
_logOutput->print(F("SYSL"));
|
||||
break;
|
||||
case TAG_TASM:
|
||||
_logOutput->print(F("TASM"));
|
||||
break;
|
||||
|
||||
case TAG_CONF:
|
||||
_logOutput->print(F("CONF"));
|
||||
break;
|
||||
case TAG_GUI:
|
||||
_logOutput->print(F("GUI "));
|
||||
break;
|
||||
case TAG_TFT:
|
||||
_logOutput->print(F("TFT "));
|
||||
break;
|
||||
|
||||
case TAG_EPRM:
|
||||
_logOutput->print(F("EPRM"));
|
||||
break;
|
||||
case TAG_FILE:
|
||||
_logOutput->print(F("FILE"));
|
||||
break;
|
||||
case TAG_GPIO:
|
||||
_logOutput->print(F("GPIO"));
|
||||
break;
|
||||
|
||||
case TAG_ETH:
|
||||
_logOutput->print(F("ETH "));
|
||||
break;
|
||||
case TAG_WIFI:
|
||||
_logOutput->print(F("WIFI"));
|
||||
break;
|
||||
case TAG_HTTP:
|
||||
_logOutput->print(F("HTTP"));
|
||||
break;
|
||||
case TAG_MDNS:
|
||||
_logOutput->print(F("MDNS"));
|
||||
break;
|
||||
case TAG_MQTT:
|
||||
_logOutput->print(F("MQTT"));
|
||||
break;
|
||||
case TAG_MQTT_PUB:
|
||||
_logOutput->print(F("MQTT PUB"));
|
||||
break;
|
||||
case TAG_MQTT_RCV:
|
||||
_logOutput->print(F("MQTT RCV"));
|
||||
break;
|
||||
|
||||
case TAG_OTA:
|
||||
_logOutput->print(F("OTA"));
|
||||
break;
|
||||
case TAG_FWUP:
|
||||
_logOutput->print(F("FWUP"));
|
||||
break;
|
||||
|
||||
case TAG_LVGL:
|
||||
_logOutput->print(F("LVGL"));
|
||||
break;
|
||||
case TAG_LVFS:
|
||||
_logOutput->print(F("LVFS"));
|
||||
break;
|
||||
case TAG_FONT:
|
||||
_logOutput->print(F("FONT"));
|
||||
break;
|
||||
|
||||
default:
|
||||
_logOutput->print(F("----"));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void debugPrintPrefix(uint8_t tag, int level, Print* _logOutput)
|
||||
{
|
||||
#if HASP_USE_SYSLOG > 0
|
||||
|
||||
if(_logOutput == syslogClient && syslogClient) {
|
||||
if(syslogClient->beginPacket(debugSyslogHost, debugSyslogPort)) {
|
||||
|
||||
// IETF Doc: https://tools.ietf.org/html/rfc5424 - The Syslog Protocol
|
||||
// BSD Doc: https://tools.ietf.org/html/rfc3164 - The BSD syslog Protocol
|
||||
|
||||
syslogClient->print(F("<"));
|
||||
syslogClient->print((16 + debugSyslogFacility) * 8 + level);
|
||||
syslogClient->print(F(">"));
|
||||
|
||||
if(debugSyslogProtocol == SYSLOG_PROTO_IETF) {
|
||||
syslogClient->print(F("1 - "));
|
||||
}
|
||||
|
||||
syslogClient->print(haspDevice.get_hostname());
|
||||
syslogClient->print(F(" "));
|
||||
debugPrintTag(tag, _logOutput);
|
||||
|
||||
if(debugSyslogProtocol == SYSLOG_PROTO_IETF) {
|
||||
syslogClient->print(F(" - - - \xEF\xBB\xBF")); // include UTF-8 BOM
|
||||
} else {
|
||||
syslogClient->print(F(": "));
|
||||
}
|
||||
|
||||
debugPrintHaspMemory(level, _logOutput);
|
||||
#if LV_MEM_CUSTOM == 0
|
||||
debugPrintLvglMemory(level, _logOutput);
|
||||
#endif
|
||||
}
|
||||
return;
|
||||
}
|
||||
#endif // HASP_USE_SYSLOG
|
||||
|
||||
debugSendAnsiCode(F(TERM_CLEAR_LINE), _logOutput);
|
||||
debugPrintTimestamp(level, _logOutput);
|
||||
debugPrintHaspMemory(level, _logOutput);
|
||||
|
||||
#if LV_MEM_CUSTOM == 0
|
||||
debugPrintLvglMemory(level, _logOutput);
|
||||
#endif
|
||||
|
||||
if(tag == TAG_MQTT_PUB && level == LOG_LEVEL_NOTICE) {
|
||||
debugSendAnsiCode(F(TERM_COLOR_GREEN), _logOutput);
|
||||
} else if(tag == TAG_MQTT_RCV && level == LOG_LEVEL_NOTICE) {
|
||||
debugSendAnsiCode(F(TERM_COLOR_ORANGE), _logOutput);
|
||||
} else {
|
||||
debugPrintPriority(level, _logOutput);
|
||||
}
|
||||
|
||||
_logOutput->print(F(" "));
|
||||
debugPrintTag(tag, _logOutput);
|
||||
_logOutput->print(F(": "));
|
||||
}
|
||||
|
||||
void debugPrintSuffix(uint8_t tag, int level, Print* _logOutput)
|
||||
{
|
||||
#if HASP_USE_SYSLOG > 0
|
||||
@ -660,37 +333,6 @@ void debugPreSetup(JsonObject settings)
|
||||
}
|
||||
}
|
||||
|
||||
#if LV_USE_LOG != 0
|
||||
void debugLvglLogEvent(lv_log_level_t level, const char* file, uint32_t line, const char* funcname, const char* descr)
|
||||
{
|
||||
/* used for duplicate detection */
|
||||
static uint32_t lastDbgLine;
|
||||
static uint16_t lastDbgFreeMem;
|
||||
|
||||
lv_mem_monitor_t mem_mon;
|
||||
lv_mem_monitor(&mem_mon);
|
||||
|
||||
/* Reduce the number of repeated debug message */
|
||||
if(line != lastDbgLine || mem_mon.free_biggest_size != lastDbgFreeMem) {
|
||||
switch(level) {
|
||||
case LV_LOG_LEVEL_TRACE:
|
||||
LOG_VERBOSE(TAG_LVGL, descr);
|
||||
break;
|
||||
case LV_LOG_LEVEL_WARN:
|
||||
LOG_WARNING(TAG_LVGL, descr);
|
||||
break;
|
||||
case LV_LOG_LEVEL_ERROR:
|
||||
LOG_ERROR(TAG_LVGL, descr);
|
||||
break;
|
||||
default:
|
||||
LOG_TRACE(TAG_LVGL, descr);
|
||||
}
|
||||
lastDbgLine = line;
|
||||
lastDbgFreeMem = mem_mon.free_biggest_size;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
void debugLoop(void)
|
||||
{
|
||||
int16_t keypress;
|
||||
@ -748,12 +390,3 @@ void printLocalTime()
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void debugEverySecond()
|
||||
{
|
||||
if(debugTelePeriod > 0 && (millis() - debugLastMillis) >= debugTelePeriod * 1000) {
|
||||
dispatch_output_statusupdate(NULL, NULL);
|
||||
debugLastMillis = millis();
|
||||
}
|
||||
// printLocalTime();
|
||||
}
|
||||
|
@ -104,11 +104,6 @@ void InitializeConsoleOutput()
|
||||
}
|
||||
#endif
|
||||
|
||||
void debugLvglLogEvent(lv_log_level_t level, const char* file, uint32_t line, const char* funcname, const char* descr)
|
||||
{
|
||||
printf("%s %d\n", file, line);
|
||||
}
|
||||
|
||||
void setup()
|
||||
{
|
||||
// Load Settings
|
||||
@ -117,8 +112,8 @@ void setup()
|
||||
// debug_init();
|
||||
|
||||
// Initialize lvgl environment
|
||||
lv_log_register_print_cb(debugLvglLogEvent);
|
||||
lv_init();
|
||||
lv_log_register_print_cb(debugLvglLogEvent);
|
||||
|
||||
haspDevice.init(); // hardware setup
|
||||
haspDevice.show_info(); // debug info
|
||||
|
Loading…
x
Reference in New Issue
Block a user