Zcela novy princip pouzivani Trace vystupnich modulu. Jeste ale stale je co zlepsovat... Taky bude nutno aktualizovat priklad.

This commit is contained in:
Pavel Brychta 2024-07-30 17:04:56 +02:00
parent 7bcecdcd2b
commit a34d7cd0c5
7 changed files with 204 additions and 411 deletions

View File

@ -1,88 +0,0 @@
/**
* @file traceStream.hpp
* @author Pavel Brychta, http://www.xpablo.cz
*
* Copyright (c) 2016-24 Pavel Brychta. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#pragma once
#include <Arduino.h>
/**
* @brief Inicializace modulu
*/
void trace_init();
/**
* @brief Vyprazdneni stopovaciho bufferu
*/
void trace_clear();
/**
* @brief Ukonceni prace stopare - vyprazdni buffer
*/
void trace_end();
/**
* @brief Ulozeni zpravy s obsahem z programove pameti (PROGMEM, F, ...)
*
* @param[in] severity Uroven - viz. TRACE_ERROR, TRACE_WARNING, ...
* @param[in] fmt Formatovaci retezec vysledne zpravy. Je ulozeny v PROGMEM
* @param[in] <unnamed> Parametry, ktere jsou dosazeny do formatovaciho retezce
*/
void trace_print(uint8_t severity, const __FlashStringHelper * fmt, ...);
/**
* @brief Ulozeni zpravy
*
* @param[in] severity Uroven - viz. TRACE_ERROR, TRACE_WARNING, ...
* @param[in] fmt Formatovaci retezec vysledne zpravy.
* @param[in] <unnamed> Parametry, ktere jsou dosazeny do formatovaciho retezce
*/
void trace_print(uint8_t severity, const char * fmt, ...);
/**
* @brief Registrace callback metody - je volana pri kazde zmene (zapisu) do stopare
*
* @param[in] <unnamed> Ukazatel na volanou metodu
*/
void trace_registermessagecb(void (*)(const char * msg));
void trace_printfunc(uint8_t severity, const char * func, const char * file, int line, const char * fmt, ...);
/**
* @brief Vypis oblasti pameti (dump)
*
* @param[in] severity Uroven - viz. TRACE_ERROR, TRACE_WARNING, ...
* @param[in] prefix Prefix (text), ktery je uvedeny pred vlastnim vypisem pameti
* @param address Adresa pocatku vypisu
* @param[in] size Pocet vypisovanych bytu (POZOR!!! musi se vejit do jednoho radku stopare!!!)
*/
void trace_dump(uint8_t severity, const char * prefix, uint8_t * address, size_t size);
/**
* @brief Vypis oblasti pameti (dump)
*
* @param[in] severity Uroven - viz. TRACE_ERROR, TRACE_WARNING, ...
* @param[in] prefix Prefix (text), ktery je uvedeny pred vlastnim vypisem pameti
* @param address Adresa pocatku vypisu
* @param[in] size Pocet vypisovanych bytu (POZOR!!! musi se vejit do jednoho radku stopare!!!)
*/
void trace_dump(uint8_t severity, const __FlashStringHelper * prefix, uint8_t * address, size_t size);
void trace_setstream(Stream * out);

View File

