Compare commits
24 Commits
Author | SHA1 | Date | |
---|---|---|---|
4c6627a4bd | |||
8b172701d2 | |||
a34d7cd0c5 | |||
7bcecdcd2b | |||
dc1c87cbf3 | |||
8f6521f761 | |||
b2d4fcdc4c | |||
b240ceca67 | |||
be3f935575 | |||
11fcc3546c | |||
67a93dbeab | |||
9f29c8b1b1 | |||
14492ac2d0 | |||
b15ce02b2a | |||
eb51464692 | |||
92dd05d0ac | |||
d9c4fb7cd8 | |||
59ae6814a1 | |||
d616088f69 | |||
542675ef2c | |||
9a8b81ecdd | |||
76d19c8ccd | |||
031da04f36 | |||
82ef0d54dd |
14
README.md
14
README.md
@ -1,3 +1,15 @@
|
||||
# Trace
|
||||
|
||||
Knihovna Trace (Stopař) pro ESP8266
|
||||
Knihovna Trace (Stopař) pro ESP8266/ESP32.
|
||||
|
||||
![Screenshot](doc/traceSerial.png)
|
||||
|
||||
## Konfigurace
|
||||
|
||||
Konfiguracni makra jsou:
|
||||
|
||||
Logovani do weboveho soketu ma prioritu pred logovanim na seriovy port!
|
||||
|
||||
* `-DTRACE_USE_ASYNC_WEBSOCKET` - TRACE vystup pouziva webovy soket v ESPAsyncWebServer knihovne
|
||||
* `-DTRACE_LOG_COLORS` - pro ESP8266 a `DEBUG_ESP_PORT` umoznuje barevne zvyraznene logovani na seriovy port.
|
||||
* `-DTRACE_LIMIT=TRACE_VERBOSE` - umozni nastavit uroven logovani
|
BIN
doc/traceSerial.png
Normal file
BIN
doc/traceSerial.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 37 KiB |
14
examples/ttest/ttest.ino
Normal file
14
examples/ttest/ttest.ino
Normal file
@ -0,0 +1,14 @@
|
||||
#include <trace.h>
|
||||
|
||||
void setup(void)
|
||||
{
|
||||
Serial.begin(115200);
|
||||
TRACE(TRACE_INFO, F("Setup..."));
|
||||
}
|
||||
|
||||
void loop(void)
|
||||
{
|
||||
|
||||
TRACE(TRACE_INFO, F("loop..."));
|
||||
delay(1000);
|
||||
}
|
10
library.json
10
library.json
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name":"Trace",
|
||||
"description":"Web based trace debug module",
|
||||
"description":"Multi-target trace debug module",
|
||||
"keywords":"debug,trace",
|
||||
"authors":
|
||||
{
|
||||
@ -10,13 +10,13 @@
|
||||
"repository":
|
||||
{
|
||||
"type": "git",
|
||||
"url": "http://git.xpablo.cz/pablo2048/Trace.git"
|
||||
"url": "https://git.xpablo.cz/pablo2048/Trace.git"
|
||||
},
|
||||
"version": "0.0.2",
|
||||
"version": "1.1.0",
|
||||
"license": "MIT",
|
||||
"frameworks": "arduino",
|
||||
"platforms": "espressif8266",
|
||||
"platforms": ["espressif8266","espressif32"],
|
||||
"build": {
|
||||
"libCompatMode": 2
|
||||
"libCompatMode": "strict"
|
||||
}
|
||||
}
|
||||
|
@ -1,9 +1,9 @@
|
||||
name=Trace
|
||||
version=0.0.1
|
||||
version=1.1.0
|
||||
author=Pavel Brychta
|
||||
maintainer=Pavel Brychta
|
||||
sentence=Make timing by using intervals instead of delay()
|
||||
paragraph=Make timing by using intervals instead of delay()
|
||||
sentence=Trace application internals & messages via web server or other output device
|
||||
paragraph=Trace application internals & messages via web server or other output device
|
||||
category=Other
|
||||
url=http://git.xpablo.cz/pablo2048/Trace
|
||||
architectures=*
|
||||
|
293
src/trace.cpp
293
src/trace.cpp
@ -1,293 +0,0 @@
|
||||
#include "trace.h"
|
||||
#include <interval.h>
|
||||
|
||||
#define MAX_TRACE_LINES 15
|
||||
#define MAX_LINE_LEN 50
|
||||
|
||||
struct TraceLine
|
||||
{
|
||||
char _text[MAX_LINE_LEN + 1];
|
||||
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(void) {}
|
||||
};
|
||||
|
||||
static TraceLine _lines[MAX_TRACE_LINES];
|
||||
static uint16_t _lines_index = 0;
|
||||
static uint16_t _lines_count = 0;
|
||||
static int _modified = 0;
|
||||
static AsyncWebSocket *_wss = NULL; // webovy soket pro trasovani
|
||||
static Interval _tint; // interval pro casovani stopare
|
||||
|
||||
static void (*message_cb)(const char *) = NULL;
|
||||
|
||||
static int modulo(int a, int b)
|
||||
{
|
||||
int r = a % b;
|
||||
|
||||
return ((r < 0) ? r + b : r);
|
||||
}
|
||||
|
||||
static TraceLine &trace_line(uint16_t index)
|
||||
{
|
||||
int start = _lines_index - _lines_count;
|
||||
int idx = modulo(start + index, _lines_count);
|
||||
|
||||
return (_lines[idx]);
|
||||
}
|
||||
|
||||
static void print(uint8_t severity, const char *buffer, int length)
|
||||
{
|
||||
char lin[MAX_LINE_LEN + 1];
|
||||
int lineptr = 0;
|
||||
|
||||
while ((0 != *buffer) && (lineptr < (sizeof(lin) - 1)))
|
||||
{
|
||||
if (*buffer > 0x1f)
|
||||
lin[lineptr] = *buffer;
|
||||
else
|
||||
lin[lineptr] = '?';
|
||||
++lineptr;
|
||||
++buffer;
|
||||
}
|
||||
lin[lineptr] = 0; // ukoncime retezec
|
||||
|
||||
TraceLine line(severity, lin, lineptr);
|
||||
|
||||
_lines[_lines_index++] = line;
|
||||
_lines_index %= MAX_TRACE_LINES;
|
||||
if (_lines_count < MAX_TRACE_LINES)
|
||||
{
|
||||
++_lines_count;
|
||||
}
|
||||
++_modified;
|
||||
}
|
||||
|
||||
void trace_init(void)
|
||||
{
|
||||
|
||||
trace_print(TRACE_INFO, F("Trace: Starting..."));
|
||||
}
|
||||
|
||||
static String _getText(const char *buffer)
|
||||
{
|
||||
String res;
|
||||
|
||||
res.reserve(MAX_LINE_LEN * 4);
|
||||
|
||||
while (0 != *buffer)
|
||||
{
|
||||
switch (*buffer)// uprava escape sekvenci pro JSON/HTML
|
||||
{
|
||||
case '"':
|
||||
res.concat(F("\\\""));
|
||||
break;
|
||||
|
||||
case '\\':
|
||||
res.concat(F("\\\\"));
|
||||
break;
|
||||
|
||||
case '/':
|
||||
res.concat(F("\\/"));
|
||||
break;
|
||||
|
||||
case '<':
|
||||
res.concat(F("<"));
|
||||
break;
|
||||
|
||||
case '>':
|
||||
res.concat(F(">"));
|
||||
break;
|
||||
|
||||
default:
|
||||
if (*buffer < 128)
|
||||
res.concat(*buffer);
|
||||
else
|
||||
{
|
||||
char chr[3];
|
||||
res.concat(F("\\u00"));
|
||||
sprintf_P(chr, PSTR("%02X"), *buffer);
|
||||
res.concat(chr);
|
||||
}
|
||||
break;
|
||||
}
|
||||
++buffer;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
void trace_dumpJSON(String &str)
|
||||
{
|
||||
|
||||
for (int i=0; i<_lines_count; i++)
|
||||
{
|
||||
TraceLine line = trace_line(i);
|
||||
|
||||
if (0 != i)
|
||||
str.concat(F(","));
|
||||
str.concat(F("{\"t\":"));
|
||||
str.concat(line._time);
|
||||
str.concat(F(",\"s\":"));
|
||||
str.concat(line._severity);
|
||||
str.concat(F(",\"d\":\""));
|
||||
str.concat(_getText(line._text));
|
||||
str.concat(F("\"}"));
|
||||
}
|
||||
}
|
||||
|
||||
void trace_clear(void)
|
||||
{
|
||||
|
||||
_lines_index = 0;
|
||||
_lines_count = 0;
|
||||
_modified = 1;
|
||||
}
|
||||
|
||||
void trace_end(void)
|
||||
{
|
||||
|
||||
trace_clear();
|
||||
}
|
||||
|
||||
void trace_print(uint8_t severity, const __FlashStringHelper *fmt, ...)
|
||||
{
|
||||
char buffer[MAX_LINE_LEN + 1];
|
||||
va_list args;
|
||||
int length;
|
||||
|
||||
va_start(args, fmt);
|
||||
length = vsnprintf_P(buffer, sizeof (buffer), (const char *)fmt, args);
|
||||
va_end(args);
|
||||
|
||||
print(severity, buffer, length);
|
||||
}
|
||||
|
||||
void trace_print(uint8_t severity, const char *fmt, ...)
|
||||
{
|
||||
char buffer[MAX_LINE_LEN + 1];
|
||||
va_list args;
|
||||
int length;
|
||||
|
||||
va_start(args, fmt);
|
||||
length = vsnprintf(buffer, sizeof(buffer), fmt, args);
|
||||
va_end(args);
|
||||
|
||||
print(severity, buffer, length);
|
||||
}
|
||||
|
||||
void trace_registermessagecb(void (*cb)(const char *))
|
||||
{
|
||||
|
||||
message_cb = cb;
|
||||
}
|
||||
|
||||
void trace_addweb(AsyncWebSocket *socket)
|
||||
{
|
||||
|
||||
_wss = socket;
|
||||
}
|
||||
|
||||
static char hexascii(uint8_t n)
|
||||
{
|
||||
|
||||
n &= 0xf;
|
||||
if (n > 9)
|
||||
return n + ('A' - 10);
|
||||
else
|
||||
return n + '0';
|
||||
}
|
||||
|
||||
void trace_dump(uint8_t severity, const char *prefix, uint8_t *address, size_t size)
|
||||
{
|
||||
char buffer[MAX_LINE_LEN + 1 + 3];
|
||||
int idx = 0;
|
||||
|
||||
if (prefix)
|
||||
idx = snprintf_P(buffer, MAX_LINE_LEN, PSTR("%s"), prefix);
|
||||
|
||||
while ((idx < MAX_LINE_LEN) && size)
|
||||
{
|
||||
buffer[idx] = hexascii(*address >> 4);
|
||||
++idx;
|
||||
buffer[idx] = hexascii(*address);
|
||||
++idx;
|
||||
buffer[idx] = 0x20;
|
||||
++idx;
|
||||
++address;
|
||||
--size;
|
||||
}
|
||||
buffer[idx] = 0;
|
||||
print(severity, buffer, idx);
|
||||
}
|
||||
|
||||
// TODO: POZOR!!! tady si nejsem jisty, zda je spravne pouziti __FlashStringHelperu v snprintf_P !!!!
|
||||
void trace_dump(uint8_t severity, const __FlashStringHelper *prefix, uint8_t *address, size_t size)
|
||||
{
|
||||
char buffer[MAX_LINE_LEN + 1 + 3];
|
||||
int idx = 0;
|
||||
|
||||
if (prefix)
|
||||
idx = snprintf_P(buffer, MAX_LINE_LEN, PSTR("%s"), prefix);
|
||||
|
||||
while ((idx < MAX_LINE_LEN) && size)
|
||||
{
|
||||
buffer[idx] = hexascii(*address >> 4);
|
||||
++idx;
|
||||
buffer[idx] = hexascii(*address);
|
||||
++idx;
|
||||
buffer[idx] = 0x20;
|
||||
++idx;
|
||||
++address;
|
||||
--size;
|
||||
}
|
||||
buffer[idx] = 0;
|
||||
print(severity, buffer, idx);
|
||||
}
|
||||
|
||||
void trace_forceupdate(void)
|
||||
{
|
||||
|
||||
++_modified; // vynutime odeslani informaci
|
||||
}
|
||||
|
||||
void trace_poll()
|
||||
{
|
||||
|
||||
if (NULL != _wss)
|
||||
{ // je definovany webovy soket
|
||||
if (_modified)
|
||||
{ // mame nejakou zmenu
|
||||
if (_wss->count() != 0)
|
||||
{ // mame klienty
|
||||
if (_tint.expired())
|
||||
{ // .. a vyprsel timeout pro obcerstvovani
|
||||
String log;
|
||||
|
||||
if (log.reserve((MAX_TRACE_LINES * MAX_LINE_LEN) + (MAX_TRACE_LINES * 50)))
|
||||
{
|
||||
log = F("{\"type\":\"trace\",\"data\":[");
|
||||
trace_dumpJSON(log);
|
||||
log.concat(F("]}"));
|
||||
_wss->textAll(log);
|
||||
}
|
||||
else
|
||||
_wss->textAll(F("{\"type\":\"trace\",\"text\":\"Memory error\"}"));
|
||||
_modified = 0; // rusime pozadavek na odeslani novych dat
|
||||
_tint.set(TRACE_CHECK_INTERVAL);
|
||||
}
|
||||
}
|
||||
else
|
||||
_modified = 0; // zadny pripojeny klient - po pripojeni stejne musime vyzadat stav, takze ted muzeme modifikaci klidne ignorovat
|
||||
}
|
||||
}
|
||||
}
|
||||
// EOF
|
126
src/trace.h
126
src/trace.h
@ -2,7 +2,7 @@
|
||||
* @file trace.h
|
||||
* @author Pavel Brychta, http://www.xpablo.cz
|
||||
*
|
||||
* Copyright (c) 2016-18 Pavel Brychta. All rights reserved.
|
||||
* 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
|
||||
@ -19,105 +19,65 @@
|
||||
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*
|
||||
*/
|
||||
#ifndef _TRACE_H_
|
||||
#define _TRACE_H_
|
||||
|
||||
#pragma once
|
||||
#include <Arduino.h>
|
||||
#include <ESPAsyncWebServer.h>
|
||||
|
||||
// Vydefinujeme-li promennou TRACE_ALL=1, pak TRACEPLUS bude vypisovat vsechny zpravy nezavisle od povoleni/zakazani
|
||||
|
||||
// Definice jednotlivych typu hlaseni do trasovani
|
||||
#define TRACE_NONE (-1) // nic
|
||||
#define TRACE_ERROR 0 // chybova zprava = cervena
|
||||
#define TRACE_WARNING 1 // varovani - zluta
|
||||
#define TRACE_INFO 2 // informacni zprava - zelena
|
||||
#define TRACE_DEBUG 3 // ladici zprava - cerna
|
||||
#define TRACE_DEBUGMORE 4 // mene zajimava ladici zprava - take cerna
|
||||
#define TRACE_VERBOSE 4 // mene zajimava ladici zprava - take cerna
|
||||
|
||||
#define TRACE_CHECK_INTERVAL 200 // interval [ms], po kterem je testovano odesilani stopare
|
||||
#ifndef TRACE_LIMIT
|
||||
#define TRACE_LIMIT TRACE_DEBUG
|
||||
#endif
|
||||
#define STRINGIFY(x) #x
|
||||
#define TOSTRING(x) STRINGIFY(x)
|
||||
|
||||
/**
|
||||
* @brief Inicializace modulu
|
||||
*/
|
||||
void trace_init(void);
|
||||
class Trace {
|
||||
protected:
|
||||
public:
|
||||
Trace() = default;
|
||||
|
||||
/**
|
||||
* @brief Ziskani vypisu v JSON formatu
|
||||
*
|
||||
* @param str retezec udalosti v JSON
|
||||
*/
|
||||
void trace_dumpJSON(String &str);
|
||||
virtual ~Trace() = default;
|
||||
|
||||
/**
|
||||
* @brief Vyprazdneni stopovaciho bufferu
|
||||
*/
|
||||
void trace_clear(void);
|
||||
virtual void init() {};
|
||||
|
||||
/**
|
||||
* @brief Ukonceni prace stopare - vyprazdni buffer
|
||||
*/
|
||||
void trace_end(void);
|
||||
virtual void print(uint8_t severity, const __FlashStringHelper * fmt, ...) {};
|
||||
|
||||
/**
|
||||
* @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, ...);
|
||||
virtual void print(uint8_t severity, const char * 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, ...);
|
||||
virtual void dump(uint8_t severity, const char * prefix, const uint8_t * address, size_t size) {};
|
||||
|
||||
/**
|
||||
* @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));
|
||||
virtual void dump(uint8_t severity, const __FlashStringHelper * prefix, const uint8_t * address, size_t size) {};
|
||||
|
||||
/**
|
||||
* @brief Pridani obsluhy stranky /trace do weboveho serveru
|
||||
*
|
||||
* @param socket webovy soket pro komunikaci
|
||||
*/
|
||||
void trace_addweb(AsyncWebSocket *socket);
|
||||
virtual void forceupdate() {};
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Periodicky tik modulem - osetruje odeslani novych dat v pripade modifikace
|
||||
*/
|
||||
void trace_poll();
|
||||
extern Trace * mTrace; // Main Trace instance
|
||||
|
||||
void trace_printfunc(uint8_t severity, const char *func, const char *file, int line, const char *fmt, ...);
|
||||
#define TRACE(severity, text, ...) \
|
||||
do { \
|
||||
constexpr bool trace_limit = (severity <= TRACE_LIMIT); \
|
||||
if (trace_limit) \
|
||||
if (mTrace) { mTrace->print(severity, PSTR(text), ##__VA_ARGS__); } \
|
||||
} while (0)
|
||||
#define TRACEFUNC(severity, ...) if (mTrace) { mTrace->printfunc(severity, __func__, __FILE__, TOSTRING(__LINE__), __VA_ARGS__); }
|
||||
#define TRACEDUMP(severity, prefix, address, size) if (mTrace) { mTrace->dump(severity, prefix, address, size); }
|
||||
#define TRACE_INIT(a) if (mTrace) { mTrace->init(); }
|
||||
#define TRACE_FORCEUPDATE(a) if (mTrace) { mTrace->forceupdate(); }
|
||||
|
||||
/**
|
||||
* @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);
|
||||
#ifndef TRACE_ALL
|
||||
#define TRACE_ALL 0
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @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(void);
|
||||
|
||||
#endif // _TRACE_H_
|
||||
#define TRACEPLUS(when, severity, ...) \
|
||||
do { \
|
||||
constexpr bool handle = (when != 0) || TRACE_ALL; \
|
||||
if (handle) \
|
||||
TRACE(severity, __VA_ARGS__); \
|
||||
} while (0)
|
||||
|
227
src/traceAsyncWebSocket/traceAsyncWebsocket.cpp
Normal file
227
src/traceAsyncWebSocket/traceAsyncWebsocket.cpp
Normal file
@ -0,0 +1,227 @@
|
||||
#include "traceAsyncWebsocket.hpp"
|
||||
#include <Chronos.hpp>
|
||||
|
||||
|
||||
int TraceAsyncWebsocket::modulo(int a, int b)
|
||||
{
|
||||
int r = a % b;
|
||||
|
||||
return ((r < 0) ? r + b : r);
|
||||
}
|
||||
|
||||
TraceLine & TraceAsyncWebsocket::tLine(const uint16_t index)
|
||||
{
|
||||
int start = _lines_index - _lines_count;
|
||||
int idx = modulo(start + index, _lines_count);
|
||||
|
||||
return (_lines[idx]);
|
||||
}
|
||||
|
||||
void TraceAsyncWebsocket::print(uint8_t severity, const char * buffer, int length)
|
||||
{
|
||||
char lin[MAX_LINE_LEN + 1];
|
||||
unsigned int lineptr = 0;
|
||||
|
||||
while ((0 != *buffer) && (lineptr < (sizeof(lin) - 1))) {
|
||||
if (*buffer > 0x1f)
|
||||
lin[lineptr] = *buffer;
|
||||
else
|
||||
lin[lineptr] = '?';
|
||||
++lineptr;
|
||||
++buffer;
|
||||
}
|
||||
lin[lineptr] = 0; // ukoncime retezec
|
||||
|
||||
TraceLine line(severity, lin, lineptr);
|
||||
|
||||
_lines[_lines_index++] = line;
|
||||
_lines_index %= MAX_TRACE_LINES;
|
||||
if (_lines_count < MAX_TRACE_LINES) {
|
||||
++_lines_count;
|
||||
}
|
||||
_modified = _modified + 1;
|
||||
}
|
||||
|
||||
void TraceAsyncWebsocket::loop()
|
||||
{
|
||||
|
||||
if (_wss) {
|
||||
// je definovany webovy soket
|
||||
if (_modified) {
|
||||
// mame nejakou zmenu
|
||||
if (_wss->count() != 0) {
|
||||
// mame klienty
|
||||
String log;
|
||||
|
||||
_modified = 0; // rusime pozadavek na odeslani novych dat
|
||||
if (log.reserve((MAX_TRACE_LINES * MAX_LINE_LEN) + (MAX_TRACE_LINES * 50))) {
|
||||
log = F("{\"type\":\"trace\",\"data\":[");
|
||||
dumpJSON(log);
|
||||
log.concat(F("]}"));
|
||||
if (_wss->availableForWriteAll())
|
||||
_wss->textAll(log);
|
||||
} else if (_wss->availableForWriteAll())
|
||||
_wss->textAll(F("{\"type\":\"trace\",\"text\":\"Memory error\"}"));
|
||||
} else
|
||||
_modified = 0; // zadny pripojeny klient - po pripojeni stejne musime vyzadat stav, takze ted muzeme modifikaci klidne ignorovat
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void TraceAsyncWebsocket::init()
|
||||
{
|
||||
|
||||
Chronos::attachMs(TRACEWS_CHECK_INTERVAL, this);
|
||||
}
|
||||
|
||||
String TraceAsyncWebsocket::_getText(const char * buffer)
|
||||
{
|
||||
String res;
|
||||
|
||||
res.reserve(MAX_LINE_LEN * 4);
|
||||
|
||||
while (0 != *buffer) {
|
||||
switch (*buffer) { // uprava escape sekvenci pro JSON/HTML
|
||||
case '"':
|
||||
res.concat(F("\\\""));
|
||||
break;
|
||||
|
||||
case '\\':
|
||||
res.concat(F("\\\\"));
|
||||
break;
|
||||
|
||||
case '/':
|
||||
res.concat(F("\\/"));
|
||||
break;
|
||||
|
||||
case '<':
|
||||
res.concat(F("<"));
|
||||
break;
|
||||
|
||||
case '>':
|
||||
res.concat(F(">"));
|
||||
break;
|
||||
|
||||
default:
|
||||
if (*buffer < 128)
|
||||
res.concat(*buffer);
|
||||
else {
|
||||
char chr[3];
|
||||
res.concat(F("\\u00"));
|
||||
sprintf_P(chr, PSTR("%02X"), *buffer);
|
||||
res.concat(chr);
|
||||
}
|
||||
break;
|
||||
}
|
||||
++buffer;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
void TraceAsyncWebsocket::dumpJSON(String & str)
|
||||
{
|
||||
|
||||
for (int i = 0; i < _lines_count; i++) {
|
||||
TraceLine line = tLine(i);
|
||||
|
||||
if (0 != i)
|
||||
str.concat(F(","));
|
||||
str.concat(F("{\"t\":"));
|
||||
str.concat(line._time);
|
||||
str.concat(F(",\"s\":"));
|
||||
str.concat(line._severity);
|
||||
str.concat(F(",\"d\":\""));
|
||||
str.concat(_getText(line._text));
|
||||
str.concat(F("\"}"));
|
||||
}
|
||||
}
|
||||
|
||||
void TraceAsyncWebsocket::print(uint8_t severity, const __FlashStringHelper * fmt, ...)
|
||||
{
|
||||
char buffer[MAX_LINE_LEN + 1];
|
||||
va_list args;
|
||||
|
||||
va_start(args, fmt);
|
||||
int length = vsnprintf_P(buffer, sizeof(buffer), (const char *) fmt, args);
|
||||
va_end(args);
|
||||
|
||||
print(severity, buffer, length);
|
||||
}
|
||||
|
||||
void TraceAsyncWebsocket::print(uint8_t severity, const char * fmt, ...)
|
||||
{
|
||||
char buffer[MAX_LINE_LEN + 1];
|
||||
va_list args;
|
||||
|
||||
va_start(args, fmt);
|
||||
int length = vsnprintf(buffer, sizeof(buffer), fmt, args);
|
||||
va_end(args);
|
||||
|
||||
print(severity, buffer, length);
|
||||
}
|
||||
|
||||
char TraceAsyncWebsocket::hexascii(uint8_t n)
|
||||
{
|
||||
|
||||
n &= 0xf;
|
||||
if (n > 9)
|
||||
return n + ('A' - 10);
|
||||
else
|
||||
return n + '0';
|
||||
}
|
||||
|
||||
void TraceAsyncWebsocket::dump(uint8_t severity, const char * prefix, const uint8_t * address, size_t size)
|
||||
{
|
||||
char buffer[MAX_LINE_LEN + 1 + 3];
|
||||
int idx = 0;
|
||||
|
||||
if (prefix)
|
||||
idx = snprintf_P(buffer, MAX_LINE_LEN, PSTR("%s"), prefix);
|
||||
|
||||
while ((idx < MAX_LINE_LEN) && size) {
|
||||
buffer[idx] = hexascii(*address >> 4);
|
||||
++idx;
|
||||
buffer[idx] = hexascii(*address);
|
||||
++idx;
|
||||
buffer[idx] = 0x20;
|
||||
++idx;
|
||||
++address;
|
||||
--size;
|
||||
}
|
||||
buffer[idx] = 0;
|
||||
print(severity, buffer, idx);
|
||||
}
|
||||
|
||||
void TraceAsyncWebsocket::dump(uint8_t severity, const __FlashStringHelper * prefix, const uint8_t * address, size_t size)
|
||||
{
|
||||
char buffer[MAX_LINE_LEN + 1 + 3];
|
||||
int idx = 0;
|
||||
|
||||
if (prefix) {
|
||||
char dummy[64];
|
||||
|
||||
strcpy_P(dummy, (const char *) prefix);
|
||||
idx = snprintf_P(buffer, MAX_LINE_LEN, PSTR("%s"), dummy);
|
||||
}
|
||||
|
||||
while ((idx < MAX_LINE_LEN) && size) {
|
||||
buffer[idx] = hexascii(*address >> 4);
|
||||
++idx;
|
||||
buffer[idx] = hexascii(*address);
|
||||
++idx;
|
||||
buffer[idx] = 0x20;
|
||||
++idx;
|
||||
++address;
|
||||
--size;
|
||||
}
|
||||
buffer[idx] = 0;
|
||||
print(severity, buffer, idx);
|
||||
}
|
||||
|
||||
void TraceAsyncWebsocket::forceupdate()
|
||||
{
|
||||
|
||||
_modified = _modified + 1; // vynutime odeslani informaci
|
||||
}
|
||||
|
||||
// EOF
|
85
src/traceAsyncWebSocket/traceAsyncWebsocket.hpp
Normal file
85
src/traceAsyncWebSocket/traceAsyncWebsocket.hpp
Normal 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;
|
||||
};
|
146
src/traceStream/traceStream.cpp
Normal file
146
src/traceStream/traceStream.cpp
Normal file
@ -0,0 +1,146 @@
|
||||
#include "traceStream.hpp"
|
||||
#include "../trace.h"
|
||||
#include <Chronos.hpp>
|
||||
|
||||
#define MAX_LINE_LEN 128
|
||||
|
||||
#ifdef 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 "%s" TRACE_LOG_RESET_COLOR "\r\n"), format
|
||||
#define TRACE_LOG_FORMAT(letter, format) PSTR(TRACE_LOG_COLOR_##letter "[%8lu][" #letter "] %s" TRACE_LOG_RESET_COLOR "\r\n"), millis(), format
|
||||
|
||||
static void (*message_cb)(const char *) = nullptr;
|
||||
|
||||
void TraceStream::print(const uint8_t severity, const char * buffer, const int length) {
|
||||
|
||||
if (mpOut) {
|
||||
if (severity == TRACE_ERROR)
|
||||
mpOut->printf_P(TRACE_LOG_FORMAT(E, buffer));
|
||||
else if (severity == TRACE_DEBUG)
|
||||
mpOut->printf_P(TRACE_LOG_FORMAT(D, buffer));
|
||||
else if (severity == TRACE_WARNING)
|
||||
mpOut->printf_P(TRACE_LOG_FORMAT(W, buffer));
|
||||
else if (severity == TRACE_INFO)
|
||||
mpOut->printf_P(TRACE_LOG_FORMAT(I, buffer));
|
||||
else if (severity == TRACE_VERBOSE)
|
||||
mpOut->printf_P(TRACE_LOG_FORMAT(V, buffer));
|
||||
}
|
||||
}
|
||||
|
||||
void TraceStream::init() {
|
||||
}
|
||||
|
||||
void TraceStream::print(const uint8_t severity, const __FlashStringHelper * fmt, ...) {
|
||||
char buffer[MAX_LINE_LEN + 1];
|
||||
va_list args;
|
||||
|
||||
va_start(args, fmt);
|
||||
const int length = vsnprintf_P(buffer, sizeof(buffer), (const char *) fmt, args);
|
||||
va_end(args);
|
||||
|
||||
print(severity, buffer, length);
|
||||
}
|
||||
|
||||
void TraceStream::print(const uint8_t severity, const char * fmt, ...) {
|
||||
char buffer[MAX_LINE_LEN + 1];
|
||||
va_list args;
|
||||
|
||||
va_start(args, fmt);
|
||||
const int length = vsnprintf(buffer, sizeof(buffer), fmt, args);
|
||||
va_end(args);
|
||||
|
||||
print(severity, buffer, length);
|
||||
}
|
||||
|
||||
void trace_registermessagecb(void (*cb)(const char *)) {
|
||||
|
||||
message_cb = cb;
|
||||
}
|
||||
|
||||
char TraceStream::hexascii(uint8_t n) {
|
||||
|
||||
n &= 0xf;
|
||||
if (n > 9)
|
||||
return n + ('A' - 10);
|
||||
else
|
||||
return n + '0';
|
||||
}
|
||||
|
||||
void TraceStream::dump(const uint8_t severity, const char * prefix, const uint8_t * address, size_t size) {
|
||||
char buffer[MAX_LINE_LEN + 1 + 3];
|
||||
int idx = 0;
|
||||
|
||||
if (prefix)
|
||||
idx = snprintf_P(buffer, MAX_LINE_LEN, PSTR("%s"), prefix);
|
||||
|
||||
while ((idx < MAX_LINE_LEN) && size) {
|
||||
buffer[idx] = hexascii(*address >> 4);
|
||||
++idx;
|
||||
buffer[idx] = hexascii(*address);
|
||||
++idx;
|
||||
buffer[idx] = 0x20;
|
||||
++idx;
|
||||
++address;
|
||||
--size;
|
||||
}
|
||||
buffer[idx] = 0;
|
||||
print(severity, buffer, idx);
|
||||
}
|
||||
|
||||
void TraceStream::dump(const uint8_t severity, const __FlashStringHelper * prefix, const uint8_t * address, size_t size) {
|
||||
char buffer[MAX_LINE_LEN + 1 + 3];
|
||||
int idx = 0;
|
||||
|
||||
if (prefix) {
|
||||
char dummy[64];
|
||||
|
||||
strcpy_P(dummy, (const char *) prefix);
|
||||
idx = snprintf_P(buffer, MAX_LINE_LEN, PSTR("%s"), dummy);
|
||||
}
|
||||
|
||||
while ((idx < MAX_LINE_LEN) && size) {
|
||||
buffer[idx] = hexascii(*address >> 4);
|
||||
++idx;
|
||||
buffer[idx] = hexascii(*address);
|
||||
++idx;
|
||||
buffer[idx] = 0x20;
|
||||
++idx;
|
||||
++address;
|
||||
--size;
|
||||
}
|
||||
buffer[idx] = 0;
|
||||
print(severity, buffer, idx);
|
||||
}
|
||||
|
||||
// EOF
|
54
src/traceStream/traceStream.hpp
Normal file
54
src/traceStream/traceStream.hpp
Normal 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;
|
||||
}
|
||||
};
|
Loading…
Reference in New Issue
Block a user