log_api.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. #include "RestfulFunc.h"
  2. #include "JsonConvertHelper.hpp"
  3. #include <sstream>
  4. #include <string>
  5. #include <locale>
  6. #include "log_util.h"
  7. #include "log_api.h"
  8. #include <string.h>
  9. #include "sds.h"
  10. #include "inner_log.h"
  11. #include "baseFun.h"
  12. #include <log_producer_client.h>
  13. //#define GBK_COMPACT
  14. static int is_utf8(const char* str)
  15. {
  16. int i;
  17. const unsigned char* bytes = (const unsigned char*)str;
  18. int num;
  19. if (str == NULL)
  20. return 1;
  21. #if 0
  22. while (*bytes != 0x00) {
  23. if ((*bytes & 0x80) == 0x00) {
  24. // U+0000 to U+007F
  25. num = 1;
  26. } else if ((*bytes & 0xE0) == 0xC0) {
  27. // U+0080 to U+07FF
  28. num = 2;
  29. } else if ((*bytes & 0xF0) == 0xE0) {
  30. // U+0800 to U+FFFF
  31. num = 3;
  32. } else if ((*bytes & 0xF8) == 0xF0) {
  33. // U+10000 to U+10FFFF
  34. num = 4;
  35. } else {
  36. return 0;
  37. }
  38. bytes += 1;
  39. for (i = 1; i < num; ++i) {
  40. if ((*bytes & 0xC0) != 0x80)
  41. return 0;
  42. bytes += 1;
  43. }
  44. }
  45. return 1;
  46. #else
  47. while (*bytes) {
  48. if ((// ASCII
  49. // use bytes[0] <= 0x7F to allow ASCII control characters
  50. bytes[0] == 0x09 ||
  51. bytes[0] == 0x0A ||
  52. bytes[0] == 0x0D ||
  53. (0x20 <= bytes[0] && bytes[0] <= 0x7E)
  54. )
  55. ) {
  56. bytes += 1;
  57. continue;
  58. }
  59. if ((// non-overlong 2-byte
  60. (0xC2 <= bytes[0] && bytes[0] <= 0xDF) &&
  61. (0x80 <= bytes[1] && bytes[1] <= 0xBF)
  62. )
  63. ) {
  64. bytes += 2;
  65. continue;
  66. }
  67. if ((// excluding overlongs
  68. bytes[0] == 0xE0 &&
  69. (0xA0 <= bytes[1] && bytes[1] <= 0xBF) &&
  70. (0x80 <= bytes[2] && bytes[2] <= 0xBF)
  71. ) ||
  72. (// straight 3-byte
  73. ((0xE1 <= bytes[0] && bytes[0] <= 0xEC) ||
  74. bytes[0] == 0xEE ||
  75. bytes[0] == 0xEF) &&
  76. (0x80 <= bytes[1] && bytes[1] <= 0xBF) &&
  77. (0x80 <= bytes[2] && bytes[2] <= 0xBF)
  78. ) ||
  79. (// excluding surrogates
  80. bytes[0] == 0xED &&
  81. (0x80 <= bytes[1] && bytes[1] <= 0x9F) &&
  82. (0x80 <= bytes[2] && bytes[2] <= 0xBF)
  83. )
  84. ) {
  85. bytes += 3;
  86. continue;
  87. }
  88. if ((// planes 1-3
  89. bytes[0] == 0xF0 &&
  90. (0x90 <= bytes[1] && bytes[1] <= 0xBF) &&
  91. (0x80 <= bytes[2] && bytes[2] <= 0xBF) &&
  92. (0x80 <= bytes[3] && bytes[3] <= 0xBF)
  93. ) ||
  94. (// planes 4-15
  95. (0xF1 <= bytes[0] && bytes[0] <= 0xF3) &&
  96. (0x80 <= bytes[1] && bytes[1] <= 0xBF) &&
  97. (0x80 <= bytes[2] && bytes[2] <= 0xBF) &&
  98. (0x80 <= bytes[3] && bytes[3] <= 0xBF)
  99. ) ||
  100. (// plane 16
  101. bytes[0] == 0xF4 &&
  102. (0x80 <= bytes[1] && bytes[1] <= 0x8F) &&
  103. (0x80 <= bytes[2] && bytes[2] <= 0xBF) &&
  104. (0x80 <= bytes[3] && bytes[3] <= 0xBF)
  105. )
  106. ) {
  107. bytes += 4;
  108. continue;
  109. }
  110. return 0;
  111. }
  112. return 1;
  113. #endif
  114. }
  115. std::wstring s2w(const std::string str)
  116. {
  117. if (str.empty()) {
  118. return L"";
  119. }
  120. std::wstring w_str(L"");
  121. const char* origin = setlocale(LC_CTYPE, NULL);
  122. unsigned len = str.size() + 1;
  123. setlocale(LC_CTYPE, "zh_CN.gbk");
  124. wchar_t* p = new wchar_t[len];
  125. if (-1 != mbstowcs(p, str.c_str(), len)) {
  126. w_str = p;
  127. } else {
  128. aos_error_log((LB, "s2w::mbstowcs failed"));
  129. }
  130. delete[] p;
  131. setlocale(LC_CTYPE, origin);
  132. return w_str;
  133. }
  134. std::string w2utf8(const std::wstring wstr)
  135. {
  136. if (wstr.empty()) {
  137. return "";
  138. }
  139. std::string str("");
  140. char* origin = setlocale(LC_CTYPE, NULL);
  141. unsigned len = wstr.size() + 1;
  142. setlocale(LC_CTYPE, "zh_CN.utf8");
  143. char* p = new char[len];
  144. if (-1 != wcstombs(p, wstr.c_str(), len)) {
  145. str = p;
  146. } else {
  147. aos_error_log((LB, "w2utf8::wcstombs failed"));
  148. }
  149. setlocale(LC_CTYPE, origin);
  150. return str;
  151. }
  152. std::string gbk2utf8(const std::string str)
  153. {
  154. std::wstring wstr = s2w(str);
  155. return w2utf8(wstr);
  156. }
  157. /*
  158. if (result->statusCode / 100 == 2)
  159. {
  160. return LOG_SEND_OK;
  161. }
  162. if (result->statusCode <= 0)
  163. {
  164. return LOG_SEND_NETWORK_ERROR;
  165. }
  166. if (result->statusCode >= 500 || result->requestID == NULL)
  167. {
  168. return LOG_SEND_SERVER_ERROR;
  169. }
  170. if (result->statusCode == 403)
  171. {
  172. return LOG_SEND_QUOTA_EXCEED;
  173. }
  174. if (result->statusCode == 401 || result->statusCode == 404)
  175. {
  176. return LOG_SEND_UNAUTHORIZED;
  177. }
  178. if (result->errorMessage != NULL && strstr(result->errorMessage, LOGE_TIME_EXPIRED) != NULL)
  179. {
  180. return LOG_SEND_TIME_ERROR;
  181. }
  182. return LOG_SEND_DISCARD_ERROR;
  183. */
  184. int LOG_OS_HttpPost(const char* url,
  185. char** header_array,
  186. int header_count,
  187. const void* data,
  188. int data_len, const char* channelId, const char* token, const char* terminalno, const char* reserve1)
  189. {
  190. int retCode = 480;
  191. HttpClientResponseResult result;
  192. HttpClientRequestConfig config(HttpRequestMethod::POST, url);
  193. config.SetTimeout(60);
  194. std::string str((const char*)data);
  195. #ifdef GBK_COMPACT
  196. if (!str.empty() && !is_utf8(str.c_str())) {
  197. std::string t = gbk2utf8(str);
  198. str = t;
  199. }
  200. #endif //GBK_COMPACT
  201. config.SetJsonBody(str.c_str());
  202. RestfulClient client = RestfulClient::getInstance();
  203. config.PreDo();
  204. client.Do(&config, &result, NULL);//upload log should not add trace link, which will cause infinite loop
  205. if (result.ResponseOK()) {
  206. retCode = 481;
  207. struct CommResponse
  208. {
  209. bool success;
  210. std::string errorCode;
  211. std::string returnCode;
  212. std::string errorMsg;
  213. std::string message;
  214. JSONCONVERT2OBJECT_MEMEBER_REGISTER(success, errorCode, returnCode, errorMsg, message)
  215. JSONCONVERT2OBJECT_MEMEBER_RENAME_REGISTER("success", "code", "return_code", "error_msg", "message")
  216. } responseIns;
  217. Json::Value rawRoot;
  218. if (GetJsonRootObject(rawRoot, result.content) && Json2Object(responseIns, rawRoot)) {
  219. if (responseIns.success) {
  220. retCode = 200;
  221. } else {
  222. /*
  223. HttpClientRequestConfig sendErr(HttpRequestMethod::POST, "http://99.12.43.134:9000/upload_msg");
  224. HttpClientResponseResult curResult;
  225. Json::Value rootReq;
  226. Json::FastWriter writer;
  227. rootReq["req"] = str.c_str();
  228. rootReq["ans"] = result.content;
  229. std::string jsonReq = writer.write(rootReq);
  230. sendErr.SetJsonBody(jsonReq.c_str());
  231. RestfulClient client = RestfulClient::getInstance();
  232. sendErr.PreDo();
  233. client.Do(&sendErr, &curResult);
  234. */
  235. if (responseIns.errorCode.find("RTI1002") != -1)
  236. retCode = 300;
  237. else if (!is_utf8((const char*)str.c_str())) {
  238. aos_warn_log((LB, "detect that content is not utf8-type ,skip it: %s", str.c_str()));
  239. retCode = 200;
  240. }
  241. }
  242. } else {
  243. aos_error_log((LB, "http %s extract failed: %s", url, result.content.c_str()));
  244. aos_info_log((LB, "content len: %d", data_len));
  245. }
  246. } else {
  247. retCode = result.statusCode;
  248. }
  249. return retCode;
  250. }
  251. log_status_t sls_log_init(int32_t log_global_flag)
  252. {
  253. return 0;
  254. }
  255. void sls_log_destroy()
  256. {
  257. }
  258. static const char sls_month_snames[12][4] =
  259. {
  260. "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  261. };
  262. static const char sls_day_snames[7][4] =
  263. {
  264. "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
  265. };
  266. void sls_rfc822_date(char *date_str, struct tm * xt)
  267. {
  268. const char *s = NULL;
  269. int real_year = 2000;
  270. /* example: "Sat, 08 Jan 2000 18:31:41 GMT" */
  271. /* 12345678901234567890123456789 */
  272. s = &sls_day_snames[xt->tm_wday][0];
  273. *date_str++ = *s++;
  274. *date_str++ = *s++;
  275. *date_str++ = *s++;
  276. *date_str++ = ',';
  277. *date_str++ = ' ';
  278. *date_str++ = xt->tm_mday / 10 + '0';
  279. *date_str++ = xt->tm_mday % 10 + '0';
  280. *date_str++ = ' ';
  281. s = &sls_month_snames[xt->tm_mon][0];
  282. *date_str++ = *s++;
  283. *date_str++ = *s++;
  284. *date_str++ = *s++;
  285. *date_str++ = ' ';
  286. real_year = 1900 + xt->tm_year;
  287. /* This routine isn't y10k ready. */
  288. *date_str++ = real_year / 1000 + '0';
  289. *date_str++ = real_year % 1000 / 100 + '0';
  290. *date_str++ = real_year % 100 / 10 + '0';
  291. *date_str++ = real_year % 10 + '0';
  292. *date_str++ = ' ';
  293. *date_str++ = xt->tm_hour / 10 + '0';
  294. *date_str++ = xt->tm_hour % 10 + '0';
  295. *date_str++ = ':';
  296. *date_str++ = xt->tm_min / 10 + '0';
  297. *date_str++ = xt->tm_min % 10 + '0';
  298. *date_str++ = ':';
  299. *date_str++ = xt->tm_sec / 10 + '0';
  300. *date_str++ = xt->tm_sec % 10 + '0';
  301. *date_str++ = ' ';
  302. *date_str++ = 'G';
  303. *date_str++ = 'M';
  304. *date_str++ = 'T';
  305. *date_str++ = 0;
  306. return;
  307. }
  308. void get_now_time_str(char * buffer, int bufLen, int timeOffset)
  309. {
  310. time_t rawtime = LOG_GET_TIME();
  311. struct tm * timeinfo;
  312. if (timeOffset != 0)
  313. {
  314. rawtime += timeOffset;
  315. }
  316. timeinfo = gmtime(&rawtime);
  317. sls_rfc822_date(buffer, timeinfo);
  318. }
  319. void post_log_result_destroy(post_log_result * result)
  320. {
  321. if (result != NULL)
  322. {
  323. if (result->errorMessage != NULL)
  324. {
  325. sdsfree(result->errorMessage);
  326. }
  327. if (result->requestID != NULL)
  328. {
  329. sdsfree(result->requestID);
  330. }
  331. free(result);
  332. }
  333. }
  334. struct cur_slist {
  335. char *data;
  336. struct cur_slist *next;
  337. };
  338. struct cur_slist * cur_slist_append(struct cur_slist *lst, const char *s)
  339. {
  340. struct cur_slist* orig;
  341. struct cur_slist *t = (struct cur_slist *)malloc(sizeof(struct cur_slist));
  342. #ifdef WIN32
  343. t->data = _strdup(s);
  344. #else
  345. t->data = strdup(s);
  346. #endif
  347. t->next = NULL;
  348. if(lst == NULL)
  349. return t;
  350. orig = lst;
  351. while(lst->next) {
  352. lst = lst->next;
  353. }
  354. lst->next = t;
  355. return orig;
  356. }
  357. void cur_slist_free_all(struct cur_slist *lst)
  358. {
  359. while(lst != NULL) {
  360. struct cur_slist *n = lst->next;
  361. free(lst->data);
  362. free(lst);
  363. lst = n;
  364. }
  365. }
  366. post_log_result* post_logs(const char* endpoint, const char* accesskeyId, const char* accessKey, const char* stsToken, lz4_log_buf* buffer, log_post_option* option,
  367. const char* channelId, const char* token, const char *terminalno, const char *reserve1)
  368. {
  369. post_log_result * result = (post_log_result *)malloc(sizeof(post_log_result));
  370. memset(result, 0, sizeof(post_log_result));
  371. {
  372. //headers
  373. struct cur_slist* headers = NULL;
  374. int res;
  375. char* header_array[50];
  376. int header_count = 0;
  377. // url
  378. sds url = NULL;
  379. if (option->using_https) {
  380. url = sdsnew("https://");
  381. } else {
  382. url = sdsnew("http://");
  383. }
  384. url = sdscat(url, endpoint);
  385. if(buffer->data != NULL && buffer->length != 0)
  386. res = LOG_OS_HttpPost(url, header_array, header_count, (const void *) buffer->data, buffer->length, channelId, token, terminalno, reserve1);
  387. /*
  388. if (buffer->type == LOG_TYPE_BEIDOU)
  389. {
  390. for (auto i = 0; i < buffer->n_logs; i++)
  391. {
  392. std::string dstLog = std::string(buffer->uuid[i]) + " post_logs_beidou";
  393. log_producer_sendTestLog_loki("RVCLogSDK", buffer->uuid[i], dstLog.c_str());
  394. }
  395. }
  396. else
  397. {
  398. for (auto i = 0; i < buffer->n_logs; i++)
  399. {
  400. std::string dstLog = std::string(buffer->uuid[i]) + " post_logs_skyeye";
  401. log_producer_sendTestLog_loki("RVCLogSDK", buffer->uuid[i], dstLog.c_str());
  402. }
  403. }
  404. */
  405. result->statusCode = res;
  406. cur_slist_free_all(headers); /* free the list again */
  407. sdsfree(url);
  408. }
  409. return result;
  410. }