@ -1,108 +0,0 @@
/**
* @file traceWebsocket.h
* @author Pavel Brychta, http://www.xpablo.cz
*
* Copyright (c) 2016-24 Pavel Brychta. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#pragma once
#include <Arduino.h>
#include <ESPAsyncWebServer.h>
#define TRACEWS_CHECK_INTERVAL 200 // interval [ms], po kterem je testovano odesilani stopare
/**
* @brief Inicializace modulu
*/
void trace_init();
/**
* @brief Ziskani vypisu v JSON formatu
*
* @param str retezec udalosti v JSON
*/
void trace_dumpJSON(String & str);
/**
* @brief Vyprazdneni stopovaciho bufferu
*/
void trace_clear();
/**
* @brief Ukonceni prace stopare - vyprazdni buffer
*/
void trace_end();
/**
* @brief Ulozeni zpravy s obsahem z programove pameti (PROGMEM, F, ...)
*
* @param[in] severity Uroven - viz. TRACE_ERROR, TRACE_WARNING, ...
* @param[in] fmt Formatovaci retezec vysledne zpravy. Je ulozeny v PROGMEM
* @param[in] <unnamed> Parametry, ktere jsou dosazeny do formatovaciho retezce
*/
void trace_print(uint8_t severity, const __FlashStringHelper * fmt, ...);
/**
* @brief Ulozeni zpravy
*
* @param[in] severity Uroven - viz. TRACE_ERROR, TRACE_WARNING, ...
* @param[in] fmt Formatovaci retezec vysledne zpravy.
* @param[in] <unnamed> Parametry, ktere jsou dosazeny do formatovaciho retezce
*/
void trace_print(uint8_t severity, const char * fmt, ...);
/**
* @brief Registrace callback metody - je volana pri kazde zmene (zapisu) do stopare
*
* @param[in] <unnamed> Ukazatel na volanou metodu
*/
void trace_registermessagecb(void (*)(const char * msg));
/**
* @brief Pridani obsluhy stranky /trace do weboveho serveru
*
* @param socket webovy soket pro komunikaci
*/
void trace_addweb(AsyncWebSocket * socket);
void trace_printfunc(uint8_t severity, const char * func, const char * file, int line, const char * fmt, ...);
/**
* @brief Vypis oblasti pameti (dump)
*
* @param[in] severity Uroven - viz. TRACE_ERROR, TRACE_WARNING, ...
* @param[in] prefix Prefix (text), ktery je uvedeny pred vlastnim vypisem pameti
* @param address Adresa pocatku vypisu
* @param[in] size Pocet vypisovanych bytu (POZOR!!! musi se vejit do jednoho radku stopare!!!)
*/
void trace_dump(uint8_t severity, const char * prefix, uint8_t * address, size_t size);
/**
* @brief Vypis oblasti pameti (dump)
*
* @param[in] severity Uroven - viz. TRACE_ERROR, TRACE_WARNING, ...
* @param[in] prefix Prefix (text), ktery je uvedeny pred vlastnim vypisem pameti
* @param address Adresa pocatku vypisu
* @param[in] size Pocet vypisovanych bytu (POZOR!!! musi se vejit do jednoho radku stopare!!!)
*/
void trace_dump(uint8_t severity, const __FlashStringHelper * prefix, uint8_t * address, size_t size);
/**
* @brief Vynuceni odeslani obsahu bufferu (napr. pri obnovovani spojeni)
*/
void trace_forceupdate();

View File

