feat: logging service
This commit is contained in:
72
include/spore/services/LoggingService.h
Normal file
72
include/spore/services/LoggingService.h
Normal file
@@ -0,0 +1,72 @@
|
||||
#pragma once
|
||||
|
||||
#include "spore/Service.h"
|
||||
#include "spore/core/NodeContext.h"
|
||||
#include "spore/core/ApiServer.h"
|
||||
#include <Arduino.h>
|
||||
|
||||
enum class LogLevel {
|
||||
DEBUG = 0,
|
||||
INFO = 1,
|
||||
WARN = 2,
|
||||
ERROR = 3
|
||||
};
|
||||
|
||||
struct LogData {
|
||||
LogLevel level;
|
||||
String component;
|
||||
String message;
|
||||
unsigned long timestamp;
|
||||
|
||||
LogData(LogLevel lvl, const String& comp, const String& msg)
|
||||
: level(lvl), component(comp), message(msg), timestamp(millis()) {}
|
||||
};
|
||||
|
||||
class LoggingService : public Service {
|
||||
public:
|
||||
LoggingService(NodeContext& ctx, ApiServer& apiServer);
|
||||
virtual ~LoggingService() = default;
|
||||
|
||||
// Service interface
|
||||
void registerEndpoints(ApiServer& api) override;
|
||||
const char* getName() const override { return "logging"; }
|
||||
|
||||
// Logging methods
|
||||
void log(LogLevel level, const String& component, const String& message);
|
||||
void debug(const String& component, const String& message);
|
||||
void info(const String& component, const String& message);
|
||||
void warn(const String& component, const String& message);
|
||||
void error(const String& component, const String& message);
|
||||
|
||||
// Configuration
|
||||
void setLogLevel(LogLevel level);
|
||||
void enableSerialLogging(bool enable);
|
||||
|
||||
private:
|
||||
NodeContext& ctx;
|
||||
ApiServer& apiServer;
|
||||
LogLevel currentLogLevel;
|
||||
bool serialLoggingEnabled;
|
||||
|
||||
void setupEventHandlers();
|
||||
void handleSerialLog(void* data);
|
||||
String formatLogMessage(const LogData& logData);
|
||||
String logLevelToString(LogLevel level);
|
||||
|
||||
// API endpoint handlers
|
||||
void handleGetLogLevel(AsyncWebServerRequest* request);
|
||||
void handleSetLogLevel(AsyncWebServerRequest* request);
|
||||
void handleGetConfig(AsyncWebServerRequest* request);
|
||||
};
|
||||
|
||||
// Global logging functions that use the event system directly
|
||||
void logDebug(NodeContext& ctx, const String& component, const String& message);
|
||||
void logInfo(NodeContext& ctx, const String& component, const String& message);
|
||||
void logWarn(NodeContext& ctx, const String& component, const String& message);
|
||||
void logError(NodeContext& ctx, const String& component, const String& message);
|
||||
|
||||
// Convenience macros for easy logging (requires ctx to be available)
|
||||
#define LOG_DEBUG(ctx, component, message) logDebug(ctx, component, message)
|
||||
#define LOG_INFO(ctx, component, message) logInfo(ctx, component, message)
|
||||
#define LOG_WARN(ctx, component, message) logWarn(ctx, component, message)
|
||||
#define LOG_ERROR(ctx, component, message) logError(ctx, component, message)
|
||||
Reference in New Issue
Block a user