80 lines
2.5 KiB
C++
80 lines
2.5 KiB
C++
#pragma once
|
|
|
|
#include <echoion/api.hpp>
|
|
#include <fstream>
|
|
#include <mutex>
|
|
#include <queue>
|
|
#include <thread>
|
|
#include <condition_variable>
|
|
#include <atomic>
|
|
#include <filesystem>
|
|
#include <vector>
|
|
#include <chrono>
|
|
#include <array>
|
|
#include <map>
|
|
|
|
namespace Echoion::Core {
|
|
|
|
// 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;
|
|
};
|
|
|
|
class Logger : public Api::ILogger {
|
|
public:
|
|
explicit Logger(std::string_view logDir = "logs", std::string_view baseName = "echoion");
|
|
~Logger() override;
|
|
|
|
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;
|
|
|
|
void flush();
|
|
void setConsoleOutput(bool enabled);
|
|
void setFileOutput(bool enabled);
|
|
void setMaxFileSize(uint64_t bytes);
|
|
void setMaxFiles(uint32_t count);
|
|
|
|
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);
|
|
|
|
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_;
|
|
};
|
|
|
|
} // namespace Echoion::Core
|