Signed-off-by: Faumaray <faumaray@gmail.com>
This commit is contained in:
19 files changed
+579
-10
No files matched your search
@@ -0,0 +1,51 @@
|
||||
#include <Logging/AsyncWorker.hpp>
|
||||
|
||||
namespace vl::logging {
|
||||
|
||||
AsyncWorker& AsyncWorker::instance() {
|
||||
static AsyncWorker w;
|
||||
return w;
|
||||
}
|
||||
|
||||
AsyncWorker::AsyncWorker()
|
||||
: thread_([this](std::stop_token st){ run(st); }) {}
|
||||
|
||||
AsyncWorker::~AsyncWorker() {
|
||||
thread_.request_stop();
|
||||
cv_.notify_all();
|
||||
}
|
||||
|
||||
void AsyncWorker::enqueue(Item&& item) {
|
||||
{
|
||||
std::scoped_lock lk(mtx_);
|
||||
queue_.push(std::move(item));
|
||||
}
|
||||
cv_.notify_one();
|
||||
}
|
||||
|
||||
void AsyncWorker::run(std::stop_token st) {
|
||||
while (!st.stop_requested()) {
|
||||
Item item;
|
||||
{
|
||||
std::unique_lock lk(mtx_);
|
||||
cv_.wait(lk, st, [this]{ return !queue_.empty(); });
|
||||
if (st.stop_requested()) break;
|
||||
item = std::move(queue_.front());
|
||||
queue_.pop();
|
||||
}
|
||||
if (item.sink) item.sink->write(item.msg);
|
||||
}
|
||||
// drain
|
||||
for (;;) {
|
||||
Item item;
|
||||
{
|
||||
std::scoped_lock lk(mtx_);
|
||||
if (queue_.empty()) break;
|
||||
item = std::move(queue_.front());
|
||||
queue_.pop();
|
||||
}
|
||||
if (item.sink) item.sink->write(item.msg);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace vl::log
|
||||
@@ -0,0 +1,34 @@
|
||||
#include <Logging/ConsoleSink.hpp>
|
||||
#include <Logging/Level.hpp>
|
||||
#include <chrono>
|
||||
#include <ctime>
|
||||
#include <format>
|
||||
#include <iostream>
|
||||
|
||||
namespace vl::logging {
|
||||
|
||||
static std::string formatTimestamp(const std::chrono::system_clock::time_point& tp) {
|
||||
const auto tt = std::chrono::system_clock::to_time_t(tp);
|
||||
std::tm tm{};
|
||||
#if defined(_WIN32)
|
||||
localtime_s(&tm, &tt);
|
||||
#else
|
||||
localtime_r(&tt, &tm);
|
||||
#endif
|
||||
char buf[32];
|
||||
std::strftime(buf, sizeof(buf), "%F %T", &tm);
|
||||
return std::string{buf};
|
||||
}
|
||||
|
||||
void ConsoleSink::write(const Message& msg) {
|
||||
const auto line = std::format("{} {} {:>5} {}\n",
|
||||
formatTimestamp(msg.ts),
|
||||
msg.prefix,
|
||||
toString(msg.level),
|
||||
msg.text);
|
||||
std::scoped_lock lk(coutMutex_);
|
||||
std::cout << line;
|
||||
std::cout.flush();
|
||||
}
|
||||
|
||||
} // namespace vl::log
|
||||
@@ -0,0 +1,96 @@
|
||||
#include <Logging/FileSink.hpp>
|
||||
#include <Logging/Level.hpp>
|
||||
#include <chrono>
|
||||
#include <ctime>
|
||||
#include <filesystem>
|
||||
#include <format>
|
||||
#include <stdexcept>
|
||||
|
||||
namespace vl::logging {
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
static std::string formatTimestamp(const std::chrono::system_clock::time_point& tp) {
|
||||
const auto tt = std::chrono::system_clock::to_time_t(tp);
|
||||
std::tm tm{};
|
||||
#if defined(_WIN32)
|
||||
localtime_s(&tm, &tt);
|
||||
#else
|
||||
localtime_r(&tt, &tm);
|
||||
#endif
|
||||
char buf[32];
|
||||
std::strftime(buf, sizeof(buf), "%F %T", &tm);
|
||||
return std::string{buf};
|
||||
}
|
||||
|
||||
static std::string ymdOf(const std::chrono::system_clock::time_point& tp) {
|
||||
const auto tt = std::chrono::system_clock::to_time_t(tp);
|
||||
std::tm tm{};
|
||||
#if defined(_WIN32)
|
||||
localtime_s(&tm, &tt);
|
||||
#else
|
||||
localtime_r(&tt, &tm);
|
||||
#endif
|
||||
char buf[16];
|
||||
std::strftime(buf, sizeof(buf), "%F", &tm); // YYYY-MM-DD
|
||||
return std::string{buf};
|
||||
}
|
||||
|
||||
FileSink::FileSink(std::string basePath, std::uintmax_t maxBytes)
|
||||
: base_(std::move(basePath)), maxBytes_(maxBytes) {
|
||||
fs::path p(base_);
|
||||
if (p.has_parent_path()) fs::create_directories(p.parent_path());
|
||||
}
|
||||
|
||||
std::string FileSink::dateOf(const std::chrono::system_clock::time_point& tp) { return ymdOf(tp); }
|
||||
|
||||
std::string FileSink::filenameFor(const std::string& base, std::string_view date, int index) {
|
||||
// base_YYYY-MM-DD_index.log
|
||||
return std::format("{}_{}_{}.log", base, date, index);
|
||||
}
|
||||
|
||||
void FileSink::openCurrent() {
|
||||
const auto name = filenameFor(base_, currentDate_, index_);
|
||||
out_.close();
|
||||
out_.clear();
|
||||
out_.open(name, std::ios::out | std::ios::app | std::ios::binary);
|
||||
if (!out_) throw std::runtime_error(std::format("FileSink: cannot open '{}'", name));
|
||||
try { written_ = fs::file_size(name); } catch (...) { written_ = 0; }
|
||||
}
|
||||
|
||||
void FileSink::rotateToDate(std::string newDate) {
|
||||
currentDate_ = std::move(newDate);
|
||||
index_ = 1;
|
||||
openCurrent();
|
||||
}
|
||||
|
||||
void FileSink::ensureOpen(const Message& msg) {
|
||||
const auto msgDate = dateOf(msg.ts);
|
||||
if (!out_.is_open()) {
|
||||
currentDate_ = msgDate;
|
||||
index_ = 1;
|
||||
openCurrent();
|
||||
return;
|
||||
}
|
||||
if (msgDate != currentDate_) {
|
||||
rotateToDate(msgDate);
|
||||
return;
|
||||
}
|
||||
if (maxBytes_ > 0 && written_ >= maxBytes_) {
|
||||
++index_;
|
||||
openCurrent();
|
||||
}
|
||||
}
|
||||
|
||||
void FileSink::write(const Message& msg) {
|
||||
ensureOpen(msg);
|
||||
const auto line = std::format("{} {} {:>5} {}\n",
|
||||
formatTimestamp(msg.ts),
|
||||
msg.prefix,
|
||||
toString(msg.level),
|
||||
msg.text);
|
||||
out_ << line;
|
||||
written_ += static_cast<std::uintmax_t>(line.size());
|
||||
out_.flush();
|
||||
}
|
||||
|
||||
} // namespace vl::log
|
||||
@@ -0,0 +1,48 @@
|
||||
#include <Logging/LoggerProvider.hpp>
|
||||
#include <Logging/LoggerInternal.hpp>
|
||||
#include <Logging/ConsoleSink.hpp>
|
||||
#include <Logging/FileSink.hpp>
|
||||
#include <Logging/CombinedSink.hpp>
|
||||
|
||||
namespace vl::logging {
|
||||
|
||||
std::shared_ptr<Logger> LoggerProvider::emplaceOrGet_(std::string prefix,
|
||||
std::shared_ptr<Sink> sink,
|
||||
Level lvl) {
|
||||
std::scoped_lock lk(mtx_);
|
||||
if (auto it = byPrefix_.find(prefix); it != byPrefix_.end()) return it->second; // P3
|
||||
auto lg = std::make_shared<LoggerInternal>(prefix, std::move(sink), lvl);
|
||||
byPrefix_.emplace(std::move(prefix), lg);
|
||||
return lg;
|
||||
}
|
||||
|
||||
std::shared_ptr<Logger> LoggerProvider::createConsole(std::string prefix, Level lvl) {
|
||||
return emplaceOrGet_(std::move(prefix), std::make_shared<ConsoleSink>(), lvl);
|
||||
}
|
||||
|
||||
std::shared_ptr<Logger> LoggerProvider::createFile(std::string prefix, std::string basePath,
|
||||
std::uintmax_t maxBytes, Level lvl) {
|
||||
return emplaceOrGet_(std::move(prefix), std::make_shared<FileSink>(std::move(basePath), maxBytes), lvl);
|
||||
}
|
||||
|
||||
std::shared_ptr<Logger> LoggerProvider::createCombined(std::string prefix, std::string basePath,
|
||||
std::uintmax_t maxBytes, Level lvl) {
|
||||
auto a = std::make_shared<ConsoleSink>();
|
||||
auto b = std::make_shared<FileSink>(std::move(basePath), maxBytes);
|
||||
auto s = std::make_shared<CombinedSink>(std::move(a), std::move(b));
|
||||
return emplaceOrGet_(std::move(prefix), std::move(s), lvl);
|
||||
}
|
||||
|
||||
std::shared_ptr<Logger> LoggerProvider::get(std::string_view prefix) const {
|
||||
std::scoped_lock lk(mtx_);
|
||||
auto it = byPrefix_.find(std::string(prefix));
|
||||
if (it == byPrefix_.end()) throw std::runtime_error("LoggerProvider::get: unknown prefix");
|
||||
return it->second;
|
||||
}
|
||||
|
||||
bool LoggerProvider::exists(std::string_view prefix) const {
|
||||
std::scoped_lock lk(mtx_);
|
||||
return byPrefix_.contains(std::string(prefix));
|
||||
}
|
||||
|
||||
} // namespace vl::log
|
||||
@@ -0,0 +1,13 @@
|
||||
#include <Logging/LoggerRegister.hpp>
|
||||
#include <Logging/LoggerManager.hpp>
|
||||
#include <Logging/LoggerProvider.hpp>
|
||||
|
||||
namespace vl::logging {
|
||||
|
||||
static LoggerProvider g_coreProvider; // internal to the Core library
|
||||
|
||||
void init_voidloader_logger() {
|
||||
LoggerManager::instance().setProvider(&g_coreProvider);
|
||||
}
|
||||
|
||||
} // namespace vl::log
|
||||
+16
-2
@@ -3,9 +3,11 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include "Logging/LoggerManager.hpp"
|
||||
#include "jvmti_handler.h"
|
||||
#include "class_transformer.h"
|
||||
#include "mod_manager.h"
|
||||
#include <Logging/LoggerRegister.hpp>
|
||||
|
||||
// Global variables defined ONLY here
|
||||
namespace {
|
||||
@@ -96,13 +98,25 @@ extern "C" JNIEXPORT void JNICALL Java_ru_faumaray_VoidLoaderAgent_nativeAgentSt
|
||||
}
|
||||
|
||||
extern "C" JNIEXPORT void JNICALL Java_ru_faumaray_VoidLoaderAgent_nativeAgentInit(JNIEnv* env, jclass clazz, jstring args, jobject instrumentation) {
|
||||
std::cout << "[VoidLoader] Native agent initialized with Instrumentation" << std::endl;
|
||||
try {
|
||||
// Register logger provider in API singleton
|
||||
vl::logging::init_voidloader_logger();
|
||||
}
|
||||
catch (const std::exception& ex) {
|
||||
// If something fails, return an error code, JVM will abort agent load
|
||||
fprintf(stderr, "[VoidLoaderCore] Logger initialization failed: %s\n", ex.what());
|
||||
return;
|
||||
}
|
||||
auto logger = vl::logging::LoggerManager::instance().createConsole("[Core]");
|
||||
|
||||
logger->info("Native agent initialized with Instrumentation");
|
||||
|
||||
// Initialize mod manager - but don't block
|
||||
g_modManager.discoverMods("mods");
|
||||
JavaVM* vm;
|
||||
auto error = env->GetJavaVM(&vm);
|
||||
if (error != JNI_OK) {
|
||||
std::cerr << "[VoidLoader] FATAL: Unable to get JavaVM from JNIEnv!" << std::endl;
|
||||
logger->error("Unable to get JavaVM from JNIEnv");
|
||||
return;
|
||||
}
|
||||
Agent_OnLoad(vm, nullptr, nullptr);
|
||||
|
||||
Reference in new issue
Block a user