diff --git a/src/trace.cpp b/src/Trace.cpp similarity index 72% rename from src/trace.cpp rename to src/Trace.cpp index 5211cca..8464c86 100644 --- a/src/trace.cpp +++ b/src/Trace.cpp @@ -1,3 +1,3 @@ -#include "trace.h" +#include "Trace.h" Trace * mTrace __attribute__((weak)) = nullptr; diff --git a/src/trace.h b/src/Trace.h similarity index 100% rename from src/trace.h rename to src/Trace.h diff --git a/src/traceAsyncWebSocket/traceAsyncWebsocket.cpp b/src/traceAsyncWebSocket/traceAsyncWebsocket.cpp deleted file mode 100644 index 7a5c082..0000000 --- a/src/traceAsyncWebSocket/traceAsyncWebsocket.cpp +++ /dev/null @@ -1,227 +0,0 @@ -#include "traceAsyncWebsocket.hpp" -#include - - -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::attach(this, TRACEWS_CHECK_INTERVAL); -} - -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 diff --git a/src/traceAsyncWebSocket/traceAsyncWebsocket.hpp b/src/traceAsyncWebSocket/traceAsyncWebsocket.hpp deleted file mode 100644 index 5a98fa4..0000000 --- a/src/traceAsyncWebSocket/traceAsyncWebsocket.hpp +++ /dev/null @@ -1,85 +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 "trace.h" -#include -#include -#include - -#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, 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; -}; diff --git a/src/traceStream/traceStream.cpp b/src/traceStream/traceStream.cpp deleted file mode 100644 index 37d1f90..0000000 --- a/src/traceStream/traceStream.cpp +++ /dev/null @@ -1,146 +0,0 @@ -#include "traceStream.hpp" -#include "../trace.h" -#include - -#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 diff --git a/src/traceStream/traceStream.hpp b/src/traceStream/traceStream.hpp deleted file mode 100644 index beab88d..0000000 --- a/src/traceStream/traceStream.hpp +++ /dev/null @@ -1,54 +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 "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; - } -};