sp_logwithlink.cpp 29 KB

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