feat: logging service
This commit is contained in:
45
examples/logging_example/main.cpp
Normal file
45
examples/logging_example/main.cpp
Normal file
@@ -0,0 +1,45 @@
|
||||
#include "spore/Spore.h"
|
||||
#include "spore/services/LoggingService.h"
|
||||
#include <Arduino.h>
|
||||
|
||||
Spore spore;
|
||||
|
||||
void setup() {
|
||||
// Initialize Spore framework
|
||||
spore.setup();
|
||||
|
||||
// Start the framework
|
||||
spore.begin();
|
||||
|
||||
// Demonstrate different logging levels
|
||||
LOG_INFO(spore.ctx, "Example", "Logging example started");
|
||||
LOG_DEBUG(spore.ctx, "Example", "This is a debug message");
|
||||
LOG_WARN(spore.ctx, "Example", "This is a warning message");
|
||||
LOG_ERROR(spore.ctx, "Example", "This is an error message");
|
||||
|
||||
// Demonstrate logging with different components
|
||||
LOG_INFO(spore.ctx, "Network", "WiFi connection established");
|
||||
LOG_INFO(spore.ctx, "Cluster", "Node discovered: esp-123456");
|
||||
LOG_INFO(spore.ctx, "API", "Server started on port 80");
|
||||
|
||||
// Demonstrate event-based logging
|
||||
LogData* logData = new LogData(LogLevel::INFO, "Example", "Event-based logging demonstration");
|
||||
spore.ctx.fire("log/serial", logData);
|
||||
|
||||
// Show that all logging now goes through the centralized system
|
||||
LOG_INFO(spore.ctx, "Example", "All Serial.println calls have been replaced with event-based logging!");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// Run the Spore framework
|
||||
spore.loop();
|
||||
|
||||
// Log some periodic information
|
||||
static unsigned long lastLog = 0;
|
||||
if (millis() - lastLog > 5000) {
|
||||
LOG_DEBUG(spore.ctx, "Example", "System running - Free heap: " + String(ESP.getFreeHeap()));
|
||||
lastLog = millis();
|
||||
}
|
||||
|
||||
delay(100);
|
||||
}
|
||||
@@ -82,7 +82,8 @@ void NeoPatternService::handleControlRequest(AsyncWebServerRequest* request) {
|
||||
|
||||
if (request->hasParam("brightness", true)) {
|
||||
int b = request->getParam("brightness", true)->value().toInt();
|
||||
if (b < 0) b = 0; if (b > 255) b = 255;
|
||||
if (b < 0) b = 0;
|
||||
if (b > 255) b = 255;
|
||||
setBrightness((uint8_t)b);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#include <Arduino.h>
|
||||
#include "spore/Spore.h"
|
||||
#include "spore/services/LoggingService.h"
|
||||
#include "NeoPatternService.h"
|
||||
|
||||
#ifndef LED_STRIP_PIN
|
||||
@@ -36,7 +37,7 @@ void setup() {
|
||||
// Start the API server and complete initialization
|
||||
spore.begin();
|
||||
|
||||
Serial.println("[Main] NeoPattern service registered and ready!");
|
||||
LOG_INFO(spore.getContext(), "Main", "NeoPattern service registered and ready!");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#include "NeoPixelService.h"
|
||||
#include "spore/core/ApiServer.h"
|
||||
#include "spore/services/LoggingService.h"
|
||||
|
||||
// Wheel helper: map 0-255 to RGB rainbow
|
||||
static uint32_t colorWheel(Adafruit_NeoPixel& strip, uint8_t pos) {
|
||||
@@ -104,7 +105,8 @@ void NeoPixelService::handleControlRequest(AsyncWebServerRequest* request) {
|
||||
|
||||
if (request->hasParam("brightness", true)) {
|
||||
int b = request->getParam("brightness", true)->value().toInt();
|
||||
if (b < 0) b = 0; if (b > 255) b = 255;
|
||||
if (b < 0) b = 0;
|
||||
if (b > 255) b = 255;
|
||||
setBrightness((uint8_t)b);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#include <Arduino.h>
|
||||
#include "spore/Spore.h"
|
||||
#include "spore/services/LoggingService.h"
|
||||
#include "NeoPixelService.h"
|
||||
|
||||
#ifndef NEOPIXEL_PIN
|
||||
@@ -36,7 +37,7 @@ void setup() {
|
||||
// Start the API server and complete initialization
|
||||
spore.begin();
|
||||
|
||||
Serial.println("[Main] NeoPixel service registered and ready!");
|
||||
LOG_INFO(spore.getContext(), "Main", "NeoPixel service registered and ready!");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
#include "RelayService.h"
|
||||
#include "spore/core/ApiServer.h"
|
||||
#include "spore/services/LoggingService.h"
|
||||
|
||||
RelayService::RelayService(TaskManager& taskMgr, int pin)
|
||||
: taskManager(taskMgr), relayPin(pin), relayOn(false) {
|
||||
RelayService::RelayService(NodeContext& ctx, TaskManager& taskMgr, int pin)
|
||||
: ctx(ctx), taskManager(taskMgr), relayPin(pin), relayOn(false) {
|
||||
pinMode(relayPin, OUTPUT);
|
||||
// Many relay modules are active LOW. Start in OFF state (relay de-energized).
|
||||
digitalWrite(relayPin, HIGH);
|
||||
@@ -64,13 +65,13 @@ void RelayService::turnOn() {
|
||||
relayOn = true;
|
||||
// Active LOW relay
|
||||
digitalWrite(relayPin, LOW);
|
||||
Serial.println("[RelayService] Relay ON");
|
||||
LOG_INFO(ctx, "RelayService", "Relay ON");
|
||||
}
|
||||
|
||||
void RelayService::turnOff() {
|
||||
relayOn = false;
|
||||
digitalWrite(relayPin, HIGH);
|
||||
Serial.println("[RelayService] Relay OFF");
|
||||
LOG_INFO(ctx, "RelayService", "Relay OFF");
|
||||
}
|
||||
|
||||
void RelayService::toggle() {
|
||||
@@ -83,7 +84,6 @@ void RelayService::toggle() {
|
||||
|
||||
void RelayService::registerTasks() {
|
||||
taskManager.registerTask("relay_status_print", 5000, [this]() {
|
||||
Serial.printf("[RelayService] Status - pin: %d, state: %s\n",
|
||||
relayPin, relayOn ? "ON" : "OFF");
|
||||
LOG_INFO(ctx, "RelayService", "Status - pin: " + String(relayPin) + ", state: " + (relayOn ? "ON" : "OFF"));
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
#pragma once
|
||||
#include "spore/Service.h"
|
||||
#include "spore/core/TaskManager.h"
|
||||
#include "spore/core/NodeContext.h"
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
class RelayService : public Service {
|
||||
public:
|
||||
RelayService(TaskManager& taskMgr, int pin);
|
||||
RelayService(NodeContext& ctx, TaskManager& taskMgr, int pin);
|
||||
void registerEndpoints(ApiServer& api) override;
|
||||
const char* getName() const override { return "Relay"; }
|
||||
|
||||
@@ -16,6 +17,7 @@ public:
|
||||
private:
|
||||
void registerTasks();
|
||||
|
||||
NodeContext& ctx;
|
||||
TaskManager& taskManager;
|
||||
int relayPin;
|
||||
bool relayOn;
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#include <Arduino.h>
|
||||
#include "spore/Spore.h"
|
||||
#include "spore/services/LoggingService.h"
|
||||
#include "RelayService.h"
|
||||
|
||||
// Choose a default relay pin. For ESP-01 this is GPIO0. Adjust as needed for your board.
|
||||
@@ -22,13 +23,13 @@ void setup() {
|
||||
spore.setup();
|
||||
|
||||
// Create and add custom service
|
||||
relayService = new RelayService(spore.getTaskManager(), RELAY_PIN);
|
||||
relayService = new RelayService(spore.getContext(), spore.getTaskManager(), RELAY_PIN);
|
||||
spore.addService(relayService);
|
||||
|
||||
// Start the API server and complete initialization
|
||||
spore.begin();
|
||||
|
||||
Serial.println("[Main] Relay service registered and ready!");
|
||||
LOG_INFO(spore.getContext(), "Main", "Relay service registered and ready!");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#include <Arduino.h>
|
||||
#include "spore/Spore.h"
|
||||
#include "spore/services/LoggingService.h"
|
||||
|
||||
// Create Spore instance
|
||||
Spore spore;
|
||||
@@ -11,8 +12,8 @@ void setup() {
|
||||
// Start the framework (this will start the API server with static file serving)
|
||||
spore.begin();
|
||||
|
||||
Serial.println("Static web server started!");
|
||||
Serial.println("Visit http://<node-ip>/ to see the web interface");
|
||||
LOG_INFO(spore.getContext(), "Example", "Static web server started!");
|
||||
LOG_INFO(spore.getContext(), "Example", "Visit http://<node-ip>/ to see the web interface");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
|
||||
Reference in New Issue
Block a user