14 Commits

5 changed files with 227 additions and 197 deletions

View File

@ -10,13 +10,13 @@
"repository": "repository":
{ {
"type": "git", "type": "git",
"url": "http://git.xpablo.cz/pablo2048/Trace.git" "url": "https://git.xpablo.cz/pablo2048/Trace.git"
}, },
"version": "0.0.2", "version": "0.0.6",
"license": "MIT", "license": "MIT",
"frameworks": "arduino", "frameworks": "arduino",
"platforms": "espressif8266", "platforms": ["espressif8266","espressif32"],
"build": { "build": {
"libCompatMode": 2 "libCompatMode": "strict"
} }
} }

View File

@ -1,9 +1,9 @@
name=Trace name=Trace
version=0.0.1 version=0.0.6
author=Pavel Brychta author=Pavel Brychta
maintainer=Pavel Brychta maintainer=Pavel Brychta
sentence=Make timing by using intervals instead of delay() sentence=Trace application internals & messages via web server
paragraph=Make timing by using intervals instead of delay() paragraph=Trace application internals & messages via web server
category=Other category=Other
url=http://git.xpablo.cz/pablo2048/Trace url=http://git.xpablo.cz/pablo2048/Trace
architectures=* architectures=*

View File

@ -0,0 +1,16 @@
#include <trace.h>
#define TRACE(severity, fmt, ...) trace_print(severity, (PGM_P)PSTR(fmt), ## __VA_ARGS__)
#define TRACE_INIT trace_init()
#define TRACE_ADDWEB(srv) trace_addweb(srv)
#define TRACE_POLL trace_poll()
void setup(void)
{
}
void loop(void)
{
}

View File

