sp_logwithlink.cpp 30 KB

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