COMPASSi/trunk/code/projects/Application/LogWindowUIFunction.cpp

85 lines
2.5 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#pragma execution_character_set("utf-8")
#include "LogWindowUIFunction.h"
#include <mutex>
#define BIND(func) std::bind(&LogWindowUIFunction::func, this, std::placeholders::_1)
std::mutex logMutex; // 用于保护日志输出
// 构造函数:启动子线程
LogWindowUIFunction::LogWindowUIFunction()
: m_running(true)
{
ccsMessageHandle::instance()->register_callback(BIND(out_log), CMD_log_window_out_log);
connect(this, &LogWindowUIFunction::logMessageReceived, this, &LogWindowUIFunction::onLogMessageReceived);
m_logThread = std::thread(&LogWindowUIFunction::logFunction, this);
}
// 析构函数:等待子线程结束
LogWindowUIFunction::~LogWindowUIFunction()
{
ccsMessageHandle::instance()->unregister_callback(CMD_log_window_out_log);
stopThread();
if (m_logThread.joinable())
{
m_logThread.join(); // 等待子线程完成
}
}
void LogWindowUIFunction::out_log(const QString message)
{
}
// `from` 和 `to` 函数可以按需实现,假设是 JSON 数据的处理
void LogWindowUIFunction::from(const json& j)
{
}
void LogWindowUIFunction::to(json j)
{
}
// 子线程的工作函数:模拟日志输出
void LogWindowUIFunction::logFunction()
{
while (m_running)
{
QString strLog = "Simulated log message"; // 示例日志内容
StabilityModel * pModle = StabilityModel::getStabilityModelpPtr();
if(!pModle)
{
continue;
}
bool bRet = pModle->showWindowsLog(strLog);
if (bRet)
{
QtToWebFunction::getInstance().qt2web(CMD_log_window_out_log, EXECUTE_SUCCESS, strLog);
//emit logMessageReceived(strLog); // 发射信号
}
std::this_thread::sleep_for(std::chrono::milliseconds(50)); // 添加延时,避免占用过多资源
}
}
// 停止子线程的函数:设置标志并通知线程退出
void LogWindowUIFunction::stopThread()
{
m_running = false;
}
// 处理日志信息的槽函数
void LogWindowUIFunction::onLogMessageReceived(const QString message)
{
// 假设这里是 UI 中显示日志的代码,可能使用 QTextBrowser 等控件
std::lock_guard<std::mutex> lock(logMutex); // 使用互斥锁保护 UI 操作
// 更新日志窗口的内容
//QString jsonMsg = ccsMessageHandle::instance()->createMsg(CMD_log_window_out_log, EXECUTE_SUCCESS, message);
//ccsMessageHandle::instance()->qt2web(CMD_log_window_out_log, jsonMsg);
int a = 0;
// 可以在这里更新 UI例如通过信号连接
}