#pragma once #include "Types.hpp" #include #include #include #include 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 void logf(LogLevel level, std::format_string fmt, Args&&... args, std::source_location location = std::source_location::current()) { log(level, std::format(fmt, std::forward(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; virtual void registerCallback(const LogCallback& callback) = 0; virtual void unregisterCallback(const LogCallback& callback) = 0; }; using LoggerPtr = std::shared_ptr; // 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(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