2020-04-27 09:01:25 -07:00
|
|
|
#pragma once
|
|
|
|
|
|
2023-08-31 20:40:01 -05:00
|
|
|
#include "../freertosinc.h"
|
2024-07-03 16:29:07 -05:00
|
|
|
#include "mesh/generated/meshtastic/mesh.pb.h"
|
2020-04-27 09:01:25 -07:00
|
|
|
#include <Print.h>
|
2020-12-25 15:17:56 +08:00
|
|
|
#include <stdarg.h>
|
2023-02-07 01:02:51 +01:00
|
|
|
#include <string>
|
2020-04-27 09:01:25 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* A Printable that can be switched to squirt its bytes to a different sink.
|
|
|
|
|
* This class is mostly useful to allow debug printing to be redirected away from Serial
|
|
|
|
|
* to some other transport if we switch Serial usage (on the fly) to some other purpose.
|
|
|
|
|
*/
|
|
|
|
|
class RedirectablePrint : public Print
|
|
|
|
|
{
|
|
|
|
|
Print *dest;
|
|
|
|
|
|
2023-08-31 20:40:01 -05:00
|
|
|
#ifdef HAS_FREE_RTOS
|
|
|
|
|
SemaphoreHandle_t inDebugPrint = nullptr;
|
|
|
|
|
StaticSemaphore_t _MutexStorageSpace;
|
|
|
|
|
#else
|
2021-03-09 16:45:40 +08:00
|
|
|
volatile bool inDebugPrint = false;
|
2023-08-31 20:40:01 -05:00
|
|
|
#endif
|
2020-04-27 09:01:25 -07:00
|
|
|
public:
|
2022-01-24 17:24:40 +00:00
|
|
|
explicit RedirectablePrint(Print *_dest) : dest(_dest) {}
|
2020-04-27 09:01:25 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set a new destination
|
|
|
|
|
*/
|
2023-08-31 20:40:01 -05:00
|
|
|
void rpInit();
|
2020-04-27 09:01:25 -07:00
|
|
|
void setDestination(Print *dest);
|
|
|
|
|
|
2020-11-13 07:48:25 +08:00
|
|
|
virtual size_t write(uint8_t c);
|
2020-12-25 15:17:56 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Debug logging print message
|
2023-01-21 14:34:29 +01:00
|
|
|
*
|
2020-12-25 15:17:56 +08:00
|
|
|
* If the provide format string ends with a newline we assume it is the final print of a single
|
|
|
|
|
* log message. Otherwise we assume more prints will come before the log message ends. This
|
|
|
|
|
* allows you to call logDebug a few times to build up a single log message line if you wish.
|
|
|
|
|
*/
|
2024-06-28 20:10:41 -05:00
|
|
|
void log(const char *logLevel, const char *format, ...) __attribute__((format(printf, 3, 4)));
|
2020-12-25 15:17:56 +08:00
|
|
|
|
|
|
|
|
/** like printf but va_list based */
|
2024-06-28 20:10:41 -05:00
|
|
|
size_t vprintf(const char *logLevel, const char *format, va_list arg);
|
2023-01-09 17:03:52 +01:00
|
|
|
|
|
|
|
|
void hexDump(const char *logLevel, unsigned char *buf, uint16_t len);
|
2023-02-07 01:02:51 +01:00
|
|
|
|
|
|
|
|
std::string mt_sprintf(const std::string fmt_str, ...);
|
2020-04-27 09:01:25 -07:00
|
|
|
|
2024-06-30 16:41:27 -07:00
|
|
|
protected:
|
|
|
|
|
/// Subclasses can override if they need to change how we format over the serial port
|
|
|
|
|
virtual void log_to_serial(const char *logLevel, const char *format, va_list arg);
|
2024-10-26 20:06:50 +10:00
|
|
|
meshtastic_LogRecord_Level getLogLevel(const char *logLevel);
|
2020-04-27 09:01:25 -07:00
|
|
|
|
2024-06-30 16:41:27 -07:00
|
|
|
private:
|
|
|
|
|
void log_to_syslog(const char *logLevel, const char *format, va_list arg);
|
|
|
|
|
void log_to_ble(const char *logLevel, const char *format, va_list arg);
|
|
|
|
|
};
|