sp_logwithlink.cpp 29 KB

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