sp_logwithlink.cpp 28 KB

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