Logger api
VoidLoader/VoidLoaderCore/pipeline/head There was a failure building this commit

Signed-off-by: Faumaray <faumaray@gmail.com>
This commit is contained in:
faumaray committed 2025-10-19 23:18:07 +03:00
1 parent c9711dc5fd
commit de538e00b6
19 files changed
+579 -10

No files matched your search

+35
View File
@@ -0,0 +1,35 @@
#pragma once
#include <Logging/Sink.hpp>
#include <Logging/Message.hpp>
#include <condition_variable>
#include <thread>
#include <memory>
#include <mutex>
#include <queue>
#include <stop_token>
namespace vl::logging {
class AsyncWorker {
public:
static AsyncWorker& instance();
struct Item {
std::shared_ptr<Sink> sink;
Message msg;
};
void enqueue(Item&& item);
private:
AsyncWorker();
~AsyncWorker();
void run(std::stop_token st);
std::mutex mtx_;
std::condition_variable_any cv_;
std::queue<Item> queue_;
std::jthread thread_;
};
} // namespace vl::log
+21
View File
@@ -0,0 +1,21 @@
#pragma once
#include <Logging/Sink.hpp>
#include <memory>
namespace vl::logging {
class CombinedSink final : public Sink {
public:
CombinedSink(std::shared_ptr<Sink> a, std::shared_ptr<Sink> b)
: a_(std::move(a)), b_(std::move(b)) {}
void write(const Message& msg) override {
a_->write(msg);
b_->write(msg);
}
private:
std::shared_ptr<Sink> a_, b_;
};
} // namespace vl::log
+15
View File
@@ -0,0 +1,15 @@
#pragma once
#include <Logging/Sink.hpp>
#include <mutex>
namespace vl::logging {
class ConsoleSink final : public Sink {
public:
void write(const Message& msg) override;
private:
std::mutex coutMutex_;
};
} // namespace vl::log
+33
View File
@@ -0,0 +1,33 @@
#pragma once
#include <Logging/Sink.hpp>
#include <cstdint>
#include <filesystem>
#include <fstream>
#include <string>
namespace vl::logging {
// File rotation: R3 (daily + index), plus optional size cap
class FileSink final : public Sink {
public:
explicit FileSink(std::string basePath, std::uintmax_t maxBytes);
void write(const Message& msg) override;
private:
std::string base_;
std::uintmax_t maxBytes_;
std::ofstream out_;
std::string currentDate_; // YYYY-MM-DD
int index_{1};
std::uintmax_t written_{0};
void ensureOpen(const Message& msg);
void openCurrent();
void rotateToDate(std::string newDate);
static std::string dateOf(const std::chrono::system_clock::time_point& tp);
static std::string filenameFor(const std::string& base, std::string_view date, int index);
};
} // namespace vl::log
+30
View File
@@ -0,0 +1,30 @@
#pragma once
#include <Logging/Logger.hpp>
#include <Logging/Sink.hpp>
#include <Logging/AsyncWorker.hpp>
#include <memory>
#include <string>
namespace vl::logging {
class LoggerInternal final : public Logger {
public:
LoggerInternal(std::string prefix, std::shared_ptr<Sink> sink, Level lvl)
: prefix_(std::move(prefix)), sink_(std::move(sink)), level_(lvl) {}
std::string_view prefix() const noexcept override { return prefix_; }
void setLevel(Level l) noexcept override { level_ = l; }
Level level() const noexcept override { return level_; }
protected:
void enqueue(Message&& m) override {
AsyncWorker::instance().enqueue({ sink_, std::move(m) });
}
private:
std::string prefix_;
std::shared_ptr<Sink> sink_;
Level level_;
};
} // namespace vl::log
+29
View File
@@ -0,0 +1,29 @@
#pragma once
#include <Logging/Provider.hpp>
#include <Logging/Sink.hpp>
#include <mutex>
#include <string>
#include <string_view>
#include <unordered_map>
namespace vl::logging {
class LoggerProvider final : public Provider {
public:
std::shared_ptr<Logger> createConsole(std::string prefix, Level lvl) override;
std::shared_ptr<Logger> createFile(std::string prefix, std::string basePath,
std::uintmax_t maxBytes, Level lvl) override;
std::shared_ptr<Logger> createCombined(std::string prefix, std::string basePath,
std::uintmax_t maxBytes, Level lvl) override;
std::shared_ptr<Logger> get(std::string_view prefix) const override;
bool exists(std::string_view prefix) const override;
private:
std::shared_ptr<Logger> emplaceOrGet_(std::string prefix, std::shared_ptr<Sink> sink, Level lvl);
mutable std::mutex mtx_;
std::unordered_map<std::string, std::shared_ptr<Logger>> byPrefix_;
};
} // namespace vl::log
+8
View File
@@ -0,0 +1,8 @@
#pragma once
namespace vl::logging {
// Call during VoidLoaderCore startup after it is loaded by JVM/JNI.
void init_voidloader_logger();
} // namespace vl::log