sp_logwithlink.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954
  1. #include "precompile.h"
  2. #include "SpBase.h"
  3. #include "SpUtility.h"
  4. #include "sp_checkEntity.h"
  5. #include "log_api.h"
  6. #include "log_producer_config.h"
  7. #include "log_producer_client.h"
  8. #include <cstdarg>
  9. #include <map>
  10. #include "sp_def.h"
  11. #include "sp_dir.h"
  12. #include "sp_logwithlinkforc.h"
  13. #include "sp_logwithlink.h"
  14. #include "sp_dbg_export.h"
  15. #include "fileutil.h"
  16. #include "iniutil.h"
  17. #include "charset.h"
  18. #include <vector>
  19. #include <algorithm>
  20. #include "sp_cfg.h"
  21. #include "sp_env.h"
  22. #include <locale>
  23. #include <winpr/file.h>
  24. #include <winpr/wlog.h>
  25. #include "sp_httpDefine.h"
  26. #define TAG SPBASE_TAG("sp_logwithlink")
  27. void* g_logProducer = NULL;//为spshell、spbase共用,所以不放入getEntityResource中
  28. //#define MAX_LOG_LEN 9700 //大致为10 * 1024 - 400(固定头)
  29. #define MAX_LOG_LEN 1900
  30. #define MAX_ENTITY_SAVE_LEN 100
  31. int g_curEntityIdx = 0;
  32. std::map<std::string, int> g_entityDebugLevelArr;
  33. void* g_logProduceSender = NULL;
  34. int curEntityLogLevel = 1;
  35. void Log_GetToken(char* channelId, char* token, char *terminalno, char *reserve1) {
  36. sp_env_t* env = sp_get_env();
  37. if (NULL != env && NULL != env->cfg && NULL != env->cfg->shell_ini && env->cfg->shell_ini->channelId != NULL && env->cfg->shell_ini->token != NULL
  38. && NULL != env->cfg->root_ini && NULL != env->cfg->root_ini->terminal_no)
  39. {
  40. snprintf(channelId, 256, "%s", env->cfg->shell_ini->channelId);
  41. snprintf(token, 256, "%s", env->cfg->shell_ini->token);
  42. snprintf(terminalno, 256, "%s", env->cfg->root_ini->terminal_no);
  43. }
  44. }
  45. /*天眼日志实例
  46. {
  47. "appName": "LR04",
  48. "version": "1.0",
  49. "meta_log": {
  50. "baseLogType": "INFO",
  51. "uuid": "ba814f0b-31e3-42ed-bf45-a36e9eb5c1f0",
  52. "needArchived": false
  53. },
  54. "log": {
  55. "type": "SYSTEM_INFO",
  56. "ts": "2023-04-03T12:57:49.000000000Z",
  57. "deployUnitId": "LR04.02@FrameworkLib_PRD_PRD",
  58. "serviceUnitId": "LR04.02_FrameworkLib",
  59. "content": {
  60. "ServName": "Terminal",
  61. "CmptId": "LR04",
  62. "CmptName": "FrameworkLib",
  63. "LogFile": "SI",
  64. "EvenTime": "2023-04-03 12:57:49.000",
  65. "ClientIP": "99.6.149.222",
  66. "Version": "1.0",
  67. "DateTime": "2023-04-03 12:57:49.000",
  68. "ResultCode": "SUC0000",
  69. "LogCode": "",
  70. "API": "",
  71. "TipMsg": "",
  72. "BussID": "",
  73. "SourceType": "",
  74. "ResultMsg": "entity UpgradeManager,cpu ratio:2.031250",
  75. "TraceID": "",
  76. "Item": "52",
  77. "EntityName": "mod_selfchecker",
  78. "LifeID": 331,
  79. "TimeSn": 178,
  80. "TerminalNo": "5710010002",
  81. "SN": "5710010002"
  82. }
  83. }
  84. }
  85. */
  86. //#define GBK_COMPACT
  87. class DbgWithLink::Link_private {
  88. public:
  89. LOG_LEVEL_E Level;
  90. LOG_TYPE_E Type;
  91. CSimpleStringA ResultCode;
  92. CSimpleStringA TraceID;
  93. CSimpleStringA ResultMsg;
  94. CSimpleStringA LogCode;
  95. CSimpleStringA API;
  96. long BeginTime;
  97. long EndTime;
  98. long CostTime;
  99. CSimpleStringA BussID;
  100. CSimpleStringA TipMsg;
  101. CSimpleStringA SourceType;
  102. CSimpleString VtmCode;
  103. void* logProducer;
  104. bool m_doLog;
  105. long m_time;
  106. Link_private(LOG_LEVEL_E t_level, LOG_TYPE_E t_type)
  107. : Level(t_level), Type(t_type), BeginTime(0), EndTime(0), CostTime(0), ResultCode("SUC0000"), m_doLog(true)
  108. {
  109. m_time = time(NULL);
  110. TraceID = ResultMsg = LogCode = API = BussID = TipMsg = SourceType = VtmCode = "";
  111. logProducer = NULL;
  112. }
  113. /** 这里先采用硬编码,控制日志等级 [Gifur@202268]*/
  114. #if defined(_MSC_VER)
  115. bool IsAllowToRecord()
  116. {
  117. return true;
  118. }
  119. #else
  120. bool IsAllowToRecord()
  121. {
  122. return (this->Level >= LOG_LEVEL_INFO);
  123. }
  124. #endif //_MSC_VER
  125. };
  126. /*北斗链路实例
  127. {
  128. "businessId": "ChromiumAutoGen",
  129. "traceId": "d2a04ca0a4834623a1323372b88e4e0a",
  130. "parentSpanId": "0",
  131. "spanId": "db5e25dffa78475c",
  132. "timestamp": "1680504454",
  133. "deployUnitId": "LR04.02_FrameworkLib",
  134. "serviceUnitId": "LR04.02@FrameworkLib_PRD_PRD",
  135. "host": "vtmtlog.paas.cmbchina.cn:8080",
  136. "api": "Exit",
  137. "returnCode": "SUC0000",
  138. "responseTime": "0",
  139. "callStack": [],
  140. "dbStack": [],
  141. "tags": {}
  142. }
  143. */
  144. class DbgToBeidou::Beidou_private {
  145. public:
  146. CSimpleStringA BussID;
  147. CSimpleStringA TraceID;//链路追踪标记,用于标记一笔业务
  148. CSimpleStringA ParentSpanId;//父节点标记
  149. CSimpleStringA SpanId;//当前节点标记
  150. CSimpleStringA Host;
  151. CSimpleStringA Api;
  152. CSimpleStringA ReturnCode;
  153. CSimpleStringA ResponseTime;
  154. CSimpleStringA CallStack;
  155. CSimpleStringA DbStack;
  156. CSimpleStringA Tags;
  157. CSimpleStringA deployUnitId;
  158. CSimpleStringA serviceUnitId;
  159. void* logProducer;
  160. Beidou_private(const linkContext& t_context, CSimpleStringA t_Api)
  161. :logProducer(NULL)
  162. {
  163. BussID = t_context.bussinessId;
  164. TraceID = t_context.traceId;
  165. ParentSpanId = t_context.parentSpanId;
  166. SpanId = t_context.spanId;
  167. Api = t_Api;
  168. ReturnCode = "SUC0000";
  169. ResponseTime = "0";
  170. Host = "vtmtlog.paas.cmbchina.cn:8080";
  171. CallStack = "{\"callStack\":[]}";
  172. DbStack = "{\"dbStack\":[]}";
  173. Tags = "{\"tags\":{}}";
  174. deployUnitId = "";
  175. serviceUnitId = "";
  176. }
  177. };
  178. linkContext EntityResource::m_link;
  179. int EntityResource::m_saveFile(0);
  180. void EntityResource::setLink(const linkContext& cur)
  181. {
  182. m_link = cur;
  183. }
  184. const linkContext& EntityResource::getLink()
  185. {
  186. return m_link;
  187. }
  188. void EntityResource::clearLink()
  189. {
  190. m_link.clear();
  191. }
  192. DbgToBeidou::DbgToBeidou(const linkContext& t_context, CSimpleStringA t_Api)
  193. :m_priPtr(new Beidou_private(t_context, t_Api))
  194. {
  195. strcpy(CharZero, "0");
  196. }
  197. DbgToBeidou::~DbgToBeidou() {
  198. if (m_priPtr)
  199. delete m_priPtr;
  200. }
  201. DbgToBeidou& DbgToBeidou::setHost(CSimpleStringA t_host) {
  202. m_priPtr->Host = t_host;
  203. return *this;
  204. }
  205. DbgToBeidou& DbgToBeidou::setReturnCode(CSimpleStringA t_ReturnCode) {
  206. m_priPtr->ReturnCode = t_ReturnCode;
  207. return *this;
  208. }
  209. DbgToBeidou& DbgToBeidou::setResponseTime(CSimpleStringA t_ResponseTime) {
  210. m_priPtr->ResponseTime = t_ResponseTime;
  211. return *this;
  212. }
  213. DbgToBeidou& DbgToBeidou::setCallStack(CSimpleStringA t_CallStack) {
  214. m_priPtr->CallStack = t_CallStack;
  215. return *this;
  216. }
  217. DbgToBeidou& DbgToBeidou::setDbStack(CSimpleStringA t_DbStack) {
  218. m_priPtr->DbStack = t_DbStack;
  219. return *this;
  220. }
  221. DbgToBeidou& DbgToBeidou::setTags(CSimpleStringA t_Tags) {
  222. m_priPtr->Tags = t_Tags;
  223. return *this;
  224. }
  225. DbgToBeidou& DbgToBeidou::set_deployUnitId(CSimpleString t_deployUnitId) {
  226. m_priPtr->deployUnitId = t_deployUnitId;
  227. return *this;
  228. }
  229. DbgToBeidou& DbgToBeidou::set_serviceUnitId(CSimpleString t_serviceUnitId) {
  230. m_priPtr->serviceUnitId = t_serviceUnitId;
  231. return *this;
  232. }
  233. void DbgToBeidou::operator () () const {
  234. beidou_log_item beidou_log = { 0 };
  235. beidou_log.BussID = (char*)m_priPtr->BussID.GetData();
  236. beidou_log.TraceID = (char*)m_priPtr->TraceID.GetData();
  237. beidou_log.ParentSpanId = (char*)m_priPtr->ParentSpanId.GetData();
  238. beidou_log.SpanId = (char*)m_priPtr->SpanId.GetData();
  239. beidou_log.Host = (char*)m_priPtr->Host.GetData();
  240. beidou_log.Api = (char*)m_priPtr->Api.GetData();
  241. beidou_log.ReturnCode = (char*)m_priPtr->ReturnCode.GetData();
  242. beidou_log.ResponseTime = m_priPtr->ResponseTime.GetLength() == 0 ? (char*)CharZero : (char*)m_priPtr->ResponseTime.GetData();
  243. beidou_log.CallStack = (char*)m_priPtr->CallStack.GetData();
  244. beidou_log.DbStack = (char*)m_priPtr->DbStack.GetData();
  245. beidou_log.Tags = (char*)m_priPtr->Tags.GetData();
  246. if (m_priPtr->deployUnitId.GetLength() > 0)
  247. beidou_log.deployUnitId = (char*)m_priPtr->deployUnitId.GetData();
  248. else
  249. beidou_log.deployUnitId = NULL;
  250. if (m_priPtr->serviceUnitId.GetLength() > 0)
  251. beidou_log.serviceUnitId = (char*)m_priPtr->serviceUnitId.GetData();
  252. else
  253. beidou_log.serviceUnitId = NULL;
  254. log_producer_client* cur = NULL;
  255. if (m_priPtr->logProducer)
  256. cur = (log_producer_client*)m_priPtr->logProducer;
  257. else {
  258. if (NULL != g_logProducer)
  259. cur = (log_producer_client*)g_logProducer;
  260. else
  261. return;
  262. }
  263. auto rst = log_producer_client_add_beidou_log(cur, &beidou_log, 1);
  264. /*
  265. if (!is_log_producer_result_ok(rst))
  266. sp_dbg_errorNoOut("beidou add log to db err: %d, %s", rst);
  267. */
  268. }
  269. DbgToBeidou& DbgToBeidou::withLogProducer(void* t_logProducer) {
  270. m_priPtr->logProducer = t_logProducer;
  271. return *this;
  272. }
  273. ////////////////////////////////////////////////////////////////////////////////////////////////
  274. DbgWithLink::DbgWithLink(LOG_LEVEL_E t_level, LOG_TYPE_E t_type) : m_priPtr(new Link_private(t_level, t_type))
  275. {
  276. auto curLink = EntityResource::getLink();
  277. if (curLink.traceId.GetLength() > 0 && curLink.bussinessId.GetLength() > 0)
  278. {
  279. m_priPtr->TraceID = curLink.traceId;
  280. m_priPtr->BussID = curLink.bussinessId;
  281. }
  282. }
  283. DbgWithLink::~DbgWithLink() {
  284. if (m_priPtr) delete m_priPtr;
  285. }
  286. DbgWithLink& DbgWithLink::setResultCode(CSimpleStringA t_ResultCode) {
  287. m_priPtr->ResultCode = t_ResultCode;
  288. return *this;
  289. }
  290. DbgWithLink& DbgWithLink::setTraceID(CSimpleStringA t_TraceID) {
  291. m_priPtr->TraceID = t_TraceID;
  292. return *this;
  293. }
  294. DbgWithLink& DbgWithLink::setResultMsg(CSimpleStringA t_ResultMsg) {
  295. if (t_ResultMsg.IsNullOrEmpty()) {
  296. m_priPtr->ResultMsg = "";
  297. } else {
  298. if (t_ResultMsg.GetLength() > MAX_LOG_LEN)
  299. t_ResultMsg = t_ResultMsg.SubString(0, MAX_LOG_LEN);
  300. #ifdef GBK_COMPACT
  301. const std::string tmp = SP::Utility::GBK2UTF8(t_ResultMsg.GetData());
  302. m_priPtr->ResultMsg = tmp.c_str();
  303. #else
  304. m_priPtr->ResultMsg = t_ResultMsg;
  305. #endif // GBK_COMPACT
  306. }
  307. return *this;
  308. }
  309. DbgWithLink& DbgWithLink::setLogCode(CSimpleStringA t_LogCode) {
  310. m_priPtr->LogCode = t_LogCode;
  311. return *this;
  312. }
  313. DbgWithLink& DbgWithLink::setAPI(CSimpleStringA t_API) {
  314. #if defined(RVC_OS_WIN)
  315. if (!t_API.IsNullOrEmpty() && t_API.IndexOf("::") != -1
  316. && !t_API.IsStartWith("DevAdapter::") /** treat specially [Gifur@2024730]*/
  317. ) {
  318. int tIdx = 0;
  319. const int nLen = t_API.GetLength();
  320. for (int i = nLen - 1; i > 0; --i) {
  321. if (t_API[i] == ':' && t_API[i - 1] == ':') {
  322. tIdx = i + 1;
  323. break;
  324. }
  325. }
  326. if (tIdx > 0 && tIdx < nLen) {
  327. t_API = t_API.SubString(tIdx);
  328. }
  329. else if (tIdx == nLen) {
  330. t_API.Clear();
  331. t_API = CSimpleStringA(true);
  332. }
  333. }
  334. #endif //RVC_OS_WIN
  335. m_priPtr->API = t_API;
  336. return *this;
  337. }
  338. DbgWithLink& DbgWithLink::setBussID(CSimpleStringA t_BussID) {
  339. m_priPtr->BussID = t_BussID;
  340. return *this;
  341. }
  342. DbgWithLink& DbgWithLink::setTipMsg(CSimpleStringA t_TipMsg) {
  343. m_priPtr->TipMsg = t_TipMsg;
  344. return *this;
  345. }
  346. DbgWithLink& DbgWithLink::setSourceType(CSimpleStringA t_SourceType) {
  347. m_priPtr->SourceType = t_SourceType;
  348. return *this;
  349. }
  350. DbgWithLink& DbgWithLink::setBeginTime(long t_BeginTime) {
  351. m_priPtr->BeginTime = t_BeginTime;
  352. return *this;
  353. }
  354. DbgWithLink& DbgWithLink::setEndTime(long t_EndTime) {
  355. m_priPtr->EndTime = t_EndTime;
  356. return *this;
  357. }
  358. DbgWithLink& DbgWithLink::setCostTime(long t_costTime) {
  359. m_priPtr->CostTime = t_costTime;
  360. return *this;
  361. }
  362. DbgWithLink& DbgWithLink::setVtmCode(CSimpleStringA t_VtmCode) {
  363. m_priPtr->VtmCode = t_VtmCode;
  364. return *this;
  365. }
  366. void DbgWithLink::operator () (const char* str, ...) const {
  367. if (str == nullptr)
  368. return;
  369. va_list arg;
  370. va_start(arg, str);
  371. int n = _vscprintf(str, arg) + 1;
  372. if (n > MAX_LOG_LEN) {
  373. n = MAX_LOG_LEN;
  374. }
  375. std::vector<char> buf(n + 1, '\0');
  376. vsnprintf(&buf[0], n, str, arg);
  377. va_end(arg);
  378. #ifdef GBK_COMPACT
  379. if (!toolkit_detect_utf8_str((const char*)&buf[0])) {
  380. char* tmp = &buf[0];
  381. const size_t len = toolkit_gbk2utf8(tmp, strlen(tmp) + 1, NULL, 0);
  382. if (len > 0) {
  383. char* temp = new char[len];
  384. if (temp != NULL) {
  385. memset(temp, 0, len);
  386. if (toolkit_gbk2utf8(tmp, strlen(tmp) + 1, temp, len) > 0) {
  387. strcpy(tmp, temp);
  388. }
  389. delete[] temp;
  390. }
  391. }
  392. }
  393. #endif //GBK_COMPACT
  394. if (m_priPtr->IsAllowToRecord()) {
  395. log_item log;
  396. log_producer_result rst;
  397. log.Level = m_priPtr->Level;
  398. log.Type = m_priPtr->Type;
  399. log.LogCode = (char*)m_priPtr->LogCode.GetData();
  400. log.API = (char*)m_priPtr->API.GetData();
  401. log.ResultCode = (char*)m_priPtr->ResultCode.GetData();
  402. log.ResultMsg = &buf[0];
  403. log.TipMsg = (char*)m_priPtr->TipMsg.GetData();
  404. log.BeginTime = m_priPtr->BeginTime;
  405. log.EndTime = m_priPtr->EndTime;
  406. log.CostTime = m_priPtr->CostTime;
  407. log.VtmCode = (char*)m_priPtr->VtmCode.GetData();
  408. log.TraceID = (char*)m_priPtr->TraceID.GetData();
  409. log.BussID = (char*)m_priPtr->BussID.GetData();
  410. log.SourceType = (char*)m_priPtr->SourceType.GetData();
  411. log_producer_client* cur = NULL;
  412. if (m_priPtr->logProducer) {
  413. cur = (log_producer_client*)m_priPtr->logProducer;
  414. } else {
  415. if (NULL != g_logProducer) {
  416. cur = (log_producer_client*)g_logProducer;
  417. }
  418. }
  419. if (cur != NULL) {
  420. if (log.Type == LOG_TYPE_SYSTEM && curEntityLogLevel <= log.Level)
  421. rst = log_producer_client_add_log(cur, &log, 1, m_priPtr->m_time);
  422. else if (log.Type != LOG_TYPE_SYSTEM)
  423. rst = log_producer_client_add_log(cur, &log, 1, m_priPtr->m_time);
  424. }
  425. }
  426. //LOG_LEVEL_DEBUG, LOG_LEVEL_INFO, LOG_LEVEL_WARN, LOG_LEVEL_ERROR, LOG_LEVEL_FATAL
  427. if (!m_priPtr->m_doLog) {
  428. return;
  429. }
  430. switch (m_priPtr->Level) {
  431. case LOG_LEVEL_DEBUG:
  432. sp_dbg_debugNoOut("%s", &buf[0]);
  433. break;
  434. case LOG_LEVEL_INFO:
  435. sp_dbg_infoNoOut("%s", &buf[0]);
  436. break;
  437. case LOG_LEVEL_WARN:
  438. sp_dbg_warnNoOut("%s", &buf[0]);
  439. break;
  440. case LOG_LEVEL_ERROR:
  441. sp_dbg_errorNoOut("%s", &buf[0]);
  442. break;
  443. case LOG_LEVEL_FATAL:
  444. sp_dbg_fatalNoOut("%s", &buf[0]);
  445. break;
  446. default:
  447. break;
  448. }
  449. }
  450. void DbgWithLink::operator () () const {
  451. log_item log;
  452. log.Level = m_priPtr->Level;
  453. log.Type = m_priPtr->Type;
  454. log.LogCode = (char*)m_priPtr->LogCode.GetData();
  455. log.API = (char*)m_priPtr->API.GetData();
  456. log.ResultCode = (char*)m_priPtr->ResultCode.GetData();
  457. if (m_priPtr->ResultMsg.GetLength() > MAX_LOG_LEN)
  458. m_priPtr->ResultMsg = m_priPtr->ResultMsg.SubString(0, MAX_LOG_LEN);
  459. log.ResultMsg = (char*)m_priPtr->ResultMsg.GetData();
  460. log.TipMsg = (char*)m_priPtr->TipMsg.GetData();
  461. log.BeginTime = m_priPtr->BeginTime;
  462. log.EndTime = m_priPtr->EndTime;
  463. log.CostTime = m_priPtr->CostTime;
  464. log.VtmCode = (char*)m_priPtr->VtmCode.GetData();
  465. log.TraceID = (char*)m_priPtr->TraceID.GetData();
  466. log.BussID = (char*)m_priPtr->BussID.GetData();
  467. log.SourceType = (char*)m_priPtr->SourceType.GetData();
  468. log_producer_client* cur = NULL;
  469. if (m_priPtr->logProducer) {
  470. cur = (log_producer_client*)m_priPtr->logProducer;
  471. } else {
  472. if (NULL != g_logProducer) {
  473. cur = (log_producer_client*)g_logProducer;
  474. }
  475. }
  476. if (cur != NULL) {
  477. if (log.Type == LOG_TYPE_SYSTEM && curEntityLogLevel <= log.Level)
  478. log_producer_client_add_log(cur, &log, 1, m_priPtr->m_time);
  479. else if (log.Type != LOG_TYPE_SYSTEM)
  480. log_producer_client_add_log(cur, &log, 1, m_priPtr->m_time);
  481. }
  482. if (!m_priPtr->m_doLog)
  483. return;
  484. switch (log.Level) {
  485. case LOG_LEVEL_DEBUG:
  486. sp_dbg_debugNoOut("%s", log.ResultMsg);
  487. break;
  488. case LOG_LEVEL_INFO:
  489. sp_dbg_infoNoOut("%s", log.ResultMsg);
  490. break;
  491. case LOG_LEVEL_WARN:
  492. sp_dbg_warnNoOut("%s", log.ResultMsg);
  493. break;
  494. case LOG_LEVEL_ERROR:
  495. sp_dbg_errorNoOut("%s", log.ResultMsg);
  496. break;
  497. case LOG_LEVEL_FATAL:
  498. sp_dbg_fatalNoOut("%s", log.ResultMsg);
  499. break;
  500. default:
  501. break;
  502. }
  503. }
  504. DbgWithLink& DbgWithLink::withLogProducer(void* t_logProducer)
  505. {
  506. m_priPtr->logProducer = t_logProducer;
  507. return *this;
  508. }
  509. DbgWithLink& DbgWithLink::withExtendLog(bool t_doLog) {
  510. m_priPtr->m_doLog = t_doLog;
  511. return *this;
  512. }
  513. CSimpleString generateDefaultStoragePath()
  514. {
  515. char dirPath[_MAX_PATH] = "";
  516. char tmp[MAX_PATH];
  517. GetModuleFileNameA(NULL, tmp, MAX_PATH);
  518. *strrchr(tmp, SPLIT_SLASH) = 0;
  519. *strrchr(tmp, SPLIT_SLASH) = 0;
  520. *strrchr(tmp, SPLIT_SLASH) = 0;
  521. *strrchr(tmp, SPLIT_SLASH) = 0;
  522. *strrchr(tmp, SPLIT_SLASH) = 0;
  523. sprintf(dirPath, "%s" SPLIT_SLASH_STR "rvc" SPLIT_SLASH_STR "terminaldbstorage", tmp);
  524. if (ExistsDirA(dirPath) || CreateDirectoryA(dirPath, NULL)) {
  525. return dirPath;
  526. }
  527. return "";
  528. }
  529. static CSimpleStringA g_entityName = "SpShell";
  530. void logCallback(const char* message) {
  531. // 在这里执行您的日志记录逻辑
  532. // 在这个示例中,我们简单地将日志消息输出到控制台
  533. WLog_DBG(TAG, "from rvcLogSDK:%s", message);
  534. }
  535. std::string toLowerCase(std::string str) {
  536. std::transform(str.begin(), str.end(), str.begin(), [](unsigned char c) {
  537. return std::tolower(c, std::locale());
  538. });
  539. return str;
  540. }
  541. SPBASE_API void* create_log_producer_storage(CSimpleStringA entityName, CSimpleStringA item, CSimpleStringA filePath, CSimpleStringA CmptId, CSimpleString CmptName)
  542. {
  543. WLog_DBG(TAG, "create log producer storage for %s, %s, %s", entityName.GetData(), item.GetData(), filePath.GetData());
  544. log_producer_config* config = create_log_producer_config();
  545. #ifdef _WIN32
  546. std::string t_entityName = toLowerCase(entityName.GetData());
  547. #else
  548. const std::string t_entityName = SP::Utility::ToLower(entityName.GetData());
  549. #endif
  550. if (config == NULL) {
  551. WLog_DBG(TAG, "create_log_producer_config return null!");
  552. return NULL;
  553. }
  554. // set resource params
  555. log_producer_config_set_packet_log_bytes(config, 4 * 1024 * 1024);
  556. log_producer_config_set_packet_log_count(config, 24);//最大发送条数
  557. log_producer_config_set_max_buffer_limit(config, 64 * 1024 * 1024);
  558. // set send thread count
  559. log_producer_config_set_send_thread_count(config, 1);
  560. log_producer_config_set_persistent_max_log_count(config, 50 * 1000);
  561. log_producer_config_set_skyeye_servname(config, (char*)"Terminal");
  562. if(CmptId.GetLength() > 0)
  563. log_producer_config_set_skyeye_cmptId(config, (char*)CmptId.GetData());
  564. else
  565. log_producer_config_set_skyeye_cmptId(config, (char*)"LR04");
  566. if(CmptName.GetLength() > 0)
  567. log_producer_config_set_skyeye_cmptname(config, (char*)CmptName.GetData());
  568. else
  569. log_producer_config_set_skyeye_cmptname(config, (char*)"FrameworkLib");
  570. log_producer_config_set_skyeye_version(config, (char*)"1.0");
  571. log_producer_config_set_persistent(config, 1);
  572. #ifndef _WIN32
  573. sp_env_t* env = sp_get_env();
  574. if (env && env->cfg && env->cfg->args && env->cfg->args->debug_mode) {
  575. //log_producer_config_switch_debug_mode(config, 1);
  576. log_producer_config_set_persistent_encrypt(config, 0);
  577. }
  578. #endif
  579. log_producer_config_set_skyeye_entityname(config, (char*)t_entityName.c_str());
  580. g_entityName = t_entityName.c_str();
  581. #ifndef _WIN32
  582. WLog_DBG(TAG, "entity name: %s", t_entityName.c_str());
  583. if (t_entityName.compare("spshell") == 0) {
  584. log_producer_config_memory_deal_type(config, 1);
  585. }
  586. #endif
  587. log_producer_config_set_skyeye_item(config, (char*)item.GetData());
  588. if (filePath.GetLength() == 0) {
  589. auto dstPath = generateDefaultStoragePath();
  590. if (dstPath.GetLength() == 0) {
  591. return NULL;//create db file failed
  592. } else {
  593. log_producer_config_set_persistent_file_path(config, dstPath.GetData());
  594. }
  595. } else {
  596. if (!ExistsDirA(filePath.GetData())) {
  597. CreateDirRecursiveA(filePath.GetData());
  598. }
  599. log_producer_config_set_persistent_file_path(config, filePath.GetData());
  600. }
  601. //新加统一日志配置
  602. log_producer_config_set_united_appname(config, (char*)"LR04");
  603. log_producer_config_set_united_version(config, (char*)"2.0");
  604. log_producer_config_set_united_needArchived(config, 0);
  605. log_producer_config_set_united_deployUnitId(config, (char*)"LR04.02_FrameworkLib");
  606. log_producer_config_set_united_serviceUnitId(config, (char*)"LR04.02@FrameworkLib_PRD_PRD");
  607. #ifndef _WIN32
  608. //log_producer_config_set_dbgFun(logCallback);
  609. WLog_DBG(TAG, "to create_log_producer_client...");
  610. log_producer_client* result = create_log_producer_client(config, NULL, NULL);
  611. if (result == NULL) {
  612. WLog_ERR(TAG, "create_log_producer_client return null!");
  613. } else {
  614. WLog_DBG(TAG, "to create_log_producer_client done");
  615. }
  616. return result;
  617. #else
  618. return create_log_producer_client(config, NULL, NULL);
  619. #endif
  620. }
  621. SPBASE_API void destroy_log_producer_storage(void* obj)
  622. {
  623. if (obj != NULL) {
  624. log_producer_client* result = (log_producer_client*)obj;
  625. destroy_log_producer_client(result);
  626. }
  627. }
  628. SPBASE_API void* create_log_producer_send(CSimpleStringA endpoint, CSimpleStringA topicSys, CSimpleStringA topicUser, CSimpleStringA topicBeidou, CSimpleStringA bussinessSys, CSimpleStringA bussinessUser, CSimpleStringA topicVTMWeb,
  629. CSimpleStringA terminalNo, CSimpleStringA sn, CSimpleStringA filePath, int sendWaitTime)
  630. {
  631. #ifndef _WIN32
  632. if (NULL != g_logProduceSender) {
  633. return g_logProduceSender;
  634. }
  635. #endif
  636. log_producer_config* config = create_log_producer_config();
  637. log_producer_config_set_endpoint(config, endpoint.GetData());
  638. log_producer_config_set_send_thread_wait_ms(config, sendWaitTime);
  639. // add topic, comment it
  640. log_producer_config_set_beidou_topic(config, topicBeidou.GetData());
  641. log_producer_config_set_skyeye_user_topic(config, topicUser.GetData());
  642. log_producer_config_set_skyeye_sys_topic(config, topicSys.GetData());
  643. log_producer_config_set_business_sys_topic(config, bussinessSys.GetData());
  644. log_producer_config_set_business_user_topic(config, bussinessUser.GetData());
  645. log_producer_config_set_vtmweb_topic(config, topicVTMWeb.GetData());
  646. // set resource params
  647. log_producer_config_set_packet_log_bytes(config, 4 * 1024 * 1024);
  648. log_producer_config_set_packet_log_count(config, 24);
  649. log_producer_config_set_max_buffer_limit(config, 64 * 1024 * 1024);
  650. // set send thread count
  651. log_producer_config_set_send_thread_count(config, 1);
  652. log_producer_config_set_persistent_max_log_count(config, 50 * 1000);
  653. log_producer_config_set_skyeye_servname(config, (char*)"Terminal");
  654. log_producer_config_set_skyeye_cmptId(config, (char*)"LR04");
  655. log_producer_config_set_skyeye_cmptname(config, (char*)"FrameworkLib");
  656. log_producer_config_set_skyeye_version(config, (char*)"1.0");
  657. log_producer_config_set_persistent(config, 1);
  658. log_producer_config_set_openDb(config, 1);
  659. log_producer_config_set_tokenCallBack(config, Log_GetToken);
  660. #ifndef _WIN32
  661. sp_env_t* env = sp_get_env();
  662. if (env && env->cfg && env->cfg->args && env->cfg->args->debug_mode) {
  663. //log_producer_config_switch_debug_mode(config, 1);
  664. log_producer_config_set_persistent_encrypt(config, 0);
  665. }
  666. #endif
  667. log_producer_config_set_skyeye_entityname(config, (char*)g_entityName.GetData());
  668. log_producer_config_set_skyeye_item(config, (char*)"upload");
  669. log_producer_config_set_skyeye_terminalNo(config, (char*)terminalNo.GetData());
  670. log_producer_config_set_skyeye_sn(config, (char*)sn.GetData());
  671. if (filePath.GetLength() == 0) {
  672. auto dstPath = generateDefaultStoragePath();
  673. if (dstPath.GetLength() == 0) {
  674. return NULL;//create db file failed
  675. } else {
  676. log_producer_config_set_persistent_file_path(config, dstPath.GetData());
  677. }
  678. } else {
  679. if (!ExistsDirA(filePath.GetData())) {
  680. CreateDirRecursiveA(filePath.GetData());
  681. }
  682. log_producer_config_set_persistent_file_path(config, filePath.GetData());
  683. }
  684. //新加统一日志配置
  685. log_producer_config_set_united_appname(config, (char*)"LR04");
  686. log_producer_config_set_united_version(config, (char*)"2.0");
  687. log_producer_config_set_united_needArchived(config, 0);
  688. log_producer_config_set_united_deployUnitId(config, (char*)"LR04.02_FrameworkLib");
  689. log_producer_config_set_united_serviceUnitId(config, (char*)"LR04.02@FrameworkLib_PRD_PRD");
  690. log_producer_config_set_enable_guard(config, 1);
  691. #ifndef _WIN32
  692. g_logProduceSender = create_log_producer_client(config, NULL, NULL);
  693. return g_logProduceSender;
  694. #else
  695. return create_log_producer_client(config, NULL, NULL);
  696. #endif
  697. }
  698. SPBASE_API void destroy_log_producer_send()
  699. {
  700. if (g_logProduceSender != NULL) {
  701. log_producer_client* result = (log_producer_client*)g_logProduceSender;
  702. destroy_log_producer_client(result);
  703. g_logProduceSender = NULL;
  704. }
  705. }
  706. SPBASE_API bool refreshLogLevel()
  707. {
  708. sp_env_t* env = sp_get_env();
  709. if (env) {
  710. sp_cfg_t* cfg = env->cfg;
  711. sp_cfg_shell_entity_t* ent = sp_cfg_get_entity_by_idx(cfg, g_curEntityIdx);
  712. if (ent) {
  713. curEntityLogLevel = ent->log_record_level;
  714. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("refreshLogLevel change to EntityLevel:%d", curEntityLogLevel);
  715. }
  716. }
  717. else
  718. {
  719. if (g_entityDebugLevelArr.find("Common") != g_entityDebugLevelArr.end())
  720. {
  721. curEntityLogLevel = g_entityDebugLevelArr["Common"];
  722. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("refreshLogLevel change to Common:%d", curEntityLogLevel);
  723. }
  724. else
  725. {
  726. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("refreshLogLevel change to Default:%d", curEntityLogLevel);
  727. curEntityLogLevel = 1;
  728. }
  729. }
  730. return true;
  731. }
  732. SPBASE_API int getCurLogLevel()
  733. {
  734. return curEntityLogLevel;
  735. }
  736. SPBASE_API bool create_log_producer_default(CSimpleStringA entityName, int idx)
  737. {
  738. refreshLogLevel();
  739. CSimpleString idxStr = CSimpleString::Format("%d", idx);
  740. #ifndef _WIN32
  741. WLog_DBG(TAG, "create defaut log_producer(create log producer storage) for %s %d", entityName.GetData(), idxStr.GetData());
  742. g_logProducer = create_log_producer_storage(entityName, idxStr, "", "", "");
  743. if (g_logProducer == NULL)
  744. WLog_ERR(TAG, "create log producer storage return null!");
  745. return (NULL != g_logProducer);
  746. #else
  747. return NULL != (g_logProducer = create_log_producer_storage(entityName, idxStr, "", "", ""));
  748. #endif
  749. }
  750. SPBASE_API void destroy_log_producer_default()
  751. {
  752. if (g_logProducer != NULL) {
  753. log_producer_client* result = (log_producer_client*)g_logProducer;
  754. destroy_log_producer_client(result);
  755. g_logProducer = NULL;
  756. }
  757. }
  758. SPBASE_API void DbgWithLinkForC(LOG_LEVEL_E t_level, LOG_TYPE_E t_type, const char* str, ...)
  759. {
  760. if (str == nullptr)
  761. return;
  762. va_list arg;
  763. va_start(arg, str);
  764. int n = _vscprintf(str, arg) + 1;
  765. std::vector<char> buf(n + 1, '\0');
  766. vsnprintf(&buf[0], n, str, arg);
  767. DbgWithLink(t_level, t_type).setResultMsg(&buf[0])();
  768. va_end(arg);
  769. }
  770. extern "C" SPBASE_API void __stdcall setCurEntityIdx(int idx)
  771. {
  772. g_curEntityIdx = idx;
  773. }
  774. void load_debugLevelInCentersetting(const char* fileName)
  775. {
  776. array_header_t* arr_section = inifile_read_section_all(fileName);
  777. g_entityDebugLevelArr.clear();
  778. g_entityDebugLevelArr["Common"] = 1;
  779. if (!arr_section)
  780. return;
  781. for (int i = 0; i < arr_section->nelts; ++i) {
  782. char* sec = ARRAY_IDX(arr_section, i, char*);
  783. array_header_t* arr_key = inifile_read_section_key_all(fileName, sec);
  784. if (!arr_key) continue;
  785. for (int j = 0; j < arr_key->nelts; ++j)
  786. {
  787. char* key = ARRAY_IDX(arr_key, j, char*);
  788. if (_stricmp(key, "UpLoadLogLevel") == 0)
  789. {
  790. std::string secName = sec;
  791. std::transform(secName.begin(), secName.end(), secName.begin(), ::tolower);
  792. g_entityDebugLevelArr[secName] = inifile_read_int(fileName, sec, "UpLoadLogLevel", 2);
  793. }
  794. }
  795. }
  796. }
  797. void load_debugLevelInMem(std::map<std::string, std::map<std::string, std::string>> & tmp_centerConfig)
  798. {
  799. g_entityDebugLevelArr.clear();
  800. const std::string compareStr = "UpLoadLogLevel";
  801. for (auto it = tmp_centerConfig.begin(); it != tmp_centerConfig.end(); it++)
  802. {
  803. auto sectionInfo = it->second;
  804. for (auto obj = sectionInfo.begin(); obj != sectionInfo.end(); obj++)
  805. {
  806. if (obj->first == compareStr)
  807. g_entityDebugLevelArr[it->first] = atoi(obj->second.c_str());
  808. }
  809. }
  810. if (g_entityDebugLevelArr.find("Common") == g_entityDebugLevelArr.end())
  811. g_entityDebugLevelArr["Common"] = 1;
  812. }
  813. int getEntityLogLevel(std::string entityName)
  814. {
  815. if (g_entityDebugLevelArr.size() == 0)
  816. return 1;
  817. if (g_entityDebugLevelArr.find(entityName) != g_entityDebugLevelArr.end())
  818. return g_entityDebugLevelArr[entityName];
  819. else
  820. return g_entityDebugLevelArr["Common"];
  821. }
  822. ErrorCodeEnum GetAllEntityIdx(CAutoArray<CSimpleStringA>& strEntityNames, CAutoArray<WORD>& wEntityDevelopIDs)
  823. {
  824. sp_env_t* env = sp_get_env();
  825. sp_cfg_t* cfg = env->cfg;
  826. array_header_t* arr_entity = cfg->shell_ini->arr_entity;
  827. strEntityNames.Init(arr_entity->nelts - 1);
  828. wEntityDevelopIDs.Init(arr_entity->nelts - 1);
  829. for (int i = 1; i < arr_entity->nelts; ++i) { // from index 1, because zero for special entity, ie. spshell entity
  830. sp_cfg_shell_entity_t* ent = ARRAY_IDX(arr_entity, i, sp_cfg_shell_entity_t*);
  831. strEntityNames[i - 1] = ent->name;
  832. wEntityDevelopIDs[i - 1] = ent->idx;
  833. }
  834. return Error_Succeed;
  835. }