Переглянути джерело

Z991239-5349 #comment fea: add function GetVTMErrMsg, it can storage the error msgs of current entity.

chenliangyu 1 рік тому
батько
коміт
baab775dd7

+ 1 - 0
Framework/Common/SpBase.h

@@ -1020,6 +1020,7 @@ struct IEntityFunction
 	*/
 	virtual ErrorCodeEnum GetRunningVersion(CSimpleString& ver) = 0;
 
+	virtual ErrorCodeEnum GetVTMErrMsg(DWORD dwUserCode, CSimpleStringA& strDescription, CSimpleStringA& strVTMCode) = 0;
 };
 
 class VTMInitParam{

+ 127 - 0
Framework/spbase/SpEntity.cpp

@@ -34,6 +34,9 @@
 #else
 #include "sp_dbg_export.h"
 #endif //_WIN32
+#include "sp_httpDefine.h"
+#include <iostream>
+#include <sstream>
 
 static void var_callback(sp_var_listener_t *listener, 
 						  const char *key, 
@@ -2041,6 +2044,130 @@ ErrorCodeEnum SpEntity::GetRunningVersion(CSimpleString& ver)
 	return (ErrorCodeEnum)ret;
 }
 
+struct ErrMsgStruct {
+	std::string VTMCode;
+	std::string errMsg;
+
+	// 带参数的构造函数,参数有默认值
+	ErrMsgStruct(const std::string& tmp_VTMCode = "", const std::string& tmp_errMsg = "") : VTMCode(tmp_VTMCode), errMsg(tmp_errMsg) {}
+};
+
+ErrorCodeEnum SpEntity::GetVTMErrMsg(DWORD dwUserCode, CSimpleStringA& strDescription, CSimpleStringA& strVTMCode)
+{
+	sp_env_t* env = sp_get_env();
+	if (env->cfg->root_ini->vtm_err_msg_config == NULL || strlen(env->cfg->root_ini->vtm_err_msg_config) == 0)
+	{
+		strVTMCode = "RTA42F0";
+		strDescription = "微服务异常";
+		DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setResultCode(strVTMCode)("GetVTMErrMsg vtm_err_msg_config is null, may have not sync from server or sync failed");
+		return ErrorCodeEnum::Error_NotConfig;
+	}
+
+	std::string userCodeStr = CSimpleString::Format("0x%X", dwUserCode).GetData();
+	std::transform(userCodeStr.begin(), userCodeStr.end(), userCodeStr.begin(), [](unsigned char c) { return std::tolower(c); });
+
+	static std::map<std::string, ErrMsgStruct> m_UserCodeToMsgTip;
+	static bool isInit = false;
+	if (!isInit)//Some entities may have no UserCode
+	{
+		CAutoArray<CSimpleStringA> strErrorCodeArr;
+		CAutoArray<CSimpleStringA> strDescriptionArr;
+		CAutoArray<CSimpleStringA> strRemarkArr;
+		ConvertStrToVTMErrMsg(env->cfg->root_ini->vtm_err_msg_config, strErrorCodeArr, strDescriptionArr, strRemarkArr);
+
+		auto intToHex = [](int num) -> std::string {
+			const char hexChars[] = "0123456789ABCDEF";
+			std::string hexStr;
+
+			// 将整数的每个字节转换为对应的16进制字符
+			for (int i = sizeof(num) - 1; i >= 0; --i) {
+				unsigned char byte = (num >> (8 * i)) & 0xFF;
+				if (byte == 0 && hexStr.empty()) {
+					continue; // 跳过前导零
+				}
+				hexStr += hexChars[byte >> 4];
+				hexStr += hexChars[byte & 0x0F];
+			}
+
+			return hexStr;
+		};
+
+		auto hexToInt = [](const std::string& hexStr)-> int {
+			std::stringstream ss;
+			ss << std::hex << hexStr;
+			int num;
+			ss >> num;
+			return num;
+		};
+
+		auto intTo36 = [](int num) -> std::string {
+			std::string base36Chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
+			std::string base36Str;
+
+			while (num > 0) {
+				base36Str = base36Chars[num % 36] + base36Str;
+				num /= 36;
+			}
+
+			return base36Str;
+		};
+
+		int id = GetDevID();
+		auto idHexStr = intToHex(id);
+		if (idHexStr.length() != 4 || idHexStr[0] != '0')
+		{
+			strVTMCode = "RTA42F2";
+			strDescription = "实体Id异常";
+			DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setResultCode(strVTMCode)("GetVTMErrMsg get error entity id %d, convert to %s", id, idHexStr.c_str());
+			return ErrorCodeEnum::Error_Unregisted;
+		}
+
+		std::string dstRTA = "RTA";
+		dstRTA.push_back(idHexStr[1]); //first 
+
+		std::string lastTwoDigits = idHexStr.substr(idHexStr.length() - 2); // last two digits
+
+		int num = hexToInt(lastTwoDigits);
+		if (num < 0 || num >= 36) {
+			strVTMCode = "RTA42F2";
+			strDescription = "无法转换实体Id";
+			DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setResultCode(strVTMCode)("GetVTMErrMsg cannot convert entity id %d ", id);
+			return ErrorCodeEnum::Error_DataCheck;
+		}
+
+		std::string base36Str = intTo36(num);
+		dstRTA.append(base36Str);
+
+		for (int i = 0; i < strErrorCodeArr.GetCount(); i++)
+		{
+			if (strErrorCodeArr[i].IsStartWith(dstRTA.c_str(), true))
+			{
+				std::string curRemark = strRemarkArr[i].GetData();
+				std::transform(curRemark.begin(), curRemark.end(), curRemark.begin(), [](unsigned char c) { return std::tolower(c); });
+				m_UserCodeToMsgTip.insert(std::make_pair(curRemark, ErrMsgStruct(strErrorCodeArr[i].GetData(), strDescriptionArr[i].GetData())));
+			}
+		}
+		isInit = true;
+	}
+
+	if (m_UserCodeToMsgTip.find(userCodeStr) != m_UserCodeToMsgTip.end())
+	{
+		strVTMCode = m_UserCodeToMsgTip[userCodeStr].VTMCode.c_str();
+		strDescription = m_UserCodeToMsgTip[userCodeStr].errMsg.c_str();
+		return ErrorCodeEnum::Error_Succeed;
+	}
+	else
+	{
+		strVTMCode = "RTA42F1";
+		strDescription = "错误映射异常";
+		DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setResultCode(strVTMCode)("GetVTMErrMsg entity did not contain the usercode %s ", userCodeStr.c_str());
+		return ErrorCodeEnum::Error_NotExist;
+	}
+
+
+
+}
+
 ErrorCodeEnum SpEntity::SetSelfPriority(EntityPriorityEnum nPriority)
 {
 	ErrorCodeEnum result(Error_Succeed);

+ 1 - 0
Framework/spbase/SpEntity.h

@@ -153,6 +153,7 @@ public:
 	virtual ErrorCodeEnum GetEntityLastError(const char* pszEntityName, CEntityLastErrorInfo& errInfo);
     virtual void ResetEntityLastError();
 	virtual ErrorCodeEnum GetRunningVersion(CSimpleString& ver);
+	virtual ErrorCodeEnum GetVTMErrMsg(DWORD dwUserCode, CSimpleStringA& strDescription, CSimpleStringA& strVTMCode);
     virtual ErrorCodeEnum SetSelfPriority(EntityPriorityEnum nPriority);
     virtual ErrorCodeEnum GetSelfPriority(EntityPriorityEnum& nPriority);
 

+ 14 - 0
Module/mod_chromium/mod_chromium.cpp

@@ -634,6 +634,14 @@ namespace Chromium {
 			auto ret = GetFunction()->GetPrivilegeFunction()->GetVTMErrMsgArr(strErrorCodeArr, strDescriptionArr, strRemarkArr);
 			if (Error_Succeed == ret)
 				InitUserCodeToMsgTip(strErrorCodeArr, strDescriptionArr, strRemarkArr);
+
+			if (m_withSpecialTest)
+			{
+				CSimpleStringA strDescription, strVTMCode;
+				GetFunction()->GetVTMErrMsg(123456, strDescription, strVTMCode);
+			}
+
+
 			startWithCfg();//属于chromium重启或者其他情况,已经初始化好配置
 		}
 		else
@@ -835,6 +843,12 @@ namespace Chromium {
 					InitUserCodeToMsgTip(strErrorCodeArr, strDescriptionArr, strRemarkArr);
 				else
 					LogWarn(Severity_Low, Error_Debug, LOG_WARN_CHROMIUM_VTMUSERMSG_ERR, CSimpleStringA::Format("GetVTMErrMsgArr err:%d", ret));
+
+				if (m_withSpecialTest)
+				{
+					CSimpleStringA strDescription, strVTMCode;
+					GetFunction()->GetVTMErrMsg(123456, strDescription, strVTMCode);
+				}
 			}
 			break;
 		default:

+ 27 - 0
Module/mod_vtmloader/VtmLoaderFSM.cpp

@@ -95,6 +95,13 @@ DWORD errMsgListUpdate(void* param)
 {
 	CVtmLoaderFSM* t_entity = (CVtmLoaderFSM*)param;
 
+	/*
+	CSimpleString code, des;
+
+	t_entity->GetEntityBase()->GetFunction()->GetVTMErrMsg(VtmLoader_DETECT_VERSION_ERR, code, des);
+	DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_USER)("VtmLoader_DETECT_VERSION_ERR, %s, %s", code.GetData(), des.GetData());
+	*/
+
 	//update err list after setting centersetting urls
 	//try updating every 20s until success
 	while (Error_Succeed != t_entity->GetEntityBase()->GetFunction()->GetPrivilegeFunction()->TryUpdateVTMERRMSG())
@@ -102,6 +109,26 @@ DWORD errMsgListUpdate(void* param)
 		Sleep(20000);
 
 	LogEvent(Severity_High, Event_VtmLoader_GetVTMERRMSG_Suc, "Get errMsgListUpdate config succeed.");
+	
+	/*
+	t_entity->GetEntityBase()->GetFunction()->GetVTMErrMsg(VtmLoader_DETECT_VERSION_ERR, code, des);
+	DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_USER)("VtmLoader_DETECT_VERSION_ERR, %s, %s", code.GetData(), des.GetData());
+
+	t_entity->GetEntityBase()->GetFunction()->GetVTMErrMsg(123456, code, des);
+	DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_USER)("123456, %s, %s", code.GetData(), des.GetData());
+
+	t_entity->GetEntityBase()->GetFunction()->GetVTMErrMsg(VtmLoader_FailToConnectEntity, code, des);
+	DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_USER)("VtmLoader_FailToConnectEntity, %s, %s", code.GetData(), des.GetData());
+
+	t_entity->GetEntityBase()->GetFunction()->GetVTMErrMsg(WARN_SendEndpoint_URL_ERR, code, des);
+	DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_USER)("WARN_SendEndpoint_URL_ERR, %s, %s", code.GetData(), des.GetData());
+
+	t_entity->GetEntityBase()->GetFunction()->GetVTMErrMsg(VtmLoader_GetConfig_UpdateCfg_Failed, code, des);
+	DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_USER)("VtmLoader_GetConfig_UpdateCfg_Failed, %s, %s", code.GetData(), des.GetData());
+
+	t_entity->GetEntityBase()->GetFunction()->GetVTMErrMsg(0x50100215, code, des);
+	DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_USER)("0x50100215, %s, %s", code.GetData(), des.GetData());
+	*/
 	return 0;
 }
 DWORD checkUrlActive(LPVOID param)