windows-filter-windivert/include/logger.h

73 lines
1.5 KiB
C++

#ifndef LOGGER_H
#define LOGGER_H
#include "common.h"
#include <string>
#include <fstream>
#include <mutex>
#include <vector>
class Logger {
public:
enum LogLevel {
DEBUG,
INFO,
WARN,
ERROR
};
static Logger& GetInstance();
// Initialize logger
bool Initialize(const std::string& log_file,
LogLevel level = INFO,
size_t max_size_mb = 100);
// Log methods
void LogDNSQuery(const std::string& domain,
const std::string& client_ip,
FilterAction action);
void LogInfo(const std::string& message);
void LogError(const std::string& message);
void LogDebug(const std::string& message);
void LogWarn(const std::string& message);
// Flush buffered logs
void Flush();
// Close logger
void Close();
private:
Logger() = default;
~Logger();
// Prevent copying
Logger(const Logger&) = delete;
Logger& operator=(const Logger&) = delete;
std::ofstream log_file_;
std::string log_path_;
LogLevel current_level_;
size_t max_size_bytes_;
std::atomic<size_t> current_size_;
// Thread safety
std::mutex log_mutex_;
// Buffering
std::vector<std::string> buffer_;
size_t buffer_threshold_ = 100;
bool initialized_ = false;
// Helper functions
void WriteLog(LogLevel level, const std::string& message);
void RotateLogFile();
std::string GetTimestamp() const;
std::string LogLevelToString(LogLevel level) const;
};
#endif // LOGGER_H