change in logger and initialization

Signed-off-by: Faumaray <faumaray@gmail.com>
This commit is contained in:
faumaray committed 2025-10-20 21:06:41 +03:00
1 parent ee296351c6
commit 4cd8386ff4
2 files changed
+36 -2

No files matched your search

+3 -2
View File
@@ -13,6 +13,7 @@ struct Logger : public std::enable_shared_from_this<Logger> {
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 <class... Args>
@@ -21,8 +22,8 @@ struct Logger : public std::enable_shared_from_this<Logger> {
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>(args)...));
m.prefix = "[" + std::string(prefix()) + "]";
m.text = std::format(fmt, std::forward<Args>(args)...);
enqueue(std::move(m));
}
+33
View File
@@ -1,4 +1,6 @@
#pragma once
#include <Logging/Logger.hpp>
#include <Logging/LoggerManager.hpp>
#include <Mod/Base.hpp>
#include <Mod/Registration.hpp>
#include <Mod/Context.hpp>
@@ -15,6 +17,11 @@ namespace vl::mod {
template<class T>
class Mod : public Base {
protected:
std::shared_ptr<vl::logging::Logger> m_logger;
const vl::mod::Context& m_ctx;
private:
static std::unique_ptr<Base> _vl_factory(const Context& ctx) {
if constexpr (std::is_constructible_v<T, const Context&>)
@@ -23,6 +30,7 @@ private:
}
else
{
static_assert(false, "Mod should have explicit constructor Mod(const vl::mod::Context& ctx) : ");
return std::make_unique<T>();
}
}
@@ -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<const T*>(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<const T*>(this)->name(), static_cast<const T*>(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