#include "stdafx.h" #include "GENErrorWarningProcess.h" #include #include #include #include #include #include #include extern Log4CPP::Logger* mLog::gLogger; DeviceErrorHandler::DeviceErrorHandler(std::shared_ptr EventCenter, std::string DevInstance, std::string strInfoPath) { m_MSGUnit.reset(new nDetail::MSGUnit(EventCenter, DevInstance)); m_EventCenter = EventCenter; mLog::FINFO("strInfoPath:{$}", strInfoPath); LoadConfig(strInfoPath); } DeviceErrorHandler::~DeviceErrorHandler() { m_activeErrors.clear(); m_activeWarnings.clear(); } bool DeviceErrorHandler::LoadConfig(const std::string& configPath) { mLog::FINFO("Loading error configuration from: {$}", configPath.c_str()); std::lock_guard lock(m_mutex); try { ResDataObject r_config; // 文件存在性检查 if (!std::filesystem::exists(configPath)) { mLog::FERROR("Config file not found: {$}" , configPath.c_str()); return false; } if (!std::filesystem::is_regular_file(configPath)) { mLog::FERROR("Invalid configuration file path: {$}", configPath.c_str()); return false; } if (!r_config.loadFile(configPath.c_str())) { mLog::FERROR("Failed to parse config file: {$}", configPath.c_str()); return false; } m_errorConfigs.clear(); for (int x = 0; x < r_config.size(); ++x) { std::string key = r_config.GetKey(x); ErrorConfig config{ r_config[x]["code"], (int)r_config[x]["level"], r_config[x]["message"], r_config[x]["type"] }; if (config.type != "error" && config.type != "warning") { mLog::FWARN("Invalid type '{$}' for code {$}, skipping entry", config.type.c_str(), key.c_str()); continue; } m_errorConfigs[key] = config; /*mLog::FDEBUG("Loaded {$} configuration: code={$}, level={$}, type={$}", config.type.c_str(), config.code.c_str(), config.level, key.c_str());*/ } mLog::FINFO("Successfully loaded {$} error configurations", m_errorConfigs.size()); return true; } catch (const std::exception& e) { mLog::FERROR("Exception occurred while loading config: {$}", e.what()); return false; } } std::string DeviceErrorHandler::ParseAndReport(const std::string& rawCode) { std::lock_guard lock(m_mutex); mLog::FDEBUG("Processing raw error code: {$}", rawCode.c_str()); auto it = m_errorConfigs.find(rawCode); if (it == m_errorConfigs.end()) { mLog::FWARN("Unknown error code: {$}" , rawCode.c_str()); return "unknow"; } const auto& config = it->second; mLog::FINFO("Processing {$}: code={$}, level={$}, message={$}", config.type.c_str(), config.code.c_str(), config.level, config.message.c_str()); std::string type = config.type; if (config.type == "error") { AddError(config.code, config.level, config.message); } else if (config.type == "warning") { AddWarning(config.code, config.level, config.message); } else { type = "unknow"; mLog::FERROR("Invalid error type '{$}' for code {$}", config.type.c_str(), rawCode.c_str()); } return type; } void DeviceErrorHandler::ClearAllErrors() { std::lock_guard lock(m_mutex); mLog::FINFO("Clearing all errors (count: {$})", m_activeErrors.size()); for (const auto& err : m_activeErrors) { int level = err.level; m_MSGUnit->DelErrorMessage(err.code.c_str(), level, err.message.c_str()); } m_activeErrors.clear(); mLog::FINFO("All errors cleared"); } void DeviceErrorHandler::ClearAllWarnings() { std::lock_guard lock(m_mutex); mLog::FINFO("Clearing all warnings (count: {$})", m_activeWarnings.size()); for (const auto& warn : m_activeWarnings) { int level = warn.level; m_MSGUnit->DelWarnMessage(warn.code.c_str(), level, warn.message.c_str()); } m_activeWarnings.clear(); mLog::FINFO("All warnings cleared"); } void DeviceErrorHandler::AddError(std::string code, int level, const std::string& message) { mLog::FINFO("AddError : code={$}, level={$}, message={$}",code.c_str(), level, message.c_str()); for (const auto& error : m_activeErrors) { if (error.code == code && error.level == level) { mLog::FDEBUG("Error already active: code={$}, level={$}", code.c_str(), level); return; // 错误已存在,直接返回 } } int tmpLevel = level; m_MSGUnit->AddErrorMessage(code.c_str(), tmpLevel, message.c_str()); m_activeErrors.push_back({ code, level, message }); } bool DeviceErrorHandler::HasActiveErrors() const { std::lock_guard lock(m_mutex); mLog::FDEBUG("Checking active errors. Current count: {$}", m_activeErrors.size()); return !m_activeErrors.empty(); } bool DeviceErrorHandler::HasActiveWarnings() const { std::lock_guard lock(m_mutex); mLog::FDEBUG("Checking active warnings. Current count: {$}", m_activeWarnings.size()); return !m_activeWarnings.empty(); } void DeviceErrorHandler::AddWarning(std::string code, int level, const std::string& message) { for (const auto& warning : m_activeWarnings) { if (warning.code == code && warning.level == level) { mLog::FDEBUG("Warning already active: code={$}, level={$}", code.c_str(), level); return; // 警告已存在,直接返回 } } int tmpLevel = level; m_MSGUnit->AddWarnMessage(code.c_str(), tmpLevel, message.c_str()); m_activeWarnings.push_back({ code, level, message }); }