| | |
| | | } |
| | | |
| | | // 构造函数 |
| | | SystemLogManager::SystemLogManager() : m_pDB(nullptr) {} |
| | | SystemLogManager::SystemLogManager() : m_pDB(nullptr) { |
| | | m_pDB = new BL::SQLiteDatabase(); |
| | | } |
| | | |
| | | // 析构函数 |
| | | SystemLogManager::~SystemLogManager() { |
| | | m_pDB = nullptr; // 清除指针引用 |
| | | } |
| | | |
| | | // 设置数据库连接 |
| | | void SystemLogManager::setDatabase(BL::Database* db) { |
| | | std::lock_guard<std::mutex> lock(m_mutex); |
| | | m_pDB = db; |
| | | if (m_pDB) { |
| | | delete m_pDB; |
| | | m_pDB = nullptr; |
| | | } |
| | | } |
| | | |
| | | // 初始化日志表 |
| | | bool SystemLogManager::initializeLogTable() { |
| | | if (!m_pDB) { |
| | | throw std::runtime_error("Database connection is not set."); |
| | | bool SystemLogManager::initSystemLogTable() { |
| | | // 获取可执行文件路径 |
| | | char szPath[MAX_PATH]; |
| | | GetModuleFileName(NULL, szPath, MAX_PATH); |
| | | std::string exePath(szPath); |
| | | std::string dbDir = exePath.substr(0, exePath.find_last_of("\\/")) + "\\DB"; |
| | | |
| | | // 创建 DB 目录 |
| | | if (!CreateDirectory(dbDir.c_str(), NULL) && GetLastError() != ERROR_ALREADY_EXISTS) { |
| | | throw std::runtime_error("创建数据库目录失败"); |
| | | } |
| | | |
| | | // 构造数据库路径 |
| | | std::string dbPath = dbDir + "\\SystemLogManager.db"; |
| | | |
| | | // 连接数据库 |
| | | if (!m_pDB->connect(dbPath, true)) { |
| | | throw std::runtime_error("连接日志数据库失败"); |
| | | } |
| | | |
| | | // 创建日志表 SQL 语句 |
| | | const std::string createTableQuery = R"( |
| | | CREATE TABLE IF NOT EXISTS system_logs ( |
| | | id INTEGER PRIMARY KEY AUTOINCREMENT, |
| | |
| | | return m_pDB->executeQuery(createTableQuery); |
| | | } |
| | | |
| | | // 终止数据库连接 |
| | | void SystemLogManager::termSystemLogTable() { |
| | | if (!m_pDB) { |
| | | return; |
| | | } |
| | | |
| | | m_pDB->disconnect(); |
| | | } |
| | | |
| | | // 添加日志(使用当前用户) |
| | | bool SystemLogManager::log(LogType logType, const std::string& event) { |
| | | if (!m_pDB) { |