Files
EchoionApi/include/echoion/api/LoggerInterface.hpp
T
2026-02-01 23:05:41 +03:00

105 lines
3.4 KiB
C++

#pragma once
#include "Types.hpp"
#include <string>
#include <functional>
#include <source_location>
#include <format>
namespace Echoion::Api {
// Logger interface for the user's custom logger implementation
class ILogger {
public:
virtual ~ILogger() = default;
// Core logging method
virtual void log(LogLevel level, std::string_view message,
std::source_location location = std::source_location::current()) = 0;
// Convenience methods
virtual void debug(std::string_view message,
std::source_location location = std::source_location::current()) {
log(LogLevel::Debug, message, location);
}
virtual void info(std::string_view message,
std::source_location location = std::source_location::current()) {
log(LogLevel::Info, message, location);
}
virtual void warning(std::string_view message,
std::source_location location = std::source_location::current()) {
log(LogLevel::Warning, message, location);
}
virtual void error(std::string_view message,
std::source_location location = std::source_location::current()) {
log(LogLevel::Error, message, location);
}
virtual void critical(std::string_view message,
std::source_location location = std::source_location::current()) {
log(LogLevel::Critical, message, location);
}
// Template method for formatted logging
template<typename... Args>
void logf(LogLevel level, std::format_string<Args...> fmt, Args&&... args,
std::source_location location = std::source_location::current()) {
log(level, std::format(fmt, std::forward<Args>(args)...), location);
}
// Set minimum log level
virtual void setMinLevel(LogLevel level) = 0;
[[nodiscard]] virtual LogLevel getMinLevel() const = 0;
// Register log callback
using LogCallback = std::function<void(const LogEntry&)>;
virtual void registerCallback(const LogCallback& callback) = 0;
virtual void unregisterCallback(const LogCallback& callback) = 0;
};
using LoggerPtr = std::shared_ptr<ILogger>;
// Global logger accessor
class LoggerRegistry {
public:
static LoggerRegistry& instance() {
static LoggerRegistry instance;
return instance;
}
void setLogger(LoggerPtr logger) { logger_ = std::move(logger); }
[[nodiscard]] LoggerPtr getLogger() const { return logger_; }
[[nodiscard]] bool hasLogger() const noexcept { return static_cast<bool>(logger_); }
private:
LoggerRegistry() = default;
LoggerPtr logger_;
};
// Convenience macros
#define ECHOION_LOG_DEBUG(msg) \
do { if (auto logger = Echoion::Api::LoggerRegistry::instance().getLogger()) \
logger->debug(msg); } while(0)
#define ECHOION_LOG_INFO(msg) \
do { if (auto logger = Echoion::Api::LoggerRegistry::instance().getLogger()) \
logger->info(msg); } while(0)
#define ECHOION_LOG_WARNING(msg) \
do { if (auto logger = Echoion::Api::LoggerRegistry::instance().getLogger()) \
logger->warning(msg); } while(0)
#define ECHOION_LOG_ERROR(msg) \
do { if (auto logger = Echoion::Api::LoggerRegistry::instance().getLogger()) \
logger->error(msg); } while(0)
#define ECHOION_LOG_CRITICAL(msg) \
do { if (auto logger = Echoion::Api::LoggerRegistry::instance().getLogger()) \
logger->critical(msg); } while(0)
} // namespace Echoion::Api