diff --git a/include/Logging/Logger.hpp b/include/Logging/Logger.hpp index 3e01c5d..49355a6 100644 --- a/include/Logging/Logger.hpp +++ b/include/Logging/Logger.hpp @@ -13,6 +13,7 @@ struct Logger : public std::enable_shared_from_this { virtual std::string_view prefix() const noexcept = 0; virtual void setLevel(Level) noexcept = 0; + virtual void setMaxBytes(std::uintmax_t) noexcept = 0; virtual Level level() const noexcept = 0; template @@ -21,8 +22,8 @@ struct Logger : public std::enable_shared_from_this { Message m; m.ts = std::chrono::system_clock::now(); m.level = lvl; - m.prefix = std::string(prefix()); - m.text = std::vformat(fmt.get(), std::make_format_args(std::forward(args)...)); + m.prefix = "[" + std::string(prefix()) + "]"; + m.text = std::format(fmt, std::forward(args)...); enqueue(std::move(m)); } diff --git a/include/Mod/Mod.hpp b/include/Mod/Mod.hpp index f229c2b..af479a6 100644 --- a/include/Mod/Mod.hpp +++ b/include/Mod/Mod.hpp @@ -1,4 +1,6 @@ #pragma once +#include +#include #include #include #include @@ -15,6 +17,11 @@ namespace vl::mod { template class Mod : public Base { + +protected: + std::shared_ptr m_logger; + const vl::mod::Context& m_ctx; + private: static std::unique_ptr _vl_factory(const Context& ctx) { if constexpr (std::is_constructible_v) @@ -23,6 +30,7 @@ private: } else { + static_assert(false, "Mod should have explicit constructor Mod(const vl::mod::Context& ctx) : "); return std::make_unique(); } } @@ -34,7 +42,32 @@ private: }; public: + explicit Mod(const vl::mod::Context& ctx) + : m_ctx(ctx) + { + // Automatically create per-mod combined logger + auto& mgr = vl::logging::LoggerManager::instance(); + + // Create log file in ctx.logDir, using mod’s name as prefix + + auto name = std::string(static_cast(this)->name()); + auto logDir = m_ctx.logDir / name; + std::filesystem::create_directories(logDir); + m_logger = mgr.createCombined( + std::move(name), + std::move(logDir), + 10 * 1024 * 1024, // example rotation size + vl::logging::Level::Info + ); + + m_logger->info("[{}] Mod constructed (v{})", static_cast(this)->name(), static_cast(this)->version()); + } + virtual ~Mod() = default; + + [[nodiscard]] auto& ctx() const noexcept { return m_ctx; } + [[nodiscard]] auto logger() const noexcept { return m_logger; } + [[nodiscard]] auto& loggerRef() const noexcept { return *m_logger; } }; } // namespace vl::mod