2018-04-22 15:48:16 +02:00
|
|
|
/**
|
|
|
|
* @file trace.h
|
|
|
|
* @author Pavel Brychta, http://www.xpablo.cz
|
|
|
|
*
|
2024-01-19 11:42:39 +01:00
|
|
|
* Copyright (c) 2016-24 Pavel Brychta. All rights reserved.
|
2018-04-22 15:48:16 +02:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
*/
|
2023-05-20 12:47:13 +02:00
|
|
|
#pragma once
|
2018-04-22 15:48:16 +02:00
|
|
|
|
2024-07-29 16:14:03 +02:00
|
|
|
// Vydefinujeme-li promennou TRACE_ALL=1, pak TRACEPLUS se bude vypisovat vsechny zpravy
|
|
|
|
|
2018-04-22 15:48:16 +02:00
|
|
|
// Definice jednotlivych typu hlaseni do trasovani
|
2024-07-29 16:14:03 +02:00
|
|
|
#define TRACE_NONE (-1) // nic
|
2024-01-19 12:51:02 +01:00
|
|
|
#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_VERBOSE 4 // mene zajimava ladici zprava - take cerna
|
2024-01-19 11:42:39 +01:00
|
|
|
|
|
|
|
#ifndef TRACE_LIMIT
|
|
|
|
#define TRACE_LIMIT TRACE_DEBUG
|
|
|
|
#endif
|
2019-10-11 16:54:53 +02:00
|
|
|
#define STRINGIFY(x) #x
|
2024-01-19 11:42:39 +01:00
|
|
|
#define TOSTRING(x) STRINGIFY(x)
|
|
|
|
|
|
|
|
#if defined(TRACE_USE_ASYNC_WEBSOCKET)
|
|
|
|
#include "asyncWebsocket/traceAsyncWebsocket.hpp"
|
2024-07-10 15:49:11 +02:00
|
|
|
|
2024-01-19 11:42:39 +01:00
|
|
|
#define TRACE(severity, text, ...) \
|
2024-01-19 12:51:02 +01:00
|
|
|
do { \
|
2024-01-19 11:42:39 +01:00
|
|
|
constexpr bool trace_limit = (severity <= TRACE_LIMIT); \
|
|
|
|
if (trace_limit) \
|
|
|
|
trace_print(severity, PSTR(text), ##__VA_ARGS__); \
|
2024-01-19 12:51:02 +01:00
|
|
|
} while (0)
|
2024-01-19 11:42:39 +01:00
|
|
|
#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_FORCEUPDATE(a) trace_forceupdate()
|
|
|
|
|
|
|
|
#else
|
|
|
|
#if defined(ESP32)
|
|
|
|
#include <Arduino.h>
|
|
|
|
#define TRACE(severity, format, ...) \
|
2024-01-19 12:51:02 +01:00
|
|
|
do { \
|
2024-01-19 11:42:39 +01:00
|
|
|
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__); \
|
2024-01-19 12:51:02 +01:00
|
|
|
} while (0)
|
2024-01-19 11:42:39 +01:00
|
|
|
#else
|
|
|
|
#ifdef DEBUG_ESP_PORT
|
|
|
|
#include <Arduino.h>
|
2024-01-19 12:51:02 +01:00
|
|
|
|
|
|
|
// 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()
|
|
|
|
|
2024-01-19 11:42:39 +01:00
|
|
|
#define TRACE(severity, text, ...) \
|
2024-01-19 12:51:02 +01:00
|
|
|
do { \
|
2024-01-19 11:42:39 +01:00
|
|
|
constexpr int s = severity; \
|
|
|
|
if (s == TRACE_ERROR) \
|
2024-01-19 12:51:02 +01:00
|
|
|
DEBUG_ESP_PORT.printf_P(TRACE_LOG_FORMAT(E, text), ##__VA_ARGS__); \
|
2024-01-19 11:42:39 +01:00
|
|
|
else if (s == TRACE_DEBUG) \
|
2024-01-19 12:51:02 +01:00
|
|
|
DEBUG_ESP_PORT.printf_P(TRACE_LOG_FORMAT(D, text), ##__VA_ARGS__); \
|
2024-01-19 11:42:39 +01:00
|
|
|
else if (s == TRACE_WARNING) \
|
2024-01-19 12:51:02 +01:00
|
|
|
DEBUG_ESP_PORT.printf_P(TRACE_LOG_FORMAT(W, text), ##__VA_ARGS__); \
|
2024-01-19 11:42:39 +01:00
|
|
|
else if (s == TRACE_INFO) \
|
2024-01-19 12:51:02 +01:00
|
|
|
DEBUG_ESP_PORT.printf_P(TRACE_LOG_FORMAT(I, text), ##__VA_ARGS__); \
|
2024-01-19 11:42:39 +01:00
|
|
|
else if (s == TRACE_VERBOSE) \
|
2024-01-19 12:51:02 +01:00
|
|
|
DEBUG_ESP_PORT.printf_P(TRACE_LOG_FORMAT(V, text), ##__VA_ARGS__); \
|
|
|
|
} while (0)
|
2024-01-19 11:42:39 +01:00
|
|
|
#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
|
2024-07-10 15:49:11 +02:00
|
|
|
|
2024-07-29 16:14:03 +02:00
|
|
|
#ifndef TRACE_ALL
|
|
|
|
#define TRACE_ALL 0
|
|
|
|
#endif
|
|
|
|
|
2024-07-10 15:49:11 +02:00
|
|
|
#define TRACEPLUS(when, severity, ...) \
|
|
|
|
do { \
|
2024-07-29 16:14:03 +02:00
|
|
|
constexpr bool handle = (when != 0) || TRACE_ALL; \
|
2024-07-10 15:49:11 +02:00
|
|
|
if (handle) \
|
|
|
|
TRACE(severity, __VA_ARGS__); \
|
|
|
|
} while (0)
|