From e48a55eb3cc807fa5a45cf398b6f523e96aa5312 Mon Sep 17 00:00:00 2001
From: LAPTOP-SNT8I5JK\Boounion <Chenluhua@qq.com>
Date: 星期二, 15 七月 2025 15:10:31 +0800
Subject: [PATCH] 1.Variable列表显示到界面; 2.从文件中加载CReport列表;
---
SourceCode/Bond/Servo/SystemLogManager.cpp | 46 +++++++++++++++++++++++++++++++++++-----------
1 files changed, 35 insertions(+), 11 deletions(-)
diff --git a/SourceCode/Bond/Servo/SystemLogManager.cpp b/SourceCode/Bond/Servo/SystemLogManager.cpp
index 7117af7..a62e07e 100644
--- a/SourceCode/Bond/Servo/SystemLogManager.cpp
+++ b/SourceCode/Bond/Servo/SystemLogManager.cpp
@@ -16,25 +16,40 @@
}
// 构造函数
-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,
@@ -48,6 +63,15 @@
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) {
--
Gitblit v1.9.3