@ -4,290 +4,281 @@
#define MAX_TRACE_LINES 15 #define MAX_TRACE_LINES 15
#define MAX_LINE_LEN 50 #define MAX_LINE_LEN 50
struct TraceLine struct TraceLine {
{ char _text[MAX_LINE_LEN + 1] = {0};
char _text[MAX_LINE_LEN + 1]; uint32_t _time{};
uint32_t _time; uint8_t _severity{};
uint8_t _severity;
TraceLine(uint8_t severity, const char *str, uint16_t len) TraceLine(uint8_t severity, const char *str, uint16_t len)
{ {
strncpy(_text, str, sizeof(_text)); strncpy(_text, str, sizeof(_text));
_text[MAX_LINE_LEN] = 0; _text[MAX_LINE_LEN] = 0;
_time = millis(); _time = millis();
_severity = severity; _severity = severity;
} }
TraceLine(void) {} TraceLine() = default;
}; };
static TraceLine _lines[MAX_TRACE_LINES]; static TraceLine _lines[MAX_TRACE_LINES];
static uint16_t _lines_index = 0; static uint16_t _lines_index = 0;
static uint16_t _lines_count = 0; static uint16_t _lines_count = 0;
static int _modified = 0; static volatile int _modified = 0;
static AsyncWebSocket *_wss = NULL; // webovy soket pro trasovani static AsyncWebSocket *_wss = nullptr; // webovy soket pro trasovani
static Interval _tint; // interval pro casovani stopare static Interval _tint(TRACE_CHECK_INTERVAL); // interval pro casovani stopare
static void (*message_cb)(const char *) = NULL; static void (*message_cb)(const char *) = nullptr;
static int modulo(int a, int b) 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) static TraceLine &trace_line(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);
return (_lines[idx]); return (_lines[idx]);
} }
static void print(uint8_t severity, const char *buffer, int length) static void 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;
while ((0 != *buffer) && (lineptr < (sizeof(lin) - 1))) while ((0 != *buffer) && (lineptr < (sizeof(lin) - 1))) {
{ if (*buffer > 0x1f)
if (*buffer > 0x1f) lin[lineptr] = *buffer;
lin[lineptr] = *buffer; else
else lin[lineptr] = '?';
lin[lineptr] = '?'; ++lineptr;
++lineptr; ++buffer;
++buffer; }
} lin[lineptr] = 0; // ukoncime retezec
lin[lineptr] = 0; // ukoncime retezec
TraceLine line(severity, lin, lineptr); TraceLine line(severity, lin, lineptr);
_lines[_lines_index++] = line; _lines[_lines_index++] = line;
_lines_index %= MAX_TRACE_LINES; _lines_index %= MAX_TRACE_LINES;
if (_lines_count < MAX_TRACE_LINES) if (_lines_count < MAX_TRACE_LINES) {
{ ++_lines_count;
++_lines_count; }
} ++_modified;
++_modified;
} }
void trace_init(void) void trace_init()
{ {
trace_print(TRACE_INFO, F("Trace: Starting..."));
} }
static String _getText(const char *buffer) static String _getText(const char *buffer)
{ {
String res; String res;
res.reserve(MAX_LINE_LEN * 4); res.reserve(MAX_LINE_LEN * 4);
while (0 != *buffer) while (0 != *buffer) {
{ switch (*buffer) { // uprava escape sekvenci pro JSON/HTML
switch (*buffer)// uprava escape sekvenci pro JSON/HTML case '"':
{ res.concat(F("\\\""));
case '"': break;
res.concat(F("\\\""));
break;
case '\\': case '\\':
res.concat(F("\\\\")); res.concat(F("\\\\"));
break; break;
case '/': case '/':
res.concat(F("\\/")); res.concat(F("\\/"));
break; break;
case '<': case '<':
res.concat(F("&lt;")); res.concat(F("&lt;"));
break; break;
case '>': case '>':
res.concat(F("&gt;")); res.concat(F("&gt;"));
break; break;
default: default:
if (*buffer < 128) if (*buffer < 128)
res.concat(*buffer); res.concat(*buffer);
else else {
{ char chr[3];
char chr[3]; res.concat(F("\\u00"));
res.concat(F("\\u00")); sprintf_P(chr, PSTR("%02X"), *buffer);
sprintf_P(chr, PSTR("%02X"), *buffer); res.concat(chr);
res.concat(chr); }
} break;
break; }
++buffer;
} }
++buffer; return res;
}
return res;
} }
void trace_dumpJSON(String &str) void trace_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 = trace_line(i);
if (0 != i) if (0 != i)
str.concat(F(",")); str.concat(F(","));
str.concat(F("{\"t\":")); str.concat(F("{\"t\":"));
str.concat(line._time); str.concat(line._time);
str.concat(F(",\"s\":")); str.concat(F(",\"s\":"));
str.concat(line._severity); str.concat(line._severity);
str.concat(F(",\"d\":\"")); str.concat(F(",\"d\":\""));
str.concat(_getText(line._text)); str.concat(_getText(line._text));
str.concat(F("\"}")); str.concat(F("\"}"));
} }
} }
void trace_clear(void) void trace_clear()
{ {
_lines_index = 0; _lines_index = 0;
_lines_count = 0; _lines_count = 0;
_modified = 1; _modified = 1;
} }
void trace_end(void) void trace_end()
{ {
trace_clear(); trace_clear();
} }
void trace_print(uint8_t severity, const __FlashStringHelper *fmt, ...) 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; int length;
va_start(args, fmt); va_start(args, fmt);
length = vsnprintf_P(buffer, sizeof (buffer), (const char *)fmt, args); 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 trace_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; int length;
va_start(args, fmt); va_start(args, fmt);
length = vsnprintf(buffer, sizeof(buffer), fmt, args); 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 *)) void trace_registermessagecb(void (*cb)(const char *))
{ {
message_cb = cb; message_cb = cb;
} }
void trace_addweb(AsyncWebSocket *socket) void trace_addweb(AsyncWebSocket *socket)
{ {
_wss = socket; _wss = socket;
} }
static char hexascii(uint8_t n) static char hexascii(uint8_t n)
{ {
n &= 0xf; n &= 0xf;
if (n > 9) if (n > 9)
return n + ('A' - 10); return n + ('A' - 10);
else else
return n + '0'; return n + '0';
} }
void trace_dump(uint8_t severity, const char *prefix, uint8_t *address, size_t size) void trace_dump(uint8_t severity, const char *prefix, 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;
if (prefix) if (prefix)
idx = snprintf_P(buffer, MAX_LINE_LEN, PSTR("%s"), prefix); idx = snprintf_P(buffer, MAX_LINE_LEN, PSTR("%s"), prefix);
while ((idx < MAX_LINE_LEN) && size) while ((idx < MAX_LINE_LEN) && size) {
{ buffer[idx] = hexascii(*address >> 4);
buffer[idx] = hexascii(*address >> 4); ++idx;
++idx; buffer[idx] = hexascii(*address);
buffer[idx] = hexascii(*address); ++idx;
++idx; buffer[idx] = 0x20;
buffer[idx] = 0x20; ++idx;
++idx; ++address;
++address; --size;
--size; }
} buffer[idx] = 0;
buffer[idx] = 0; print(severity, buffer, idx);
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) void trace_dump(uint8_t severity, const __FlashStringHelper *prefix, 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;
if (prefix) if (prefix) {
idx = snprintf_P(buffer, MAX_LINE_LEN, PSTR("%s"), prefix); char dummy[64];
while ((idx < MAX_LINE_LEN) && size) strcpy_P(dummy, (const char *)prefix);
{ idx = snprintf_P(buffer, MAX_LINE_LEN, PSTR("%s"), dummy);
buffer[idx] = hexascii(*address >> 4); }
++idx;
buffer[idx] = hexascii(*address); while ((idx < MAX_LINE_LEN) && size) {
++idx; buffer[idx] = hexascii(*address >> 4);
buffer[idx] = 0x20; ++idx;
++idx; buffer[idx] = hexascii(*address);
++address; ++idx;
--size; buffer[idx] = 0x20;
} ++idx;
buffer[idx] = 0; ++address;
print(severity, buffer, idx); --size;
}
buffer[idx] = 0;
print(severity, buffer, idx);
} }
void trace_forceupdate(void) void trace_forceupdate(void)
{ {
++_modified; // vynutime odeslani informaci ++_modified; // vynutime odeslani informaci
} }
void trace_poll() void trace_poll()
{ {
if (NULL != _wss) if (nullptr != _wss) {
{ // je definovany webovy soket // je definovany webovy soket
if (_modified) if (_modified) {
{ // mame nejakou zmenu // mame nejakou zmenu
if (_wss->count() != 0) if (_wss->count() != 0) {
{ // mame klienty // mame klienty
if (_tint.expired()) if (_tint.expired()) {
{ // .. a vyprsel timeout pro obcerstvovani // .. a vyprsel timeout pro obcerstvovani
String log; String log;
if (log.reserve((MAX_TRACE_LINES * MAX_LINE_LEN) + (MAX_TRACE_LINES * 50))) _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\":["); log = F("{\"type\":\"trace\",\"data\":[");
trace_dumpJSON(log); trace_dumpJSON(log);
log.concat(F("]}")); log.concat(F("]}"));
_wss->textAll(log); if (_wss->availableForWriteAll())
} _wss->textAll(log);
else } else
_wss->textAll(F("{\"type\":\"trace\",\"text\":\"Memory error\"}")); if (_wss->availableForWriteAll())
_modified = 0; // rusime pozadavek na odeslani novych dat _wss->textAll(F("{\"type\":\"trace\",\"text\":\"Memory error\"}"));
_tint.set(TRACE_CHECK_INTERVAL); }
} else
_modified = 0; // zadny pripojeny klient - po pripojeni stejne musime vyzadat stav, takze ted muzeme modifikaci klidne ignorovat
} }
}
else
_modified = 0; // zadny pripojeny klient - po pripojeni stejne musime vyzadat stav, takze ted muzeme modifikaci klidne ignorovat
} }
}
} }
// EOF // EOF

