sp_logwithlink.cpp 28 KB

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