log_producer_config.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. #include "log_producer_config.h"
  2. #include "sds.h"
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include "inner_log.h"
  6. dbg_callback g_callback;
  7. static void _copy_config_string(const char* value, sds* src_value)
  8. {
  9. int strLen;
  10. if (value == NULL || src_value == NULL)
  11. {
  12. return;
  13. }
  14. strLen = strlen(value);
  15. if (*src_value == NULL)
  16. {
  17. *src_value = sdsnewEmpty(strLen);
  18. }
  19. else {
  20. sdsfree(*src_value);
  21. }
  22. *src_value = sdscpylen(*src_value, value, strLen);
  23. }
  24. static void _set_default_producer_config(log_producer_config* pConfig)
  25. {
  26. pConfig->enableGuarder = 0;
  27. pConfig->tokenFun = NULL;
  28. pConfig->sendThreadCount = 2;
  29. pConfig->sendThreadWaitMs = 100;
  30. pConfig->logBytesPerPackage = MAX_LOG_COUNT * 1024;
  31. pConfig->logCountPerPackage = 5;
  32. pConfig->maxBufferBytes = 64 * 1024 * 1024;
  33. pConfig->connectTimeoutSec = 10;
  34. pConfig->sendTimeoutSec = 15;
  35. pConfig->destroySenderWaitTimeoutSec = 1;
  36. pConfig->destroyFlusherWaitTimeoutSec = 1;
  37. pConfig->compressType = 0;
  38. pConfig->ntpTimeOffset = 0;
  39. pConfig->using_https = 0;
  40. pConfig->usePersistent = 1;
  41. pConfig->openDB = 0;
  42. pConfig->maxPersistentLogCount = 5*10000;
  43. pConfig->usePersistentEncrypt = 2;
  44. pConfig->useMemoryType = 0;
  45. pConfig->debugMode = 0;
  46. _copy_config_string("RVC_LOG_SDK_25846", &pConfig->PersistentEncryptPassword);
  47. }
  48. log_producer_config* create_log_producer_config()
  49. {
  50. log_producer_config* pConfig = (log_producer_config*)malloc(sizeof(log_producer_config));
  51. memset(pConfig, 0, sizeof(log_producer_config));
  52. _set_default_producer_config(pConfig);
  53. return pConfig;
  54. }
  55. void destroy_log_producer_config(log_producer_config* pConfig)
  56. {
  57. if (pConfig->endpoint != NULL)
  58. {
  59. sdsfree(pConfig->endpoint);
  60. }
  61. if (pConfig->skyeye_user_topic != NULL)
  62. {
  63. sdsfree(pConfig->skyeye_user_topic);
  64. }
  65. if (pConfig->skyeye_sys_topic != NULL)
  66. {
  67. sdsfree(pConfig->skyeye_sys_topic);
  68. }
  69. if (pConfig->beidou_topic != NULL)
  70. {
  71. sdsfree(pConfig->beidou_topic);
  72. }
  73. if (pConfig->business_user_topic != NULL)
  74. {
  75. sdsfree(pConfig->business_user_topic);
  76. }
  77. if (pConfig->business_sys_topic != NULL)
  78. {
  79. sdsfree(pConfig->business_sys_topic);
  80. }
  81. if (pConfig->vtmweb_topic != NULL)
  82. {
  83. sdsfree(pConfig->vtmweb_topic);
  84. }
  85. if (pConfig->persistentFilePath != NULL)
  86. {
  87. sdsfree(pConfig->persistentFilePath);
  88. }
  89. if (pConfig->PersistentEncryptPassword != NULL)
  90. {
  91. sdsfree(pConfig->PersistentEncryptPassword);
  92. }
  93. if (pConfig->skyeyeServName != NULL)
  94. {
  95. sdsfree(pConfig->skyeyeServName);
  96. }
  97. if (pConfig->skyeyeCmptId != NULL)
  98. {
  99. sdsfree(pConfig->skyeyeCmptId);
  100. }
  101. if (pConfig->skyeyeCmptName != NULL)
  102. {
  103. sdsfree(pConfig->skyeyeCmptName);
  104. }
  105. if (pConfig->skyeyeTerminalNo != NULL)
  106. {
  107. sdsfree(pConfig->skyeyeTerminalNo);
  108. }
  109. if (pConfig->skyeyeVersion != NULL)
  110. {
  111. sdsfree(pConfig->skyeyeVersion);
  112. }
  113. if (pConfig->skyeyeSn != NULL)
  114. {
  115. sdsfree(pConfig->skyeyeSn);
  116. }
  117. if (pConfig->skyeyeItem != NULL)
  118. {
  119. sdsfree(pConfig->skyeyeItem);
  120. }
  121. if (pConfig->skyeyeItem != NULL)
  122. {
  123. sdsfree(pConfig->skyeyeItem);
  124. }
  125. if (pConfig->skyeyeEntityName != NULL)
  126. {
  127. sdsfree(pConfig->skyeyeEntityName);
  128. }
  129. if (pConfig->unitedAppName != NULL)
  130. {
  131. sdsfree(pConfig->unitedAppName);
  132. }
  133. if (pConfig->unitedVersion != NULL)
  134. {
  135. sdsfree(pConfig->unitedVersion);
  136. }
  137. if (pConfig->unitedDeployUnitId != NULL)
  138. {
  139. sdsfree(pConfig->unitedDeployUnitId);
  140. }
  141. if (pConfig->unitedServiceUnitId != NULL)
  142. {
  143. sdsfree(pConfig->unitedServiceUnitId);
  144. }
  145. free(pConfig);
  146. }
  147. #ifdef LOG_PRODUCER_DEBUG
  148. void log_producer_config_print(log_producer_config* pConfig, FILE* file)
  149. {
  150. fprintf(file, "endpoint : %s\n", pConfig->endpoint);
  151. fprintf(file, "project : %s\n", pConfig->project);
  152. fprintf(file, "logstore : %s\n", pConfig->logstore);
  153. fprintf(file, "accessKeyId : %s\n", pConfig->accessKeyId);
  154. fprintf(file, "accessKey : %s\n", pConfig->accessKey);
  155. fprintf(file, "configName : %s\n", pConfig->configName);
  156. fprintf(file, "topic : %s\n", pConfig->topic);
  157. fprintf(file, "logLevel : %d\n", pConfig->logLevel);
  158. fprintf(file, "packageTimeoutInMS : %d\n", pConfig->packageTimeoutInMS);
  159. fprintf(file, "logCountPerPackage : %d\n", pConfig->logCountPerPackage);
  160. fprintf(file, "logBytesPerPackage : %d\n", pConfig->logBytesPerPackage);
  161. fprintf(file, "maxBufferBytes : %d\n", pConfig->maxBufferBytes);
  162. fprintf(file, "tags: \n");
  163. int32_t i = 0;
  164. for (i = 0; i < pConfig->tagCount; ++i)
  165. {
  166. fprintf(file, "tag key : %s, value : %s \n", pConfig->tags[i].key, pConfig->tags[i].value);
  167. }
  168. }
  169. #endif
  170. void log_producer_config_set_packet_log_count(log_producer_config* config, int32_t log_count)
  171. {
  172. if (config == NULL || log_count < 0)
  173. {
  174. return;
  175. }
  176. if (log_count > MAX_LOG_COUNT) {
  177. log_count = MAX_LOG_COUNT;
  178. }
  179. config->logCountPerPackage = log_count;
  180. }
  181. void log_producer_config_set_packet_log_bytes(log_producer_config* config, int32_t log_bytes)
  182. {
  183. if (config == NULL || log_bytes < 0)
  184. {
  185. return;
  186. }
  187. config->logBytesPerPackage = log_bytes;
  188. }
  189. void log_producer_config_set_max_buffer_limit(log_producer_config* config, int64_t max_buffer_bytes)
  190. {
  191. if (config == NULL || max_buffer_bytes < 0)
  192. {
  193. return;
  194. }
  195. config->maxBufferBytes = max_buffer_bytes;
  196. }
  197. void log_producer_config_set_send_thread_count(log_producer_config* config, int32_t thread_count)
  198. {
  199. if (config == NULL || thread_count < 0)
  200. {
  201. return;
  202. }
  203. config->sendThreadCount = thread_count;
  204. }
  205. void log_producer_config_set_connect_timeout_sec(log_producer_config* config, int32_t connect_timeout_sec)
  206. {
  207. if (config == NULL || connect_timeout_sec <= 0)
  208. {
  209. return;
  210. }
  211. config->connectTimeoutSec = connect_timeout_sec;
  212. }
  213. void log_producer_config_set_send_timeout_sec(log_producer_config* config, int32_t send_timeout_sec)
  214. {
  215. if (config == NULL || send_timeout_sec <= 0)
  216. {
  217. return;
  218. }
  219. config->sendTimeoutSec = send_timeout_sec;
  220. }
  221. void log_producer_config_set_destroy_flusher_wait_sec(log_producer_config* config, int32_t destroy_flusher_wait_sec)
  222. {
  223. if (config == NULL || destroy_flusher_wait_sec <= 0)
  224. {
  225. return;
  226. }
  227. config->destroyFlusherWaitTimeoutSec = destroy_flusher_wait_sec;
  228. }
  229. void log_producer_config_set_destroy_sender_wait_sec(log_producer_config* config, int32_t destroy_sender_wait_sec)
  230. {
  231. if (config == NULL || destroy_sender_wait_sec <= 0)
  232. {
  233. return;
  234. }
  235. config->destroySenderWaitTimeoutSec = destroy_sender_wait_sec;
  236. }
  237. void log_producer_config_set_compress_type(log_producer_config* config, int32_t compress_type)
  238. {
  239. if (config == NULL || compress_type < 0 || compress_type > 1)
  240. {
  241. return;
  242. }
  243. config->compressType = compress_type;
  244. }
  245. void log_producer_config_set_ntp_time_offset(log_producer_config* config, int32_t ntp_time_offset)
  246. {
  247. if (config == NULL)
  248. {
  249. return;
  250. }
  251. config->ntpTimeOffset = ntp_time_offset;
  252. }
  253. void log_producer_config_set_enable_guard(log_producer_config* config, int enable) {
  254. if (config == NULL)
  255. return;
  256. config->enableGuarder = enable;
  257. }
  258. void log_producer_config_set_tokenCallBack(log_producer_config* config, Log_GetTokenCallBack fun)
  259. {
  260. if (config == NULL)
  261. return;
  262. config->tokenFun = fun;
  263. }
  264. void log_producer_config_set_skyeye_servname(log_producer_config* config, char* servname) {
  265. _copy_config_string(servname, &config->skyeyeServName);
  266. }
  267. void log_producer_config_set_skyeye_cmptId(log_producer_config* config, char* cmptId) {
  268. _copy_config_string(cmptId, &config->skyeyeCmptId);
  269. }
  270. void log_producer_config_set_skyeye_cmptname(log_producer_config* config, char* cmptname) {
  271. _copy_config_string(cmptname, &config->skyeyeCmptName);
  272. }
  273. void log_producer_config_set_skyeye_terminalNo(log_producer_config* config, char* terminalNo) {
  274. _copy_config_string(terminalNo, &config->skyeyeTerminalNo);
  275. }
  276. void log_producer_config_set_skyeye_version(log_producer_config* config, char* version) {
  277. _copy_config_string(version, &config->skyeyeVersion);
  278. }
  279. void log_producer_config_set_skyeye_sn(log_producer_config* config, char* sn) {
  280. _copy_config_string(sn, &config->skyeyeSn);
  281. }
  282. void log_producer_config_set_skyeye_item(log_producer_config* config, char* item) {
  283. _copy_config_string(item, &config->skyeyeItem);
  284. }
  285. void log_producer_config_set_skyeye_entityname(log_producer_config* config, char* entityname) {
  286. _copy_config_string(entityname, &config->skyeyeEntityName);
  287. }
  288. void log_producer_config_set_united_appname(log_producer_config* config, char* appname) {
  289. _copy_config_string(appname, &config->unitedAppName);
  290. }
  291. void log_producer_config_set_united_version(log_producer_config* config, char* version) {
  292. _copy_config_string(version, &config->unitedVersion);
  293. }
  294. void log_producer_config_set_united_needArchived(log_producer_config* config, int needArchived) {
  295. config->unitedNeedArchived = needArchived;
  296. }
  297. void log_producer_config_set_united_deployUnitId(log_producer_config* config, char* deployUnitId) {
  298. _copy_config_string(deployUnitId, &config->unitedDeployUnitId);
  299. }
  300. void log_producer_config_set_united_serviceUnitId(log_producer_config* config, char* serviceUnitId) {
  301. _copy_config_string(serviceUnitId, &config->unitedServiceUnitId);
  302. }
  303. void log_producer_config_get_upload_info(unsigned long* t_upload_TerminalSys_Suc, unsigned long* t_upload_TerminalUser_Suc,
  304. unsigned long* t_upload_BussinessSys_Suc, unsigned long* t_upload_BussinessUser_Suc, unsigned long* t_upload_beidou_Suc,
  305. unsigned long* t_upload_TerminalSys_Err, unsigned long* t_upload_TerminalUser_Err,
  306. unsigned long* t_upload_BussinessSys_Err, unsigned long* t_upload_BussinessUser_Err, unsigned long* t_upload_beidou_Err)
  307. {
  308. *t_upload_TerminalSys_Suc = g_upload_TerminalSys_Suc;
  309. *t_upload_TerminalUser_Suc = g_upload_TerminalUser_Suc;
  310. *t_upload_BussinessSys_Suc = g_upload_BussinessSys_Suc;
  311. *t_upload_BussinessUser_Suc = g_upload_BussinessUser_Suc;
  312. *t_upload_beidou_Suc = g_upload_beidou_Suc;
  313. *t_upload_TerminalSys_Err = g_upload_TerminalSys_Err;
  314. *t_upload_TerminalUser_Err = g_upload_TerminalUser_Err;
  315. *t_upload_BussinessSys_Err = g_upload_BussinessSys_Err;
  316. *t_upload_BussinessUser_Err = g_upload_BussinessUser_Err;
  317. *t_upload_beidou_Err = g_upload_beidou_Err;
  318. }
  319. LOG_EXPORT void log_producer_config_set_dbgFun(dbg_callback t_callback)
  320. {
  321. g_callback = t_callback;
  322. }
  323. void log_producer_config_set_send_thread_wait_ms(log_producer_config* config, int32_t ts)
  324. {
  325. config->sendThreadWaitMs = ts;
  326. }
  327. void log_producer_config_set_endpoint(log_producer_config* config, const char* endpoint)
  328. {
  329. if (strlen(endpoint) < 8) {
  330. return;
  331. }
  332. if (strncmp(endpoint, "http://", 7) == 0)
  333. {
  334. endpoint += 7;
  335. }
  336. else if (strncmp(endpoint, "https://", 8) == 0)
  337. {
  338. config->using_https = 1;
  339. endpoint += 8;
  340. }
  341. _copy_config_string(endpoint, &config->endpoint);
  342. }
  343. void log_producer_config_set_skyeye_user_topic(log_producer_config* config, const char* topic)
  344. {
  345. _copy_config_string(topic, &config->skyeye_user_topic);
  346. }
  347. void log_producer_config_set_skyeye_sys_topic(log_producer_config* config, const char* topic)
  348. {
  349. _copy_config_string(topic, &config->skyeye_sys_topic);
  350. }
  351. void log_producer_config_set_business_user_topic(log_producer_config* config, const char* topic)
  352. {
  353. _copy_config_string(topic, &config->business_user_topic);
  354. }
  355. void log_producer_config_set_business_sys_topic(log_producer_config* config, const char* topic)
  356. {
  357. _copy_config_string(topic, &config->business_sys_topic);
  358. }
  359. void log_producer_config_set_vtmweb_topic(log_producer_config* config, const char* topic)
  360. {
  361. _copy_config_string(topic, &config->vtmweb_topic);
  362. }
  363. void log_producer_config_set_beidou_topic(log_producer_config* config, const char* topic)
  364. {
  365. _copy_config_string(topic, &config->beidou_topic);
  366. }
  367. int log_producer_config_is_valid(log_producer_config* config)
  368. {
  369. if (config == NULL)
  370. {
  371. aos_error_log((LB, "invalid producer config"));
  372. return 0;
  373. }
  374. if (config->enableGuarder && config->endpoint == NULL)
  375. {
  376. aos_error_log((LB, "invalid producer config destination params"));
  377. return 0;
  378. }
  379. if (config->enableGuarder && (config->skyeyeTerminalNo == NULL || config->skyeyeSn == NULL))
  380. {
  381. aos_error_log((LB, "invalid producer skyeye TerminalNo config params"));
  382. return 0;
  383. }
  384. if (config->skyeyeServName == NULL || config->skyeyeCmptId == NULL ||
  385. config->skyeyeCmptName == NULL ||
  386. config->skyeyeVersion == NULL ||
  387. config->skyeyeItem == NULL || config->skyeyeEntityName == NULL)
  388. {
  389. aos_error_log((LB, "invalid producer skyeye config params"));
  390. return 0;
  391. }
  392. if (config->unitedAppName == NULL || config->unitedVersion == NULL || config->unitedDeployUnitId == NULL || config->unitedServiceUnitId == NULL)
  393. {
  394. aos_error_log((LB, "invalid producer united config params"));
  395. return 0;
  396. }
  397. if (config->maxBufferBytes < 0 || config->logCountPerPackage < 0 || config->logBytesPerPackage < 0)
  398. {
  399. aos_error_log((LB, "invalid producer config log merge and buffer params"));
  400. return 0;
  401. }
  402. if (config->usePersistent)
  403. {
  404. if (config->persistentFilePath == NULL || config->maxPersistentLogCount <= 0)
  405. {
  406. aos_error_log((LB, "invalid producer persistent config params"));
  407. return 0;
  408. }
  409. if (config->usePersistentEncrypt) {
  410. if (config->PersistentEncryptPassword == NULL)
  411. {
  412. aos_error_log((LB, "invalid producer persistent password config params"));
  413. return 0;
  414. }
  415. }
  416. }
  417. return 1;
  418. }
  419. void log_producer_config_set_using_http(log_producer_config* config, int32_t using_https)
  420. {
  421. if (config == NULL || using_https < 0)
  422. {
  423. return;
  424. }
  425. config->using_https = using_https;
  426. }
  427. int log_producer_persistent_config_is_enabled(log_producer_config* config)
  428. {
  429. if (config == NULL)
  430. {
  431. aos_error_log((LB, "invalid producer config"));
  432. return 0;
  433. }
  434. if (config->usePersistent == 0)
  435. {
  436. return 0;
  437. }
  438. return 1;
  439. }
  440. void log_producer_config_set_persistent(log_producer_config* config,
  441. int32_t persistent)
  442. {
  443. if (config == NULL)
  444. return;
  445. config->usePersistent = persistent;
  446. }
  447. void log_producer_config_set_openDb(log_producer_config* config, int32_t openDB)
  448. {
  449. if (config == NULL)
  450. return;
  451. config->openDB = openDB;
  452. }
  453. void log_producer_config_set_persistent_encrypt(log_producer_config* config, int32_t enable) {
  454. if (config == NULL)
  455. return;
  456. config->usePersistentEncrypt = enable;
  457. }
  458. void log_producer_config_set_persistent_encrypt_password(log_producer_config* config, char* password) {
  459. if (config == NULL)
  460. return;
  461. _copy_config_string(password, &config->PersistentEncryptPassword);
  462. }
  463. void log_producer_config_set_persistent_file_path(log_producer_config* config,
  464. const char* file_path)
  465. {
  466. if (config == NULL)
  467. return;
  468. _copy_config_string(file_path, &config->persistentFilePath);
  469. }
  470. void log_producer_config_set_persistent_max_log_count(log_producer_config* config,
  471. int32_t max_log_count)
  472. {
  473. if (config == NULL)
  474. return;
  475. config->maxPersistentLogCount = max_log_count;
  476. }
  477. LOG_EXPORT void log_producer_config_switch_debug_mode(log_producer_config* config, int32_t switch_on)
  478. {
  479. if (config == NULL)
  480. return;
  481. config->debugMode = switch_on;
  482. }
  483. LOG_EXPORT void log_producer_config_memory_deal_type(log_producer_config* config, int32_t memory_deal_type)
  484. {
  485. if (config == NULL)
  486. return;
  487. config->useMemoryType = memory_deal_type;
  488. }