GENErrorWarningProcess.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. #include "stdafx.h"
  2. #include "GENErrorWarningProcess.h"
  3. #include <string>
  4. #include <unordered_map>
  5. #include <vector>
  6. #include <mutex>
  7. #include <fstream>
  8. #include <sstream>
  9. #include <filesystem>
  10. extern Log4CPP::Logger* mLog::gLogger;
  11. DeviceErrorHandler::DeviceErrorHandler(std::shared_ptr<DIOS::Dev::IOEventCenter> EventCenter, std::string DevInstance, std::string strInfoPath)
  12. {
  13. m_MSGUnit.reset(new nDetail::MSGUnit(EventCenter, DevInstance));
  14. m_EventCenter = EventCenter;
  15. mLog::FINFO("strInfoPath:{$}", strInfoPath);
  16. LoadConfig(strInfoPath);
  17. }
  18. DeviceErrorHandler::~DeviceErrorHandler()
  19. {
  20. m_activeErrors.clear();
  21. m_activeWarnings.clear();
  22. }
  23. bool DeviceErrorHandler::LoadConfig(const std::string& configPath)
  24. {
  25. mLog::FINFO("Loading error configuration from: {$}", configPath.c_str());
  26. std::lock_guard<std::mutex> lock(m_mutex);
  27. try {
  28. ResDataObject r_config;
  29. // 文件存在性检查
  30. if (!std::filesystem::exists(configPath)) {
  31. mLog::FERROR("Config file not found: {$}" , configPath.c_str());
  32. return false;
  33. }
  34. if (!std::filesystem::is_regular_file(configPath)) {
  35. mLog::FERROR("Invalid configuration file path: {$}", configPath.c_str());
  36. return false;
  37. }
  38. if (!r_config.loadFile(configPath.c_str())) {
  39. mLog::FERROR("Failed to parse config file: {$}", configPath.c_str());
  40. return false;
  41. }
  42. m_errorConfigs.clear();
  43. for (int x = 0; x < r_config.size(); ++x)
  44. {
  45. std::string key = r_config.GetKey(x);
  46. ErrorConfig config{
  47. r_config[x]["code"],
  48. (int)r_config[x]["level"],
  49. r_config[x]["message"],
  50. r_config[x]["type"]
  51. };
  52. if (config.type != "error" && config.type != "warning") {
  53. mLog::FWARN("Invalid type '{$}' for code {$}, skipping entry", config.type.c_str(), key.c_str());
  54. continue;
  55. }
  56. m_errorConfigs[key] = config;
  57. /*mLog::FDEBUG("Loaded {$} configuration: code={$}, level={$}, type={$}",
  58. config.type.c_str(), config.code.c_str(), config.level, key.c_str());*/
  59. }
  60. mLog::FINFO("Successfully loaded {$} error configurations", m_errorConfigs.size());
  61. return true;
  62. }
  63. catch (const std::exception& e) {
  64. mLog::FERROR("Exception occurred while loading config: {$}", e.what());
  65. return false;
  66. }
  67. }
  68. std::string DeviceErrorHandler::ParseAndReport(const std::string& rawCode)
  69. {
  70. std::lock_guard<std::mutex> lock(m_mutex);
  71. mLog::FDEBUG("Processing raw error code: {$}", rawCode.c_str());
  72. auto it = m_errorConfigs.find(rawCode);
  73. if (it == m_errorConfigs.end()) {
  74. mLog::FWARN("Unknown error code: {$}" , rawCode.c_str());
  75. return "unknow";
  76. }
  77. const auto& config = it->second;
  78. mLog::FINFO("Processing {$}: code={$}, level={$}, message={$}",
  79. config.type.c_str(), config.code.c_str(), config.level, config.message.c_str());
  80. std::string type = config.type;
  81. if (config.type == "error") {
  82. AddError(config.code, config.level, config.message);
  83. }
  84. else if (config.type == "warning") {
  85. AddWarning(config.code, config.level, config.message);
  86. }
  87. else {
  88. type = "unknow";
  89. mLog::FERROR("Invalid error type '{$}' for code {$}", config.type.c_str(), rawCode.c_str());
  90. }
  91. return type;
  92. }
  93. void DeviceErrorHandler::ClearAllErrors()
  94. {
  95. std::lock_guard<std::mutex> lock(m_mutex);
  96. mLog::FINFO("Clearing all errors (count: {$})", m_activeErrors.size());
  97. for (const auto& err : m_activeErrors) {
  98. int level = err.level;
  99. m_MSGUnit->DelErrorMessage(err.code.c_str(), level, err.message.c_str());
  100. }
  101. m_activeErrors.clear();
  102. mLog::FINFO("All errors cleared");
  103. }
  104. void DeviceErrorHandler::ClearAllWarnings()
  105. {
  106. std::lock_guard<std::mutex> lock(m_mutex);
  107. mLog::FINFO("Clearing all warnings (count: {$})", m_activeWarnings.size());
  108. for (const auto& warn : m_activeWarnings) {
  109. int level = warn.level;
  110. m_MSGUnit->DelWarnMessage(warn.code.c_str(), level, warn.message.c_str());
  111. }
  112. m_activeWarnings.clear();
  113. mLog::FINFO("All warnings cleared");
  114. }
  115. void DeviceErrorHandler::AddError(std::string code, int level, const std::string& message)
  116. {
  117. mLog::FINFO("AddError : code={$}, level={$}, message={$}",code.c_str(), level, message.c_str());
  118. for (const auto& error : m_activeErrors) {
  119. if (error.code == code && error.level == level) {
  120. mLog::FDEBUG("Error already active: code={$}, level={$}", code.c_str(), level);
  121. return; // 错误已存在,直接返回
  122. }
  123. }
  124. int tmpLevel = level;
  125. m_MSGUnit->AddErrorMessage(code.c_str(), tmpLevel, message.c_str());
  126. m_activeErrors.push_back({ code, level, message });
  127. }
  128. bool DeviceErrorHandler::HasActiveErrors() const
  129. {
  130. std::lock_guard<std::mutex> lock(m_mutex);
  131. mLog::FDEBUG("Checking active errors. Current count: {$}", m_activeErrors.size());
  132. return !m_activeErrors.empty();
  133. }
  134. bool DeviceErrorHandler::HasActiveWarnings() const
  135. {
  136. std::lock_guard<std::mutex> lock(m_mutex);
  137. mLog::FDEBUG("Checking active warnings. Current count: {$}", m_activeWarnings.size());
  138. return !m_activeWarnings.empty();
  139. }
  140. void DeviceErrorHandler::AddWarning(std::string code, int level, const std::string& message)
  141. {
  142. for (const auto& warning : m_activeWarnings) {
  143. if (warning.code == code && warning.level == level) {
  144. mLog::FDEBUG("Warning already active: code={$}, level={$}", code.c_str(), level);
  145. return; // 警告已存在,直接返回
  146. }
  147. }
  148. int tmpLevel = level;
  149. m_MSGUnit->AddWarnMessage(code.c_str(), tmpLevel, message.c_str());
  150. m_activeWarnings.push_back({ code, level, message });
  151. }