| ¶Ô±ÈÐÂÎļþ |
| | |
| | | #include "stdafx.h" |
| | | #include <sstream> |
| | | #include <iostream> |
| | | #include <stdexcept> |
| | | #include <ctime> |
| | | |
| | | // éææååå§å |
| | | std::mutex SystemLogManager::m_mutex; |
| | | |
| | | // è·ååä¾å®ä¾ |
| | | SystemLogManager& SystemLogManager::getInstance() { |
| | | static SystemLogManager instance; |
| | | return instance; |
| | | } |
| | | |
| | | // æé 彿° |
| | | SystemLogManager::SystemLogManager() : m_pDB(nullptr) {} |
| | | |
| | | // ææå½æ° |
| | | SystemLogManager::~SystemLogManager() { |
| | | m_pDB = nullptr; // æ¸
餿éå¼ç¨ |
| | | } |
| | | |
| | | // è®¾ç½®æ°æ®åºè¿æ¥ |
| | | void SystemLogManager::setDatabase(BL::Database* db) { |
| | | std::lock_guard<std::mutex> lock(m_mutex); |
| | | m_pDB = db; |
| | | } |
| | | |
| | | // åå§åæ¥å¿è¡¨ |
| | | bool SystemLogManager::initializeLogTable() { |
| | | if (!m_pDB) { |
| | | throw std::runtime_error("Database connection is not set."); |
| | | } |
| | | |
| | | const std::string createTableQuery = R"( |
| | | CREATE TABLE IF NOT EXISTS system_logs ( |
| | | id INTEGER PRIMARY KEY AUTOINCREMENT, |
| | | log_type TEXT NOT NULL, |
| | | event TEXT NOT NULL, |
| | | username TEXT NOT NULL, |
| | | timestamp DATETIME DEFAULT (datetime('now', 'localtime')) |
| | | ) |
| | | )"; |
| | | |
| | | return m_pDB->executeQuery(createTableQuery); |
| | | } |
| | | |
| | | // æ·»å æ¥å¿ï¼ä½¿ç¨å½åç¨æ·ï¼ |
| | | bool SystemLogManager::log(LogType logType, const std::string& event) { |
| | | if (!m_pDB) { |
| | | throw std::runtime_error("Database connection is not set."); |
| | | } |
| | | |
| | | cleanOldLogs(); |
| | | |
| | | std::string username = UserManager::getInstance().getCurrentUser(); |
| | | if (username.empty()) { |
| | | username = "SYSTEM"; |
| | | } |
| | | |
| | | std::ostringstream query; |
| | | query << "INSERT INTO system_logs (log_type, event, username) VALUES (" |
| | | << "'" << logTypeToString(logType) << "', " |
| | | << "'" << event << "', " |
| | | << "'" << username << "')"; |
| | | |
| | | std::lock_guard<std::mutex> lock(m_mutex); |
| | | return m_pDB->executeQuery(query.str()); |
| | | } |
| | | |
| | | // æ·»å æ¥å¿ï¼æå®ç¨æ·ï¼ |
| | | bool SystemLogManager::log(LogType logType, const std::string& event, const std::string& username) { |
| | | if (!m_pDB) { |
| | | throw std::runtime_error("Database connection is not set."); |
| | | } |
| | | |
| | | cleanOldLogs(); |
| | | |
| | | std::ostringstream query; |
| | | query << "INSERT INTO system_logs (log_type, event, username) VALUES (" |
| | | << "'" << logTypeToString(logType) << "', " |
| | | << "'" << event << "', " |
| | | << "'" << username << "')"; |
| | | |
| | | std::lock_guard<std::mutex> lock(m_mutex); |
| | | return m_pDB->executeQuery(query.str()); |
| | | } |
| | | |
| | | // è·åæ¥å¿å
容 |
| | | std::vector<std::vector<std::string>> SystemLogManager::getLogs(int startPosition, int count) { |
| | | if (!m_pDB) { |
| | | throw std::runtime_error("Database connection is not set."); |
| | | } |
| | | |
| | | std::ostringstream query; |
| | | if (startPosition == -1 && count == -1) { |
| | | query << "SELECT id, log_type, event, username, datetime(timestamp, 'localtime') FROM system_logs"; |
| | | } |
| | | else { |
| | | query << "SELECT id, log_type, event, username, datetime(timestamp, 'localtime') FROM system_logs " |
| | | << "LIMIT " << count << " OFFSET " << startPosition; |
| | | } |
| | | |
| | | return m_pDB->fetchResults(query.str()); |
| | | } |
| | | |
| | | // è·åçéåçæ¥å¿æ°æ® |
| | | std::vector<std::vector<std::string>> SystemLogManager::getFilteredLogs( |
| | | const std::string& logType, |
| | | const std::string& username, |
| | | const std::string& description, |
| | | const std::string& startTime, |
| | | const std::string& endTime, |
| | | int pageNumber, |
| | | int pageSize) |
| | | { |
| | | if (!m_pDB) { |
| | | throw std::runtime_error("Database connection is not set."); |
| | | } |
| | | |
| | | std::ostringstream query; |
| | | query << "SELECT id, log_type, event, username, datetime(timestamp, 'localtime') FROM system_logs WHERE 1=1"; |
| | | |
| | | if (logType != "ALL") { |
| | | query << " AND log_type = '" << logType << "'"; |
| | | } |
| | | if (username != "ALL") { |
| | | query << " AND username = '" << username << "'"; |
| | | } |
| | | if (!description.empty()) { |
| | | query << " AND event LIKE '%" << description << "%'"; |
| | | } |
| | | if (!startTime.empty()) { |
| | | query << " AND timestamp >= '" << startTime << "'"; |
| | | } |
| | | if (!endTime.empty()) { |
| | | query << " AND timestamp <= '" << endTime << "'"; |
| | | } |
| | | |
| | | int offset = (pageNumber - 1) * pageSize; |
| | | query << " ORDER BY timestamp DESC LIMIT " << pageSize << " OFFSET " << offset; |
| | | |
| | | return m_pDB->fetchResults(query.str()); |
| | | } |
| | | |
| | | // è·åç¬¦åæ¡ä»¶çæ¥å¿æ»æ° |
| | | int SystemLogManager::getTotalLogCount( |
| | | const std::string& logType, |
| | | const std::string& username, |
| | | const std::string& description, |
| | | const std::string& startTime, |
| | | const std::string& endTime) |
| | | { |
| | | if (!m_pDB) { |
| | | throw std::runtime_error("Database connection is not set."); |
| | | } |
| | | |
| | | std::ostringstream query; |
| | | query << "SELECT COUNT(*) FROM system_logs WHERE 1=1"; |
| | | |
| | | if (logType != "ALL") { |
| | | query << " AND log_type = '" << logType << "'"; |
| | | } |
| | | if (username != "ALL") { |
| | | query << " AND username = '" << username << "'"; |
| | | } |
| | | if (!description.empty()) { |
| | | query << " AND event LIKE '%" << description << "%'"; |
| | | } |
| | | if (!startTime.empty()) { |
| | | query << " AND timestamp >= '" << startTime << "'"; |
| | | } |
| | | if (!endTime.empty()) { |
| | | query << " AND timestamp <= '" << endTime << "'"; |
| | | } |
| | | |
| | | auto results = m_pDB->fetchResults(query.str()); |
| | | return (!results.empty() && !results[0].empty()) ? std::stoi(results[0][0]) : 0; |
| | | } |
| | | |
| | | // æ¸
çè¶
è¿æå®å¤©æ°çæ§æ¥å¿ |
| | | void SystemLogManager::cleanOldLogs(int daysToKeep) { |
| | | if (!m_pDB) { |
| | | throw std::runtime_error("Database connection is not set."); |
| | | } |
| | | |
| | | std::ostringstream query; |
| | | query << "DELETE FROM system_logs WHERE timestamp < datetime('now', '-" << daysToKeep << " days')"; |
| | | m_pDB->executeQuery(query.str()); |
| | | } |
| | | |
| | | // è½¬æ¢æ¥å¿ç±»å为å符串 |
| | | std::string SystemLogManager::logTypeToString(LogType logType) { |
| | | switch (logType) { |
| | | case LogType::Info: |
| | | return "ä¿¡æ¯"; |
| | | case LogType::Error: |
| | | return "é误"; |
| | | case LogType::Operation: |
| | | return "æä½"; |
| | | default: |
| | | return "δ֪"; |
| | | } |
| | | } |