Migrate core infrastructure to VoidLibrary
This commit is contained in:
1 parent
53801f2b53
commit
2bdb63766d
65 files changed
+16109
-12011
No files matched your search
@@ -1,79 +1,77 @@
|
||||
#pragma once
|
||||
|
||||
#include <echoion/api.hpp>
|
||||
#include <fstream>
|
||||
#include <mutex>
|
||||
#include <queue>
|
||||
#include <thread>
|
||||
#include <condition_variable>
|
||||
#include <LoggerInterface.hpp>
|
||||
#include <atomic>
|
||||
#include <cstdint>
|
||||
#include <filesystem>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <chrono>
|
||||
#include <array>
|
||||
#include <map>
|
||||
|
||||
namespace Echoion::Core {
|
||||
namespace Void::Logging
|
||||
{
|
||||
class MultiLogger;
|
||||
} // namespace Void::Logging
|
||||
|
||||
// Log file info for rotation
|
||||
struct LogFileInfo {
|
||||
std::filesystem::path path;
|
||||
std::ofstream stream;
|
||||
uint64_t size = 0;
|
||||
std::chrono::system_clock::time_point createdAt;
|
||||
};
|
||||
namespace Echoion::Core
|
||||
{
|
||||
|
||||
class Logger : public Api::ILogger {
|
||||
public:
|
||||
explicit Logger(std::string_view logDir = "logs", std::string_view baseName = "echoion");
|
||||
~Logger() override;
|
||||
// ILogger implementation backed by the Void::Logging framework.
|
||||
//
|
||||
// Output is produced by a Void MultiLogger fanning out to:
|
||||
// * a ConsoleLogger (stdout),
|
||||
// * a SplitFileLogger writing per-level files (<dir>/<level>.log) plus a
|
||||
// combined <dir>/all.log, each rolling independently by size, and
|
||||
// * a CallbackLogger bridging back to the registered ILogger callbacks.
|
||||
// The ILogger surface (levels, callbacks, console/file toggles, rotation
|
||||
// knobs) is preserved so existing call sites and the ECHOION_LOG_* macros
|
||||
// keep working unchanged.
|
||||
class Logger : public Api::ILogger
|
||||
{
|
||||
public:
|
||||
|
||||
void log(Api::LogLevel level, std::string_view message,
|
||||
std::source_location location) override;
|
||||
void setMinLevel(Api::LogLevel level) override;
|
||||
[[nodiscard]] Api::LogLevel getMinLevel() const override;
|
||||
void registerCallback(const LogCallback& callback) override;
|
||||
void unregisterCallback(const LogCallback& callback) override;
|
||||
explicit Logger(std::string_view logDir = "logs",
|
||||
std::string_view baseName = "echoion");
|
||||
~Logger() override;
|
||||
|
||||
void flush();
|
||||
void setConsoleOutput(bool enabled);
|
||||
void setFileOutput(bool enabled);
|
||||
void setMaxFileSize(uint64_t bytes);
|
||||
void setMaxFiles(uint32_t count);
|
||||
void log(Api::LogLevel level, std::string_view message,
|
||||
std::source_location location) override;
|
||||
void setMinLevel(Api::LogLevel level) override;
|
||||
[[nodiscard]] Api::LogLevel getMinLevel() const override;
|
||||
void registerCallback(const LogCallback& callback) override;
|
||||
void unregisterCallback(const LogCallback& callback) override;
|
||||
|
||||
private:
|
||||
void logWorker();
|
||||
void rotateIfNeeded(Api::LogLevel level);
|
||||
void openLogFiles();
|
||||
void cleanupOldLogs();
|
||||
void moveToHistory(const std::filesystem::path& filePath);
|
||||
void ensureHistoryDirExists();
|
||||
[[nodiscard]] std::string formatLogEntry(const Api::LogEntry& entry);
|
||||
[[nodiscard]] std::string levelToString(Api::LogLevel level);
|
||||
[[nodiscard]] std::filesystem::path generateLogFilePath(Api::LogLevel level);
|
||||
[[nodiscard]] std::string formatTimestamp(const std::chrono::system_clock::time_point& tp);
|
||||
void writeToFile(Api::LogLevel level, const std::string& formattedEntry);
|
||||
void flush();
|
||||
void setConsoleOutput(bool enabled);
|
||||
void setFileOutput(bool enabled);
|
||||
void setMaxFileSize(uint64_t bytes);
|
||||
void setMaxFiles(uint32_t count);
|
||||
|
||||
std::filesystem::path logDir_;
|
||||
std::string baseName_;
|
||||
|
||||
// Separate files for each level + combined file
|
||||
std::map<Api::LogLevel, LogFileInfo> levelFiles_;
|
||||
LogFileInfo combinedFile_;
|
||||
|
||||
std::mutex fileMutex_;
|
||||
std::mutex queueMutex_;
|
||||
std::queue<Api::LogEntry> logQueue_;
|
||||
std::thread workerThread_;
|
||||
std::condition_variable cv_;
|
||||
std::atomic<bool> running_{true};
|
||||
std::atomic<Api::LogLevel> minLevel_{Api::LogLevel::Info};
|
||||
std::atomic<bool> consoleOutput_{true};
|
||||
std::atomic<bool> fileOutput_{true};
|
||||
std::atomic<uint64_t> maxFileSize_{10 * 1024 * 1024}; // 10 MB per file
|
||||
std::atomic<uint32_t> maxFiles_{5};
|
||||
std::vector<LogCallback> callbacks_;
|
||||
std::mutex callbackMutex_;
|
||||
};
|
||||
private:
|
||||
|
||||
// (Re)assemble the Void sink tree from the current toggles/limits.
|
||||
void rebuildSinks();
|
||||
// Fan a Void log entry out to the registered ILogger callbacks.
|
||||
void dispatchToCallbacks(Api::LogLevel level,
|
||||
const std::string& message,
|
||||
const std::string& file, uint32_t line,
|
||||
const std::string& function);
|
||||
|
||||
std::filesystem::path logDir_;
|
||||
std::string baseName_;
|
||||
|
||||
std::shared_ptr<Void::Logging::MultiLogger> sink_;
|
||||
std::mutex sinkMutex_;
|
||||
|
||||
std::atomic<Api::LogLevel> minLevel_{Api::LogLevel::Info};
|
||||
std::atomic<bool> consoleOutput_{true};
|
||||
std::atomic<bool> fileOutput_{true};
|
||||
std::atomic<uint64_t> maxFileSize_{10 * 1024 * 1024}; // 10 MB
|
||||
std::atomic<uint32_t> maxFiles_{5};
|
||||
|
||||
std::vector<LogCallback> callbacks_;
|
||||
std::mutex callbackMutex_;
|
||||
};
|
||||
|
||||
} // namespace Echoion::Core
|
||||
Reference in new issue
Block a user