| | |
| | | #include "stdafx.h" |
| | | #include "SystemLogManager.h" |
| | | #include <iostream> |
| | | #include <sstream> |
| | | #include <stdexcept> |
| | |
| | | log_type TEXT NOT NULL, |
| | | event TEXT NOT NULL, |
| | | username TEXT NOT NULL, |
| | | timestamp DATETIME DEFAULT CURRENT_TIMESTAMP |
| | | timestamp DATETIME DEFAULT (datetime('now', 'localtime')) |
| | | ) |
| | | )"; |
| | | return (*m_pDB)->executeQuery(createTableQuery); |
| | | } |
| | | |
| | | // 添加日志 |
| | | bool SystemLogManager::log(LogType logType, const std::string& event) { |
| | | if (!m_pDB || !(*m_pDB)) { |
| | | std::cerr << "Database connection is not set." << std::endl; |
| | | return false; |
| | | } |
| | | |
| | | // 清理旧日志 |
| | | cleanOldLogs(30); |
| | | |
| | | std::string strUsername = UserManager::getInstance().getCurrentUser(); |
| | | if (strUsername.empty()) { |
| | | strUsername = "SYSTEM"; |
| | | } |
| | | |
| | | std::ostringstream query; |
| | | query << "INSERT INTO system_logs (log_type, event, username) VALUES (" |
| | | << "'" << logTypeToString(logType) << "', " |
| | | << "'" << event << "', " |
| | | << "'" << strUsername << "')"; |
| | | return (*m_pDB)->executeQuery(query.str()); |
| | | } |
| | | |
| | | bool SystemLogManager::log(LogType logType, const std::string& event, const std::string& username) { |
| | | if (!m_pDB || !(*m_pDB)) { |
| | | std::cerr << "Database connection is not set." << std::endl; |
| | | return false; |
| | | } |
| | | |
| | | std::vector<std::string> vecUserInfo = UserManager::getInstance().getUserInfo(username); |
| | | if (username.empty() || vecUserInfo.empty()) { |
| | | std::cerr << "Username empty." << std::endl; |
| | | return false; |
| | | } |
| | | |
| | | // 清理旧日志 |
| | | cleanOldLogs(30); |
| | | |
| | | std::ostringstream query; |
| | | query << "INSERT INTO system_logs (log_type, event, username) VALUES (" |
| | |
| | | return logs; |
| | | } |
| | | |
| | | // 获取筛选后的日志数据 |
| | | 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 || !(*m_pDB)) { |
| | | throw std::runtime_error("Database connection is not set."); |
| | | } |
| | | |
| | | // 构建基础 SQL 查询 |
| | | std::ostringstream query; |
| | | query << "SELECT id, log_type, event, username, timestamp 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 << "'"; |
| | | } |
| | | |
| | | // 分页逻辑:设置 LIMIT 和 OFFSET |
| | | int offset = (pageNumber - 1) * pageSize; |
| | | query << " ORDER BY timestamp DESC"; // 按时间降序排列 |
| | | query << " 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 || !(*m_pDB)) { |
| | | throw std::runtime_error("Database connection is not set."); |
| | | } |
| | | |
| | | // 构建基础 SQL 查询 |
| | | 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()); |
| | | |
| | | // 返回记录总数 |
| | | if (!results.empty() && !results[0].empty()) { |
| | | return std::stoi(results[0][0]); // 第一行第一列是计数值 |
| | | } |
| | | |
| | | return 0; // 如果没有结果,返回 0 |
| | | } |
| | | |
| | | void SystemLogManager::cleanOldLogs(int daysToKeep) |
| | | { |
| | | if (!m_pDB || !(*m_pDB)) { |
| | | throw std::runtime_error("Database connection is not set."); |
| | | } |
| | | |
| | | // 获取当前日期并计算截断日期 |
| | | time_t now = time(nullptr); |
| | | tm timeInfo; |
| | | localtime_s(&timeInfo, &now); |
| | | timeInfo.tm_mday -= daysToKeep; // 计算指定天数之前的日期 |
| | | mktime(&timeInfo); // 规范化日期 |
| | | |
| | | char buffer[20]; |
| | | strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", &timeInfo); |
| | | std::string cutoffDate(buffer); |
| | | |
| | | std::ostringstream query; |
| | | query << "DELETE FROM system_logs WHERE timestamp < '" << cutoffDate << "'"; |
| | | bool ret = (*m_pDB)->executeQuery(query.str()); |
| | | if (!ret) { |
| | | throw std::runtime_error("System Log cleanup operation failed."); |
| | | } |
| | | } |
| | | |
| | | // 转换日志类型为字符串 |
| | | std::string SystemLogManager::logTypeToString(LogType logType) const { |
| | | switch (logType) { |
| | |
| | | default: |
| | | return _T("δ֪"); |
| | | } |
| | | } |
| | | } |