@ -20,6 +20,8 @@
* *
*/ */
#pragma once #pragma once
#include <Arduino.h>
#include <ESPAsyncWebServer.h>
// Vydefinujeme-li promennou TRACE_ALL=1, pak TRACEPLUS bude vypisovat vsechny zpravy nezavisle od povoleni/zakazani // Vydefinujeme-li promennou TRACE_ALL=1, pak TRACEPLUS bude vypisovat vsechny zpravy nezavisle od povoleni/zakazani
@ -37,123 +39,39 @@
#define STRINGIFY(x) #x #define STRINGIFY(x) #x
#define TOSTRING(x) STRINGIFY(x) #define TOSTRING(x) STRINGIFY(x)
#if defined(TRACE_USE_ASYNC_WEBSOCKET) class Trace {
#include "asyncWebsocket/traceAsyncWebsocket.hpp" protected:
public:
Trace() = default;
#define TRACE(severity, text, ...) \ virtual ~Trace() = default;
virtual void init() {};
virtual void print(uint8_t severity, const __FlashStringHelper * fmt, ...) {};
virtual void print(uint8_t severity, const char * fmt, ...) {};
virtual void dump(uint8_t severity, const char * prefix, const uint8_t * address, size_t size) {};
virtual void dump(uint8_t severity, const __FlashStringHelper * prefix, const uint8_t * address, size_t size) {};
virtual void forceupdate() {};
};
extern Trace * mTrace; // Main Trace instance
#define TRACE(severity, text, ...) \
do { \ do { \
constexpr bool trace_limit = (severity <= TRACE_LIMIT); \ constexpr bool trace_limit = (severity <= TRACE_LIMIT); \
if (trace_limit) \ if (trace_limit) \
trace_print(severity, PSTR(text), ##__VA_ARGS__); \ if (mTrace) { mTrace->print(severity, PSTR(text), ##__VA_ARGS__); } \
} while (0) } while (0)
#define TRACEFUNC(severity, ...) trace_printfunc(severity, __func__, __FILE__, TOSTRING(__LINE__), __VA_ARGS__) #define TRACEFUNC(severity, ...) if (mTrace) { mTrace->printfunc(severity, __func__, __FILE__, TOSTRING(__LINE__), __VA_ARGS__); }
#define TRACEDUMP(severity, prefix, address, size) trace_dump(severity, prefix, address, size) #define TRACEDUMP(severity, prefix, address, size) if (mTrace) { mTrace->dump(severity, prefix, address, size); }
#define TRACE_INIT(a) trace_init() #define TRACE_INIT(a) if (mTrace) { mTrace->init(); }
#define TRACE_ADDWEB(srv) trace_addweb(srv) #define TRACE_ADDWEB(srv) if (mTrace) { mTrace->addweb(srv); }
#define TRACE_FORCEUPDATE(a) trace_forceupdate() #define TRACE_FORCEUPDATE(a) if (mTrace) { mTrace->forceupdate(); }
#elif defined(TRACE_USE_STREAM)
#include "Stream/traceStream.hpp"
#define TRACE(severity, text, ...) \
do { \
constexpr bool trace_limit = (severity <= TRACE_LIMIT); \
if (trace_limit) \
trace_print(severity, PSTR(text), ##__VA_ARGS__); \
} while (0)
#define TRACEFUNC(severity, ...) trace_printfunc(severity, __func__, __FILE__, TOSTRING(__LINE__), __VA_ARGS__)
#define TRACEDUMP(severity, prefix, address, size) trace_dump(severity, prefix, address, size)
#define TRACE_INIT(a) trace_init()
#define TRACE_ADDWEB(srv) ((void) 0)
#define TRACE_FORCEUPDATE(a) ((void) 0)
#else
#if defined(ESP32)
#include <Arduino.h>
#define TRACE(severity, format, ...) \
do { \
constexpr int s = severity; \
if (s == TRACE_ERROR) \
log_e(format, ##__VA_ARGS__); \
else if (s == TRACE_DEBUG) \
log_d(format, ##__VA_ARGS__); \
else if (s == TRACE_WARNING) \
log_w(format, ##__VA_ARGS__); \
else if (s == TRACE_INFO) \
log_i(format, ##__VA_ARGS__); \
else if (s == TRACE_VERBOSE) \
log_v(format, ##__VA_ARGS__); \
} while (0)
#else
#ifdef DEBUG_ESP_PORT
#include <Arduino.h>
// following is inspired by macros, used in ESP-IDF log_x()
#ifndef TRACE_LOG_COLORS
#define TRACE_LOG_COLORS 0
#endif
#if TRACE_LOG_COLORS
#define TRACE_LOG_COLOR_BLACK "30"
#define TRACE_LOG_COLOR_RED "31" // ERROR
#define TRACE_LOG_COLOR_GREEN "32" // INFO
#define TRACE_LOG_COLOR_YELLOW "33" // WARNING
#define TRACE_LOG_COLOR_BLUE "34"
#define TRACE_LOG_COLOR_MAGENTA "35"
#define TRACE_LOG_COLOR_CYAN "36" // DEBUG
#define TRACE_LOG_COLOR_GRAY "37" // VERBOSE
#define TRACE_LOG_COLOR_WHITE "38"
#define TRACE_LOG_COLOR(COLOR) "\033[0;" COLOR "m"
#define TRACE_LOG_BOLD(COLOR) "\033[1;" COLOR "m"
#define TRACE_LOG_RESET_COLOR "\033[0m"
#define TRACE_LOG_COLOR_E TRACE_LOG_COLOR(TRACE_LOG_COLOR_RED)
#define TRACE_LOG_COLOR_W TRACE_LOG_COLOR(TRACE_LOG_COLOR_YELLOW)
#define TRACE_LOG_COLOR_I TRACE_LOG_COLOR(TRACE_LOG_COLOR_GREEN)
#define TRACE_LOG_COLOR_D TRACE_LOG_COLOR(TRACE_LOG_COLOR_CYAN)
#define TRACE_LOG_COLOR_V TRACE_LOG_COLOR(TRACE_LOG_COLOR_GRAY)
#define TRACE_LOG_COLOR_PRINT(letter) log_printf(TRACE_LOG_COLOR_##letter)
#define TRACE_LOG_COLOR_PRINT_END log_printf(TRACE_LOG_RESET_COLOR)
#else
#define TRACE_LOG_COLOR_E
#define TRACE_LOG_COLOR_W
#define TRACE_LOG_COLOR_I
#define TRACE_LOG_COLOR_D
#define TRACE_LOG_COLOR_V
#define TRACE_LOG_RESET_COLOR
#define TRACE_LOG_COLOR_PRINT(letter)
#define TRACE_LOG_COLOR_PRINT_END
#endif
#define TRACE_SHORT_LOG_FORMAT(letter, format) PSTR(TRACE_LOG_COLOR_##letter format TRACE_LOG_RESET_COLOR "\n")
#define TRACE_LOG_FORMAT(letter, format) PSTR(TRACE_LOG_COLOR_##letter "[%8lu][" #letter "] " format TRACE_LOG_RESET_COLOR "\n"), millis()
#define TRACE(severity, text, ...) \
do { \
constexpr int s = severity; \
if (s == TRACE_ERROR) \
DEBUG_ESP_PORT.printf_P(TRACE_LOG_FORMAT(E, text), ##__VA_ARGS__); \
else if (s == TRACE_DEBUG) \
DEBUG_ESP_PORT.printf_P(TRACE_LOG_FORMAT(D, text), ##__VA_ARGS__); \
else if (s == TRACE_WARNING) \
DEBUG_ESP_PORT.printf_P(TRACE_LOG_FORMAT(W, text), ##__VA_ARGS__); \
else if (s == TRACE_INFO) \
DEBUG_ESP_PORT.printf_P(TRACE_LOG_FORMAT(I, text), ##__VA_ARGS__); \
else if (s == TRACE_VERBOSE) \
DEBUG_ESP_PORT.printf_P(TRACE_LOG_FORMAT(V, text), ##__VA_ARGS__); \
} while (0)
#else
#warning TRACE output is disabled!
#define TRACE(...) ((void) 0) // from assert.h "NOP" - http://stackoverflow.com/questions/9187628/c-empty-function-macros
#endif
#endif
#define TRACEFUNC(...) ((void) 0)
#define TRACEDUMP(...) ((void) 0)
#define TRACE_INIT(a) ((void) 0)
#define TRACE_ADDWEB(a) ((void) 0)
#define TRACE_FORCEUPDATE(a) ((void) 0)
#endif
#ifndef TRACE_ALL #ifndef TRACE_ALL
#define TRACE_ALL 0 #define TRACE_ALL 0

View File

@ -1,41 +1,15 @@
#include "traceAsyncWebsocket.hpp" #include "traceAsyncWebsocket.hpp"
#include <Chronos.hpp> #include <Chronos.hpp>
#define MAX_TRACE_LINES 15
#define MAX_LINE_LEN 50
struct TraceLine { int TraceAsyncWebsocket::modulo(int a, int b)
char _text[MAX_LINE_LEN + 1] = {0};
uint32_t _time{};
uint8_t _severity{};
TraceLine(uint8_t severity, const char * str, uint16_t len)
{
strncpy(_text, str, sizeof(_text));
_text[MAX_LINE_LEN] = 0;
_time = millis();
_severity = severity;
}
TraceLine() = default;
};
static TraceLine _lines[MAX_TRACE_LINES];
static uint16_t _lines_index = 0;
static uint16_t _lines_count = 0;
static volatile int _modified = 0;
static AsyncWebSocket * _wss = nullptr; // webovy soket pro trasovani
static void (*message_cb)(const char *) = nullptr;
static int modulo(int a, int b)
{ {
int r = a % b; int r = a % b;
return ((r < 0) ? r + b : r); return ((r < 0) ? r + b : r);
} }
static TraceLine & trace_line(uint16_t index) TraceLine & TraceAsyncWebsocket::tLine(const uint16_t index)
{ {
int start = _lines_index - _lines_count; int start = _lines_index - _lines_count;
int idx = modulo(start + index, _lines_count); int idx = modulo(start + index, _lines_count);
@ -43,7 +17,7 @@ static TraceLine & trace_line(uint16_t index)
return (_lines[idx]); return (_lines[idx]);
} }
static void print(uint8_t severity, const char * buffer, int length) void TraceAsyncWebsocket::print(uint8_t severity, const char * buffer, int length)
{ {
char lin[MAX_LINE_LEN + 1]; char lin[MAX_LINE_LEN + 1];
unsigned int lineptr = 0; unsigned int lineptr = 0;
@ -68,10 +42,10 @@ static void print(uint8_t severity, const char * buffer, int length)
_modified = _modified + 1; _modified = _modified + 1;
} }
static void trace_loop() void TraceAsyncWebsocket::loop()
{ {
if (nullptr != _wss) { if (_wss) {
// je definovany webovy soket // je definovany webovy soket
if (_modified) { if (_modified) {
// mame nejakou zmenu // mame nejakou zmenu
@ -82,7 +56,7 @@ static void trace_loop()
_modified = 0; // rusime pozadavek na odeslani novych dat _modified = 0; // rusime pozadavek na odeslani novych dat
if (log.reserve((MAX_TRACE_LINES * MAX_LINE_LEN) + (MAX_TRACE_LINES * 50))) { if (log.reserve((MAX_TRACE_LINES * MAX_LINE_LEN) + (MAX_TRACE_LINES * 50))) {
log = F("{\"type\":\"trace\",\"data\":["); log = F("{\"type\":\"trace\",\"data\":[");
trace_dumpJSON(log); dumpJSON(log);
log.concat(F("]}")); log.concat(F("]}"));
if (_wss->availableForWriteAll()) if (_wss->availableForWriteAll())
_wss->textAll(log); _wss->textAll(log);
@ -94,13 +68,13 @@ static void trace_loop()
} }
} }
void trace_init() void TraceAsyncWebsocket::init()
{ {
Chronos::attachMs(TRACEWS_CHECK_INTERVAL, trace_loop); Chronos::attachMs(TRACEWS_CHECK_INTERVAL, this);
} }
static String _getText(const char * buffer) String TraceAsyncWebsocket::_getText(const char * buffer)
{ {
String res; String res;
@ -144,11 +118,11 @@ static String _getText(const char * buffer)
return res; return res;
} }
void trace_dumpJSON(String & str) void TraceAsyncWebsocket::dumpJSON(String & str)
{ {
for (int i = 0; i < _lines_count; i++) { for (int i = 0; i < _lines_count; i++) {
TraceLine line = trace_line(i); TraceLine line = tLine(i);
if (0 != i) if (0 != i)
str.concat(F(",")); str.concat(F(","));
@ -162,59 +136,31 @@ void trace_dumpJSON(String & str)
} }
} }
void trace_clear() void TraceAsyncWebsocket::print(uint8_t severity, const __FlashStringHelper * fmt, ...)
{
_lines_index = 0;
_lines_count = 0;
_modified = 1;
}
void trace_end()
{
trace_clear();
}
void trace_print(uint8_t severity, const __FlashStringHelper * fmt, ...)
{ {
char buffer[MAX_LINE_LEN + 1]; char buffer[MAX_LINE_LEN + 1];
va_list args; va_list args;
int length;
va_start(args, fmt); va_start(args, fmt);
length = vsnprintf_P(buffer, sizeof(buffer), (const char *) fmt, args); int length = vsnprintf_P(buffer, sizeof(buffer), (const char *) fmt, args);
va_end(args); va_end(args);
print(severity, buffer, length); print(severity, buffer, length);
} }
void trace_print(uint8_t severity, const char * fmt, ...) void TraceAsyncWebsocket::print(uint8_t severity, const char * fmt, ...)
{ {
char buffer[MAX_LINE_LEN + 1]; char buffer[MAX_LINE_LEN + 1];
va_list args; va_list args;
int length;
va_start(args, fmt); va_start(args, fmt);
length = vsnprintf(buffer, sizeof(buffer), fmt, args); int length = vsnprintf(buffer, sizeof(buffer), fmt, args);
va_end(args); va_end(args);
print(severity, buffer, length); print(severity, buffer, length);
} }
void trace_registermessagecb(void (*cb)(const char *)) char TraceAsyncWebsocket::hexascii(uint8_t n)
{
message_cb = cb;
}
void trace_addweb(AsyncWebSocket * socket)
{
_wss = socket;
}
static char hexascii(uint8_t n)
{ {
n &= 0xf; n &= 0xf;
@ -224,7 +170,7 @@ static char hexascii(uint8_t n)
return n + '0'; return n + '0';
} }
void trace_dump(uint8_t severity, const char * prefix, uint8_t * address, size_t size) void TraceAsyncWebsocket::dump(uint8_t severity, const char * prefix, const uint8_t * address, size_t size)
{ {
char buffer[MAX_LINE_LEN + 1 + 3]; char buffer[MAX_LINE_LEN + 1 + 3];
int idx = 0; int idx = 0;
@ -246,7 +192,7 @@ void trace_dump(uint8_t severity, const char * prefix, uint8_t * address, size_t
print(severity, buffer, idx); print(severity, buffer, idx);
} }
void trace_dump(uint8_t severity, const __FlashStringHelper * prefix, uint8_t * address, size_t size) void TraceAsyncWebsocket::dump(uint8_t severity, const __FlashStringHelper * prefix, const uint8_t * address, size_t size)
{ {
char buffer[MAX_LINE_LEN + 1 + 3]; char buffer[MAX_LINE_LEN + 1 + 3];
int idx = 0; int idx = 0;
@ -272,7 +218,7 @@ void trace_dump(uint8_t severity, const __FlashStringHelper * prefix, uint8_t *
print(severity, buffer, idx); print(severity, buffer, idx);
} }
void trace_forceupdate() void TraceAsyncWebsocket::forceupdate()
{ {
_modified = _modified + 1; // vynutime odeslani informaci _modified = _modified + 1; // vynutime odeslani informaci

View File

@ -0,0 +1,85 @@
/**
* @file traceWebsocket.h
* @author Pavel Brychta, http://www.xpablo.cz
*
* Copyright (c) 2016-24 Pavel Brychta. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#pragma once
#include "trace.h"
#include <Arduino.h>
#include <Chronos.hpp>
#include <ESPAsyncWebServer.h>
#define TRACEWS_CHECK_INTERVAL 200 // interval [ms], po kterem je testovano odesilani stopare
#define MAX_TRACE_LINES 15
#define MAX_LINE_LEN 50
struct TraceLine {
char _text[MAX_LINE_LEN + 1] = {0};
uint32_t _time{};
uint8_t _severity{};
TraceLine(uint8_t severity, const char * str, uint16_t len) {
strncpy(_text, str, sizeof(_text));
_text[MAX_LINE_LEN] = 0;
_time = millis();
_severity = severity;
}
TraceLine() = default;
};
class TraceAsyncWebsocket final : public Trace, public Loop {
protected:
TraceLine _lines[MAX_TRACE_LINES];
uint16_t _lines_index = 0;
uint16_t _lines_count = 0;
volatile int _modified = 0;
AsyncWebSocket * _wss = nullptr; // webovy soket pro trasovani
static int modulo(int a, int b);
TraceLine & tLine(uint16_t index);
static char hexascii(uint8_t n);
static String _getText(const char * buffer);
void dumpJSON(String & str);
void print(uint8_t severity, const char * buffer, int length);
public:
explicit TraceAsyncWebsocket(AsyncWebSocket * srv) : _wss(srv) {};
void init() override;
void print(uint8_t severity, const __FlashStringHelper * fmt, ...) override;
void print(uint8_t severity, const char * fmt, ...) override;
void dump(uint8_t severity, const char * prefix, const uint8_t * address, size_t size) override;
void dump(uint8_t severity, const __FlashStringHelper * prefix, const uint8_t * address, size_t size) override;
void loop() override;
void forceupdate() override;
};

View File

@ -40,38 +40,28 @@
#define TRACE_SHORT_LOG_FORMAT(letter, format) PSTR(TRACE_LOG_COLOR_##letter "%s" TRACE_LOG_RESET_COLOR "\r\n"), format #define TRACE_SHORT_LOG_FORMAT(letter, format) PSTR(TRACE_LOG_COLOR_##letter "%s" TRACE_LOG_RESET_COLOR "\r\n"), format
#define TRACE_LOG_FORMAT(letter, format) PSTR(TRACE_LOG_COLOR_##letter "[%6lu][" #letter "] %s" TRACE_LOG_RESET_COLOR "\r\n"), millis(), format #define TRACE_LOG_FORMAT(letter, format) PSTR(TRACE_LOG_COLOR_##letter "[%6lu][" #letter "] %s" TRACE_LOG_RESET_COLOR "\r\n"), millis(), format
static Stream * _out = nullptr; // vystupni stream
static void (*message_cb)(const char *) = nullptr; static void (*message_cb)(const char *) = nullptr;
static void print(const uint8_t severity, const char * buffer, const int length) { void TraceStream::print(const uint8_t severity, const char * buffer, const int length) {
if (_out) { if (mpOut) {
if (severity == TRACE_ERROR) if (severity == TRACE_ERROR)
_out->printf_P(TRACE_LOG_FORMAT(E, buffer)); mpOut->printf_P(TRACE_LOG_FORMAT(E, buffer));
else if (severity == TRACE_DEBUG) else if (severity == TRACE_DEBUG)
_out->printf_P(TRACE_LOG_FORMAT(D, buffer)); mpOut->printf_P(TRACE_LOG_FORMAT(D, buffer));
else if (severity == TRACE_WARNING) else if (severity == TRACE_WARNING)
_out->printf_P(TRACE_LOG_FORMAT(W, buffer)); mpOut->printf_P(TRACE_LOG_FORMAT(W, buffer));
else if (severity == TRACE_INFO) else if (severity == TRACE_INFO)
_out->printf_P(TRACE_LOG_FORMAT(I, buffer)); mpOut->printf_P(TRACE_LOG_FORMAT(I, buffer));
else if (severity == TRACE_VERBOSE) else if (severity == TRACE_VERBOSE)
_out->printf_P(TRACE_LOG_FORMAT(V, buffer)); mpOut->printf_P(TRACE_LOG_FORMAT(V, buffer));
} }
} }
void trace_init() { void TraceStream::init() {
} }
void trace_clear() { void TraceStream::print(const uint8_t severity, const __FlashStringHelper * fmt, ...) {
}
void trace_end() {
trace_clear();
}
void trace_print(const uint8_t severity, const __FlashStringHelper * fmt, ...) {
char buffer[MAX_LINE_LEN + 1]; char buffer[MAX_LINE_LEN + 1];
va_list args; va_list args;
@ -82,7 +72,7 @@ void trace_print(const uint8_t severity, const __FlashStringHelper * fmt, ...) {
print(severity, buffer, length); print(severity, buffer, length);
} }
void trace_print(const uint8_t severity, const char * fmt, ...) { void TraceStream::print(const uint8_t severity, const char * fmt, ...) {
char buffer[MAX_LINE_LEN + 1]; char buffer[MAX_LINE_LEN + 1];
va_list args; va_list args;
@ -98,7 +88,7 @@ void trace_registermessagecb(void (*cb)(const char *)) {
message_cb = cb; message_cb = cb;
} }
static char hexascii(uint8_t n) { char TraceStream::hexascii(uint8_t n) {
n &= 0xf; n &= 0xf;
if (n > 9) if (n > 9)
@ -107,7 +97,7 @@ static char hexascii(uint8_t n) {
return n + '0'; return n + '0';
} }
void trace_dump(const uint8_t severity, const char * prefix, const uint8_t * address, size_t size) { void TraceStream::dump(const uint8_t severity, const char * prefix, const uint8_t * address, size_t size) {
char buffer[MAX_LINE_LEN + 1 + 3]; char buffer[MAX_LINE_LEN + 1 + 3];
int idx = 0; int idx = 0;
@ -128,7 +118,7 @@ void trace_dump(const uint8_t severity, const char * prefix, const uint8_t * add
print(severity, buffer, idx); print(severity, buffer, idx);
} }
void trace_dump(const uint8_t severity, const __FlashStringHelper * prefix, const uint8_t * address, size_t size) { void TraceStream::dump(const uint8_t severity, const __FlashStringHelper * prefix, const uint8_t * address, size_t size) {
char buffer[MAX_LINE_LEN + 1 + 3]; char buffer[MAX_LINE_LEN + 1 + 3];
int idx = 0; int idx = 0;
@ -153,8 +143,4 @@ void trace_dump(const uint8_t severity, const __FlashStringHelper * prefix, cons
print(severity, buffer, idx); print(severity, buffer, idx);
} }
void trace_setstream(Stream * out) {
_out = out;
}
// EOF // EOF

View File

@ -0,0 +1,54 @@
/**
* @file traceStream.hpp
* @author Pavel Brychta, http://www.xpablo.cz
*
* Copyright (c) 2016-24 Pavel Brychta. All rights reserved.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#pragma once
#include "trace.h"
class TraceStream final : public Trace {
protected:
Stream * mpOut = nullptr; // output stream
static char hexascii(uint8_t n);
void print(uint8_t severity, const char * buffer, int length);
public:
TraceStream() = default;
explicit TraceStream(Stream * out)
: mpOut(out) {
}
void init() override;
void print(uint8_t severity, const __FlashStringHelper * fmt, ...) override;
void print(uint8_t severity, const char * fmt, ...) override;
void dump(uint8_t severity, const char * prefix, const uint8_t * address, size_t size) override;
void dump(uint8_t severity, const __FlashStringHelper * prefix, const uint8_t * address, size_t size) override;
void setOutput(Stream * out) {
mpOut = out;
}
};