View File

@ -2,7 +2,7 @@
* @file trace.h * @file trace.h
* @author Pavel Brychta, http://www.xpablo.cz * @author Pavel Brychta, http://www.xpablo.cz
* *
* Copyright (c) 2016-18 Pavel Brychta. All rights reserved. * Copyright (c) 2016-23 Pavel Brychta. All rights reserved.
* *
* This library is free software; you can redistribute it and/or * This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public * modify it under the terms of the GNU Lesser General Public
@ -19,11 +19,7 @@
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
* *
*/ */
#ifndef _TRACE_H_ #pragma once
#define _TRACE_H_
#include <Arduino.h>
#include <ESPAsyncWebServer.h>
// Definice jednotlivych typu hlaseni do trasovani // Definice jednotlivych typu hlaseni do trasovani
#define TRACE_ERROR 0 // chybova zprava = cervena #define TRACE_ERROR 0 // chybova zprava = cervena
@ -34,10 +30,15 @@
#define TRACE_CHECK_INTERVAL 200 // interval [ms], po kterem je testovano odesilani stopare #define TRACE_CHECK_INTERVAL 200 // interval [ms], po kterem je testovano odesilani stopare
#ifndef DONT_USE_TRACE
#include <Arduino.h>
#include <ESPAsyncWebServer.h>
/** /**
* @brief Inicializace modulu * @brief Inicializace modulu
*/ */
void trace_init(void); void trace_init();
/** /**
* @brief Ziskani vypisu v JSON formatu * @brief Ziskani vypisu v JSON formatu
@ -49,12 +50,12 @@ void trace_dumpJSON(String &str);
/** /**
* @brief Vyprazdneni stopovaciho bufferu * @brief Vyprazdneni stopovaciho bufferu
*/ */
void trace_clear(void); void trace_clear();
/** /**
* @brief Ukonceni prace stopare - vyprazdni buffer * @brief Ukonceni prace stopare - vyprazdni buffer
*/ */
void trace_end(void); void trace_end();
/** /**
* @brief Ulozeni zpravy s obsahem z programove pameti (PROGMEM, F, ...) * @brief Ulozeni zpravy s obsahem z programove pameti (PROGMEM, F, ...)
@ -118,6 +119,28 @@ void trace_dump(uint8_t severity, const __FlashStringHelper *prefix, uint8_t *ad
/** /**
* @brief Vynuceni odeslani obsahu bufferu (napr. pri obnovovani spojeni) * @brief Vynuceni odeslani obsahu bufferu (napr. pri obnovovani spojeni)
*/ */
void trace_forceupdate(void); void trace_forceupdate();
#endif // _TRACE_H_ #define STRINGIFY(x) #x
#define TOSTRING(x) STRINGIFY(x)
#define TRACEPLUS(when, severity, ...) if (when != 0) TRACE(severity, __VA_ARGS__)
//#define TRACE(severity, ...) trace_print(severity, __VA_ARGS__)
#define TRACE(severity, text, ...) trace_print(severity, PSTR(text), ##__VA_ARGS__)
#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) trace_addweb(srv)
#define TRACE_POLL(a) trace_poll()
#define TRACE_FORCEUPDATE(a) trace_forceupdate()
#else // DONT_USE_TRACE
#define TRACEPLUS(...) ((void)0)
#define TRACE(...) ((void)0) // from assert.h "NOP" - http://stackoverflow.com/questions/9187628/c-empty-function-macros
#define TRACEFUNC(...) ((void)0)
#define TRACEDUMP(...) ((void)0)
#define TRACE_INIT(a) ((void)0)
#define TRACE_ADDWEB(a) ((void)0)
#define TRACE_POLL(a) ((void)0)
#define TRACE_FORCEUPDATE(a) ((void)0)
#endif // DONT_USE_TRACE