Add ArduinoHALog helpers, extend ArduinoHADefines, update mqtt usage doc

This commit is contained in:
2026-04-22 12:09:12 +10:00
parent 40b98a38ee
commit c9e6eef5b2
4 changed files with 45 additions and 6 deletions
+1 -1
View File
@@ -56,7 +56,7 @@ device.enableLastWill(); // broker publishes offline when TCP drops
Defined in `ArduinoHADefines.h` or via build flags. Defined in `ArduinoHADefines.h` or via build flags.
- **`ARDUINOHA_DEBUG`** — enables library logging (useful when debugging MQTT). - **`ARDUINOHA_DEBUG`** — enables library logging (useful when debugging MQTT). By default lines go to **`Serial`** unless you install a sink with **`arduinoHASetLogSink(ArduinoHALogSink*)`** (for example **DeviceFramework** forwards debug output into its main log stream).
**Exclude unused device types** (saves flash from vtables), e.g.: **Exclude unused device types** (saves flash from vtables), e.g.:
+13 -5
View File
@@ -1,8 +1,10 @@
// Turns on debug information of the ArduinoHA core. // Turns on debug information of the ArduinoHA core.
// Please note that you need to initialize serial interface manually // Without a log sink (arduinoHASetLogSink), initialize Serial (e.g. Serial.begin(115200)) before ArduinoHA.
// by calling Serial.begin([baudRate]) before initializing ArduinoHA. // With a sink installed, the host is responsible for transport setup.
// #define ARDUINOHA_DEBUG // #define ARDUINOHA_DEBUG
#include "ArduinoHALog.h"
// These macros allow to exclude some parts of the library to save more resources. // These macros allow to exclude some parts of the library to save more resources.
// #define EX_ARDUINOHA_BINARY_SENSOR // #define EX_ARDUINOHA_BINARY_SENSOR
// #define EX_ARDUINOHA_BUTTON // #define EX_ARDUINOHA_BUTTON
@@ -35,9 +37,15 @@
#if defined(ARDUINOHA_DEBUG) #if defined(ARDUINOHA_DEBUG)
#include <Arduino.h> #include <Arduino.h>
#define ARDUINOHA_DEBUG_INIT() Serial.begin(115200); #define ARDUINOHA_DEBUG_INIT() do { if (!arduinoHAGetLogSink()) { Serial.begin(115200); } } while(0)
#define ARDUINOHA_DEBUG_PRINTLN(x) Serial.println(x); #define ARDUINOHA_DEBUG_PRINTLN(x) do { \
#define ARDUINOHA_DEBUG_PRINT(x) Serial.print(x); if (arduinoHAGetLogSink()) { arduinoHAGetLogSink()->println(String(x)); } \
else { Serial.println(x); } \
} while(0)
#define ARDUINOHA_DEBUG_PRINT(x) do { \
if (arduinoHAGetLogSink()) { arduinoHAGetLogSink()->print(String(x)); } \
else { Serial.print(x); } \
} while(0)
#else #else
#define ARDUINOHA_DEBUG_INIT() #define ARDUINOHA_DEBUG_INIT()
#define ARDUINOHA_DEBUG_PRINTLN(x) #define ARDUINOHA_DEBUG_PRINTLN(x)
+11
View File
@@ -0,0 +1,11 @@
#include "ArduinoHALog.h"
static ArduinoHALogSink* g_arduinoHALogSink = nullptr;
void arduinoHASetLogSink(ArduinoHALogSink* sink) {
g_arduinoHALogSink = sink;
}
ArduinoHALogSink* arduinoHAGetLogSink() {
return g_arduinoHALogSink;
}
+20
View File
@@ -0,0 +1,20 @@
#ifndef ARDUINOHA_LOG_H
#define ARDUINOHA_LOG_H
#include <Arduino.h>
/**
* Optional sink for ArduinoHA debug output (ARDUINOHA_DEBUG).
* When set, ARDUINOHA_DEBUG_* macros route here; otherwise Serial is used.
*/
class ArduinoHALogSink {
public:
virtual void print(const String& s) = 0;
virtual void println(const String& s) = 0;
virtual ~ArduinoHALogSink() = default;
};
void arduinoHASetLogSink(ArduinoHALogSink* sink);
ArduinoHALogSink* arduinoHAGetLogSink();
#endif